#ifndef _HSLSHIFT_FXH_
#define _HSLSHIFT_FXH_
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//  HSL Shift effect11 by kingeric1992
//                                          Nov.4.2017
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include "Shaders/enbInternals.fxh"

static const float HSLShift_Threshold_Base  = 0.05;
static const float HSLShift_Threshold_Curve = 1.0;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

struct HSLShiftStruct {
    bool      State;
    float     Weight;
    float4    node[9];
};

//todo: rethink the logic
float3 HSLShift(float3 col, HSLShiftStruct IN)
{
    if(!IN.State) return col;
    float3 hsl = RGB_to_HSL(col);
    float base;
    for(int i=0; i<8; i++) if(IN.node[i].a < hsl.r*360.0 ) base = i;

    float w = saturate((hsl.r*360.0-IN.node[base].a)/(IN.node[base+1].a-IN.node[base].a));

    float3 H0 = RGB_to_HSL(IN.node[base].rgb);
    float3 H1 = RGB_to_HSL(IN.node[base+1].rgb);

    H1.x += (H1.x < H0.x)? 1.0:0.0;

    float3 shift    = frac(lerp( H0, H1 , w));
           w        = max( hsl.g, 0.0) * max( 1.0-hsl.b, 0.0);
           shift.b -=0.5; 
           shift.b *= (pow(w, HSLShift_Threshold_Curve) * (1.0-HSLShift_Threshold_Base) + HSLShift_Threshold_Base) * 2.0; //[-1.0, 1.0]
    return max(HSL_to_RGB(saturate(float3(shift.r, hsl.g*(shift.g*2.0), hsl.b*(1.0+shift.b)))), 0.0);
}

#endif //_HSLSHIFT_FXH_