shader_type canvas_item;
render_mode blend_mix;

uniform sampler2D color_gradient;
uniform sampler2D luminosity_gradient;
uniform bool flash;
uniform float flash_strength = 0.4;
uniform vec4 highlight : source_color = vec4(1,1,1,0);


void vertex() {
}

void fragment() {
	vec4 col = texture(TEXTURE, UV);
	vec4 cgrad_col = texture(color_gradient, vec2(col.r));
	
	vec3 c = vec3(cgrad_col.xyz);
	vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
	vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
	vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
	float d = q.x - min(q.w, q.y);
	float e = 1.0e-10;
	vec3 hsv = vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
	
	vec4 l_col = texture(luminosity_gradient, vec2(col.g));

	vec3 fin_hsv = vec3(hsv.x, hsv.y, l_col.x);


	c = fin_hsv;
	K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
	vec3 p2 = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
	vec3 fin_rgb = c.z * mix(K.xxx, clamp(p2 - K.xxx, 0.0, 1.0), c.y);
	vec4 h = highlight;
	if(flash){
		h.a = flash_strength * (sin(TIME * 10.0) + 1.0)/2.0;
	}
	COLOR.rgb = mix(fin_rgb, h.rgb, h.a);
	COLOR.a = col.a;


}