Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: seven plus zero?
(Example: nine)

The Note You're Voting On

hofstadler dot andi at gmx dot at
16 years ago
I have optimized the rgb2hsl function from slepichev a bit, so that it is a bit shorter and hopefully a bit faster:

<?php
/**
* Convert RGB colors array into HSL array
*
* @param array $ RGB colors set, each color component with range 0 to 255
* @return array HSL set, each color component with range 0 to 1
*/
function rgb2hsl($rgb){
$clrR = ($rgb[0]);
$clrG = ($rgb[1]);
$clrB = ($rgb[2]);

$clrMin = min($clrR, $clrG, $clrB);
$clrMax = max($clrR, $clrG, $clrB);
$deltaMax = $clrMax - $clrMin;

$L = ($clrMax + $clrMin) / 510;

if (
0 == $deltaMax){
$H = 0;
$S = 0;
}
else{
if (
0.5 > $L){
$S = $deltaMax / ($clrMax + $clrMin);
}
else{
$S = $deltaMax / (510 - $clrMax - $clrMin);
}

if (
$clrMax == $clrR) {
$H = ($clrG - $clrB) / (6.0 * $deltaMax);
}
else if (
$clrMax == $clrG) {
$H = 1/3 + ($clrB - $clrR) / (6.0 * $deltaMax);
}
else {
$H = 2 / 3 + ($clrR - $clrG) / (6.0 * $deltaMax);
}

if (
0 > $H) $H += 1;
if (
1 < $H) $H -= 1;
}
return array(
$H, $S,$L);
}
?>

<< Back to user notes page

To Top