// R if(6.0*temp3R<1) R = temp1+(temp2-temp1)*6.0*temp3R; else if(temp3R<0.5) R = temp2; else if(3.0*temp3R<2) R = temp1+(temp2-temp1)*(4.0-temp3R*6.0); else R=temp1;
// G if(6.0*temp3G<1) G = temp1+(temp2-temp1)*6.0*temp3G; else if(temp3G<0.5) G = temp2; else if(3.0*temp3G<2) G = temp1+(temp2-temp1)*(4.0-temp3G*6.0); else G=temp1;
// B if( 6.0*temp3B<1.0) B = temp1+(temp2-temp1)*6.0*temp3B; else if(temp3B<0.5) B = temp2; else if(3.0*temp3B<2.0) B = temp1+(temp2-temp1)*(4.0-temp3B*6.0); else B=temp1; return float4(R,G,B,HSLColor.a); }
float4 RGB2HSL(float4 RGB)
{
// convert rgb into HSL
float maxChnl = max(max(RGB.r,RGB.b),RGB.g);
float minChnl = min(min(RGB.r,RGB.b),RGB.g);
float delta = maxChnl - minChnl;
float sum = maxChnl + minChnl;
float H=0.0f, S=0.0f, L=0.0f; L = sum/2.0f;
if( delta != 0 ){
if( RGB.r == maxChnl ) H = (RGB.g-RGB.b)/delta;
if( RGB.g == maxChnl ) H = 2.0f + (RGB.b-RGB.r)/delta;
if( RGB.b == maxChnl ) H = 4.0f + (RGB.r-RGB.g)/delta;
if (H < 0) H = H + 6;
if( L < 0.5f )
S=delta/sum;
else
S=delta/(2.0f-sum);
}else{
S=0;H=0; // H: undefined
}
return float4(H/6.0,S,L,RGB.a);
}// rgb to hsl conversion
float4 HSL2RGB(float4 HSLColor)
{
float H,S,L;
H = HSLColor.r;
S = HSLColor.g;
L = HSLColor.b;
float R,G,B,A;
// convert HSL into rgb
float temp1=0, temp2=0, temp3R=0,temp3G=0,temp3B=0;
if( S == 0 )
return float4(L,L,L,HSLColor.a); if( L < 0.5 ) temp2 = L*(1.0f+S);
else temp2 = L+S-L*S; temp1 = 2.0f * L - temp2;
temp3R = H + 1.0f/3.0f;
temp3G = H;
temp3B = H-1.0f/3.0f;
temp3R = temp3R - floor(temp3R);
temp3G = temp3G - floor(temp3G);
temp3B = temp3B - floor(temp3B);
// R
if(6.0*temp3R<1) R = temp1+(temp2-temp1)*6.0*temp3R;
else if(temp3R<0.5) R = temp2;
else if(3.0*temp3R<2) R = temp1+(temp2-temp1)*(4.0-temp3R*6.0);
else R=temp1;
// G
if(6.0*temp3G<1) G = temp1+(temp2-temp1)*6.0*temp3G;
else if(temp3G<0.5) G = temp2;
else if(3.0*temp3G<2) G = temp1+(temp2-temp1)*(4.0-temp3G*6.0);
else G=temp1;
// B
if( 6.0*temp3B<1.0) B = temp1+(temp2-temp1)*6.0*temp3B;
else if(temp3B<0.5) B = temp2;
else if(3.0*temp3B<2.0) B = temp1+(temp2-temp1)*(4.0-temp3B*6.0);
else B=temp1;
return float4(R,G,B,HSLColor.a);
}