Thread: HSLtoRGB
View Single Post
  #1  
Old 02-16-2010, 08:19 PM
Crow Crow is offline
ǝɔɐɹq ʎןɹnɔ
Crow's Avatar
Join Date: Dec 2006
Location: Germany
Posts: 5,153
Crow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond reputeCrow has a reputation beyond repute
HSLtoRGB

Converts colors from the HSL color space to the RGB color space. I copied and converted this from a toolkit for C#. Judging from my tests, it works well.

PHP Code:
function HSLtoRGB(HSL) {
  
temp.0;
  
temp.0;
  
temp.0;
  
  
: (H);
  
: (S);
  
: (L);
  
  if (
== 0) {
    
0;
  } else {
    if (
== 0) {
      
L;
    } else {
      
temp.v1 <= 0.5 * (S) : - (S);
      
temp.v2 temp.v1;
      
      
temp.t3  = { 3H};
      
temp.clr = new[3];
      
      for (
temp.03i++) {
        if (
temp.t3[i] < 0)
          
temp.t3[i]++;
        if (
temp.t3[i] > 1)
          
temp.t3[i]--;
        
        if (
temp.t3[i] * 1)
          
temp.clr[i] = temp.v2 + (temp.v1 temp.v2)
                        * 
temp.t3[i] * 6;
        else if (
temp.t3[i] * 1)
          
temp.clr[i] = temp.v1;
        else if (
temp.t3[i] * 2)
          
temp.clr[i] = temp.v2 + (temp.v1 temp.v2)
                        * (
temp.t3[i]) * 6;
        else
          
temp.clr[i] = temp.v2;
      }
    }
  }
  
  
temp.clr[0];
  
temp.clr[1];
  
temp.clr[2];
  return { 
RG};


Example usage:
PHP Code:
temp.rgb HSLtoRGB(0.010.70.5); // slightly desaturated medium red
echo(temp.rgb); // echoes "0.85,0.192,0.15" 

H, S and L have to be values between 0 and 1. The returned RGB values are also between 0 and 1.

Enjoy!
Reply With Quote