/*
   Shader Modified: Pokefan531
   Color Mangler
   Author: hunterk
   License: Public domain
*/

// Shader that replicates the LCD dynamics from a GameBoy Advance
#pragma parameter target_gamma "Darken Intensity" 2.9 1.0 2.9 0.05
#ifdef PARAMETER_UNIFORM
uniform float target_gamma;
#else
#define target_gamma 2.9
#endif

#define display_gamma 1.45
#define sat 1.0
#define lum 1.0
#define contrast 1.0
#define blr 0.0
#define blg 0.0
#define blb 0.0
#define r 0.74
#define g 0.68
#define b 0.68
#define rg 0.08
#define rb 0.08
#define gr 0.26
#define gb 0.24
#define br 0.0
#define bg 0.24
#define overscan_percent_x 0.0
#define overscan_percent_y 0.0

struct input
{
  float2 video_size;
  float2 texture_size;
  float2 output_size;
  float  frame_count;
  float  frame_direction;
  float frame_rotation;
};

void main_vertex
(
   float4 position : POSITION,
   out float4 oPosition : POSITION,
   uniform float4x4 modelViewProj,
   uniform input IN,

   float2 tex : TEXCOORD,
   out float2 oTex : TEXCOORD
)
{
   oPosition = mul(modelViewProj, position);
   float2 shift = 0.5 * IN.video_size / IN.texture_size;
   float2 overscan_coord = (tex - shift) * (1.0 - float2(overscan_percent_x / 100.0, overscan_percent_y / 100.0)) + shift;
   oTex = overscan_coord;
}

float4 main_fragment(float2 tex : TEXCOORD, uniform sampler2D s0 : TEXUNIT0) : COLOR
{
float4 screen = pow(tex2D(s0, tex), target_gamma).rgba; //sample image in linear colorspace
float4 avglum = float4(0.5);
screen = lerp(screen, avglum, (1.0 - contrast));

//				r   g    b   black
mat4x4 color = {r,  gr,  br, blr,  //red channel
			   rg,   g,  bg, blg,  //green channel
			   rb,  gb,   b, blb,  //blue channel
			  0.0, 0.0, 0.0,    1.0}; //alpha channel; these numbers do nothing for our purposes.
			  
mat4x4 adjust = {(1.0 - sat) * 0.3086 + sat, (1.0 - sat) * 0.6094,          (1.0 - sat) * 0.0820,          0.0,
			    (1.0 - sat) * 0.3086,          (1.0 - sat) * 0.6094 + sat, (1.0 - sat) * 0.0820,          0.0,
			    (1.0 - sat) * 0.3086,          (1.0 - sat) * 0.6094,          (1.0 - sat) * 0.0820 + sat, 0.0,
			     1.0, 1.0, 1.0, 1.0};

color = mul(color, adjust);
screen = saturate(screen * lum);
screen = mul(color, screen);
return pow(screen, 1.0 / display_gamma);
}