Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   HSLtoRGB (https://forums.graalonline.com/forums/showthread.php?t=134258034)

Crow 02-16-2010 08:19 PM

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!

coreys 02-17-2010 02:09 AM

Very nice :)

adam 03-15-2010 06:40 PM

Props for the function AND that avatar! Best platformer ever.


All times are GMT +2. The time now is 11:15 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.