uHSL2RGB

From Second Life Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

User-Defined Function: vector uHSL2RGB( vector vColHSL );

Returns a percentage based vector that is vColHSL converted to a RGB.

  • vColHSL: source color as percentages of range.


Code:

  • LSO: 420 bytes
  • MONO: 1536 bytes
vector uHSL2RGB( vector vColHSL ){ //-- <H, S, L>
	vector vColRGB;
	if (vColHSL.y){
		vColRGB.x = (1.0 - llFabs( 2 * vColHSL.z - 1.0 )) * vColHSL.y;                                             //-- C
		vColHSL.x = vColHSL.x * 6.0;                                                                               //-- H'
		vColRGB.y = vColRGB.x * (1.0 - llFabs( (integer)vColHSL.x % 2 + (vColHSL.x - (integer)vColHSL.x) - 1.0 )); //-- X 
		vColRGB = llList2Vector( [<vColRGB.x, vColRGB.y, vColRGB.z>,
		                          <vColRGB.y, vColRGB.x, vColRGB.z>,
		                          <vColRGB.z, vColRGB.x, vColRGB.y>,
		                          <vColRGB.z, vColRGB.y, vColRGB.x>,
		                          <vColRGB.y, vColRGB.z, vColRGB.x>,
		                          <vColRGB.x, vColRGB.z, vColRGB.y>],
		                         (integer)vColHSL.x % 6 ) + (vColHSL.z - 0.5 * vColRGB.x) * <1.0, 1.0, 1.0>;
	}else{
		vColRGB.x = vColRGB.y = vColRGB.z = vColHSL.z; //-- greyscale
	}
	return vColRGB;
}
/*//--                       Anti-License Text                         --//*/
/*//     Contributed Freely to the Public Domain without limitation.     //*/
/*//   2011 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                 --//*/

Caveats

  • This function does not check input HSL validity.
    • expected values are in percentage of range (value / range)
  • HSL is kept percentage based here for best compatibility with image mapping

Notes

  • To convert RGB or HSL values from other software to percentage based, simply divide each value by it's range
    • range is determined by subtracting the lower bound from the upper bound.
  • To convert RGB or HSL percentages to your preferred format, multiply the value by the preferred range.