Converts colors from the HSL color space to the RGB color space. I copied and converted this from a toolkit for C#. Judging from my tests, it works well.
PHP Code:
function HSLtoRGB(H, S, L) {
temp.R = 0;
temp.G = 0;
temp.B = 0;
H = H > 1 ? 1 : (H < 0 ? 0 : H);
S = S > 1 ? 1 : (S < 0 ? 0 : S);
L = L > 1 ? 1 : (L < 0 ? 0 : L);
if (L == 0) {
R = G = B = 0;
} else {
if (S == 0) {
R = G = B = L;
} else {
temp.v1 = L <= 0.5 ? L * (1 + S) : L + S - (L * S);
temp.v2 = 2 * L - temp.v1;
temp.t3 = { H + 1 / 3, H, H - 1 / 3 };
temp.clr = new[3];
for (temp.i = 0; i < 3; i++) {
if (temp.t3[i] < 0)
temp.t3[i]++;
if (temp.t3[i] > 1)
temp.t3[i]--;
if (temp.t3[i] * 6 < 1)
temp.clr[i] = temp.v2 + (temp.v1 - temp.v2)
* temp.t3[i] * 6;
else if (temp.t3[i] * 2 < 1)
temp.clr[i] = temp.v1;
else if (temp.t3[i] * 3 < 2)
temp.clr[i] = temp.v2 + (temp.v1 - temp.v2)
* (2 / 3 - temp.t3[i]) * 6;
else
temp.clr[i] = temp.v2;
}
}
}
R = temp.clr[0];
G = temp.clr[1];
B = temp.clr[2];
return { R, G, B };
}
Example usage:
PHP Code:
temp.rgb = HSLtoRGB(0.01, 0.7, 0.5); // slightly desaturated medium red
echo(temp.rgb); // echoes "0.85,0.192,0.15"
H, S and L have to be values between 0 and 1. The returned RGB values are also between 0 and 1.
Enjoy!