uRGB2HSL

From Second Life Wiki
Jump to: navigation, search

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.