View Single Post
  #9  
Old 04-11-2011, 04:01 AM
MrOmega MrOmega is offline
One More Time
MrOmega's Avatar
Join Date: Aug 2010
Location: TN, USA
Posts: 631
MrOmega is an unknown quantity at this point
Send a message via AIM to MrOmega Send a message via MSN to MrOmega Send a message via Yahoo to MrOmega
figure I show how to convert RGB to HSL to RGB
PHP Code:
function HSL2RGB( temp.hue, temp.sat, temp.lum)
{

  
temp.chroma = ( 1 - abs( 2 * temp.lum - 1)) * temp.sat;
  
temp.hue    = temp.hue / 60;
  
temp.xVal   = temp.chroma * ( 1 - abs(( temp.hue % 2) - 1));
  
temp.r      = temp.hue == 0? 0: temp.hue < 1? temp.chroma: temp.hue < 2?   temp.xVal: temp.hue < 3?           0: temp.hue < 4?           0: temp.hue < 5?   temp.xVal: temp.chroma;
  
temp.g      = temp.hue == 0? 0: temp.hue < 1?   temp.xVal: temp.hue < 2? temp.chroma: temp.hue < 3? temp.chroma: temp.hue < 4?   temp.xVal: temp.hue < 5?           0:           0;
  
temp.b      = temp.hue == 0? 0: temp.hue < 1?           0: temp.hue < 2?           0: temp.hue < 3?   temp.xVal: temp.hue < 4? temp.chroma: temp.hue < 5? temp.chroma:   temp.xVal;                 
  
temp.m      = temp.lum - 0.5 * temp.chroma;          
 
  return { 
temp.r + temp.m, temp.g + temp.m, temp.b + temp.m};
 
}

function 
RGB2HSL( temp.r, temp.g, temp.b)
{

    
temp.max = max( max( temp.r, temp.g), temp.b);
    
temp.min = min( min( temp.r, temp.g), temp.b);
    
temp.chroma = temp.max - temp.min;
    
temp.a = (( 2 * temp.r) - temp.g - temp.b) / 2;
    
temp.b = (( 3 ^ 0.5) / 2) * ( temp.g - temp.b);
    
temp.hue = arctan( temp.b / temp.a);
    
temp.lum = 0.5 * ( temp.max + temp.min);
    
temp.sat = temp.chroma == 0? 0: ( temp.chroma / ( 1 - abs( 2 * temp.lum - 1)));
    
  return { 
int( radToDeg( temp.hue) % 360), temp.sat, temp.lum};

} 
and now an example to find the Inverted color of an HSL color

lets say we want to find the inverted RGB color with red's HSL

PHP Code:
function onCreated()
{

  
temp.red = { 0, 1, 0.5}; //Red's HSL
  
temp.inv = HSL2RGB(( temp.red[ 0] - 180) % 360, temp.red[ 1], temp.red[ 2]);
  echo( 
temp.inv);

} 
which you get { 0, 1, 1} which is Cyan's RGB
__________________
Time is the fire in which we burn...
Up, Up, Down, Down, Left, Right, Left, Right, B, A, Select, Start! Now I got 99 LIVES!!!
Reply With Quote