shader_type canvas_item;

#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Colors.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Math.gdshaderinc"

uniform sampler2D screenTexture:
  hint_screen_texture,
  repeat_disable,
  filter_linear_mipmap;

uniform sampler2D rChannelCurve;
uniform float rChannelEffectStrength: hint_range(0,1) = 1;

uniform sampler2D gChannelCurve;
uniform float gChannelEffectStrength: hint_range(0,1) = 1;

uniform sampler2D bChannelCurve;
uniform float bChannelEffectStrength: hint_range(0,1) = 1;

uniform sampler2D hChannelCurve;
uniform float hChannelEffectStrength: hint_range(0,1) = 1;

uniform sampler2D sChannelCurve;
uniform float sChannelEffectStrength: hint_range(0,1) = 1;

uniform sampler2D lChannelCurve;
uniform float lChannelEffectStrength: hint_range(0,1) = 1;

uniform sampler2D lumaTemparatureShiftCurve;
uniform float warmenShift: hint_range(-1,1) = 0;
uniform float saturationShift: hint_range(-1,1) = 0;
uniform float luminanceShift: hint_range(-1,1) = 0;
uniform float lumaTemparatureShiftEffectStrength: hint_range(0,1) = 1;


uniform sampler2D lumaSaturationCurve;
uniform float lumaSaturationEffectStrength: hint_range(0,1) = 1;

uniform float effectStrength : hint_range(0,1) = 1;

void fragment()
{
  vec4 rgb = texture( screenTexture, UV );
  vec4 originalRGB = rgb;

  rgb.r = mix( rgb.r, texture( rChannelCurve, vec2( rgb.r, 0.0 ) ).x, rChannelEffectStrength );
  rgb.g = mix( rgb.g, texture( gChannelCurve, vec2( rgb.g, 0.0 ) ).x, gChannelEffectStrength );
  rgb.b = mix( rgb.b, texture( bChannelCurve, vec2( rgb.b, 0.0 ) ).x, bChannelEffectStrength );

  rgb = clamp01_v4( rgb );

  vec3 hsl = RGBtoHSL( rgb.rgb );

  hsl.r = mix( hsl.r,  hsl.r + texture( hChannelCurve, vec2( hsl.r, 0.0 ) ).x - 0.5, hChannelEffectStrength );
  hsl.r = mod( hsl.r, 1.0 );
  hsl.g = mix( hsl.g, texture( sChannelCurve, vec2( hsl.g, 0.0 ) ).x, sChannelEffectStrength );
  hsl.b = mix( hsl.b, texture( lChannelCurve, vec2( hsl.b, 0.0 ) ).x, lChannelEffectStrength );

  vec3 lumaTemparatureShift = vec3( warmenShift, -saturationShift, luminanceShift );
  float lumaTemparatureShiftAmount = texture( lumaTemparatureShiftCurve, vec2( hsl.b, 0 ) ).x * 2.0 - 1.0;
  lumaTemparatureShiftAmount = lumaTemparatureShiftAmount * lumaTemparatureShiftEffectStrength;
  hsl = shiftHSL( hsl, lumaTemparatureShift * lumaTemparatureShiftAmount, 10 );

  hsl.g = mix( hsl.g,  hsl.g + texture( lumaSaturationCurve, vec2( hsl.b, 0.0 ) ).x - 0.5, hChannelEffectStrength );
  rgb = vec4( HSLtoRGB( hsl ), rgb.a );

  COLOR = mix( originalRGB, rgb, effectStrength );
}