Thread: Showimg HSL
View Single Post
  #8  
Old 01-26-2014, 05:08 PM
callimuc callimuc is offline
ジ
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
converted the link fp4 has been posting into gs2 for lazy people

PHP Code:
function onCreated() {
  echo(
rgb2hsl(234, 255, 0));
  echo(
hsl2rgb(196, 100, 50));
}


function 
rgb2hsl(r, g, b) {
  
temp.r = min(max(temp.r, 0), 255) / 255;
  
temp.g = min(max(temp.g, 0), 255) / 255;
  
temp.b = min(max(temp.b, 0), 255) / 255;
  
  
temp.max = max(temp.r, max(temp.g, temp.b));
  
temp.min = min(temp.r, min(temp.g, temp.b));
  
  
temp.h = temp.s = temp.l = (temp.max + temp.min) / 2;
  
  if (
temp.max != temp.min) {
    
temp.d = temp.max - temp.min;
    
temp.s = (temp.l > 0.5 ? temp.d / (2 - temp.max - temp.min) : temp.d / (temp.max + temp.min));
    if (
temp.max == temp.r)
      
temp.h = (temp.g - temp.b) / temp.d + (temp.g < temp.b ? 6 : 0);
    else if (
temp.max == temp.g)
      
temp.h = (temp.b - temp.r) / temp.d + 2;
    else
      
temp.h = (temp.r - temp.g) / temp.d + 4;
    
temp.h /= 6;
  }
  else 
temp.h = temp.s = 0; //achromatic
  
  
return {temp.h*360,temp.s*100,temp.l*100};
}

function 
hsl2rgb(h, s, l) {
  
temp.h = min(max(temp.h, 0), 360) / 360;
  
temp.s = min(max(temp.s, 0), 100) / 100;
  
temp.l = min(max(temp.l, 0), 100) / 100;
  if (
temp.s > 0) {
    
temp.q = (temp.l < 0.5 ? (temp.l * (1 + temp.s)) : (temp.l + temp.s - (temp.l * temp.s)));
    
temp.p = (2 * temp.l) - temp.q;
    
    
temp.r = hue2rgb(temp.p, temp.q, temp.h + 1/3);
    
temp.g = hue2rgb(temp.p, temp.q, temp.h);
    
temp.b = hue2rgb(temp.p, temp.q, temp.h - 1/3);
  }
  else 
temp.r = temp.g = temp.b = 1; //achromatic
  
  
return {temp.r*255, temp.g*255, temp.b*255};
}
function 
hue2rgb(p, q, t) {
  if (
temp.t < 0) temp.t += 1;
  if (
temp.t > 1) temp.t -= 1;
  
  if (
temp.t < 1/6) return (temp.p + (temp.q - temp.p) * 6 * temp.t);
  if (
temp.t < 0.5) return temp.q;
  if (
temp.t < 2/3) return (temp.p + (temp.q - temp.p) * (2/3 - temp.t) * 6);
  return 
temp.p;
} 
__________________
MEEP!
Reply With Quote