Difference between revisions of "URGB2HSL"

From Second Life Wiki
Jump to navigation Jump to search
m (~correction of stupid mistake)
m (<lsl> tag to <source>)
 
Line 12: Line 12:
* LSO: 502 bytes
* LSO: 502 bytes
* MONO: 1536 bytes
* MONO: 1536 bytes
<lsl>vector uRGB2HSL( vector vColRGB ){
<source lang="lsl2">vector uRGB2HSL( vector vColRGB ){
vector vColHSL = vColRGB;
vector vColHSL = vColRGB;
if (vColHSL.x < vColHSL.y){
if (vColHSL.x < vColHSL.y){
Line 39: Line 39:
/*//  2011 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]  //*/
/*//  2011 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]  //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
/*//--                                                                --//*/</lsl><!-- Please do not remove license statement -->
/*//--                                                                --//*/</source><!-- Please do not remove license statement -->
}}
}}



Latest revision as of 14:34, 22 January 2015

User-Defined Function: vector uRGB2HSL( vector vColRGB );

Returns a percentage based vector that is vColRGB converted to a HSL.

  • vColRGB: source color as percentages of range.


Code:

  • LSO: 502 bytes
  • MONO: 1536 bytes
vector uRGB2HSL( vector vColRGB ){
	vector vColHSL = vColRGB;
	if (vColHSL.x < vColHSL.y){
		vColHSL = <vColHSL.y, vColHSL.x, vColHSL.z>;
	}
	if (vColHSL.x < vColHSL.z){
		vColHSL = <vColHSL.z, vColHSL.y, vColHSL.x>;
	}else if (vColHSL.y > vColHSL.z){
		vColHSL = <vColHSL.x, vColHSL.z, vColHSL.y>;
	}
	vColHSL.z = (vColHSL.x + vColHSL.y) * 0.5; //-- L
	vColHSL.y = vColHSL.x - vColHSL.y;         //-- C
	if (vColHSL.y){
		vColHSL.x = llList2Float( [(vColRGB.y - vColRGB.z) / vColHSL.y + 6.0 * (vColRGB.z > vColRGB.y),
		                           (vColRGB.z - vColRGB.x) / vColHSL.y + 2.0,
		                           (vColRGB.x - vColRGB.y) / vColHSL.y + 4.0],
		                          llListFindList( [vColRGB.x, vColRGB.y, vColRGB.z], (list)vColHSL.x  ) ) / 6.0; //-- H
		vColHSL.y = vColHSL.y / llList2Float( [2.0 * vColHSL.z, 2.0 - 2.0 * vColHSL.z], vColHSL.z > 0.5 );       //-- S
	}else{
		vColHSL.x = vColHSL.y; //-- Greyscale
	}
	return vColHSL;
}
/*//--                       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 RGB validity. See Articles below
  • HSL is kept percentage based here for best compatibility with image mapping
  • HSL color vectors may not be set directly

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.