shader_type canvas_item;

uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;

uniform float darken_screen;
const float display_gamma = 2.5;
const float target_gamma = 2.2;
const float sat = 1.0;
const float lum = 0.99;
const float contrast = 1.0;

const vec3 bl = vec3(0.0, 0.0, 0.0);
const vec3 r = vec3(0.84, 0.09, 0.15);
const vec3 g = vec3(0.18, 0.67, 0.10);
const vec3 b = vec3(0.0, 0.26, 0.73);

void fragment() {
	vec4 screen = pow(texture(screen_texture, SCREEN_UV), vec4(target_gamma + darken_screen)).rgba;
	vec4 avglum = vec4(0.5);
	screen = mix(screen, avglum, (1.0 - contrast));
	
	mat4 color_mat = mat4(
		vec4(r, 0.0),
		vec4(g, 0.0),
		vec4(b, 0.0),
		vec4(bl, 1.0)
	);
	mat4 adjust = mat4(
		vec4((1.0 - sat) * 0.3086 + sat, (1.0 - sat) * 0.3086, (1.0 - sat) * 0.3086, 1.0),
		vec4((1.0 - sat) * 0.6094, (1.0 - sat) * 0.6094 + sat, (1.0 - sat) * 0.6094, 1.0),
		vec4((1.0 - sat) * 0.0820, (1.0 - sat) * 0.0820, (1.0 - sat) * 0.0820 + sat, 1.0),
		vec4(0.0, 0.0, 0.0, 1.0)
	);
	color_mat *= adjust;
	screen = clamp(screen * lum, 0.0, 1.0);
	screen = color_mat * screen;
	COLOR = pow(screen, vec4(1.0 / display_gamma + (darken_screen * 0.125)));
}

//void light() {
	// Called for every pixel for every light affecting the CanvasItem.
	// Uncomment to replace the default light processing function with this one.
//}
