// Based on Foley and van Dam algorithm. void ConvertHSLToRGB (const CGFloat *hslComponents, CGFloat *rgbComponents) { CGFloat hue = hslComponents[0]; CGFloat saturation = hslComponents[1]; CGFloat lightness = hslComponents[2]; CGFloat temp1, temp2; CGFloat rgb[3]; // "temp3" if (saturation == 0) { // Like totally gray man. rgb[0] = rgb[1] = rgb[2] = lightness; } else { if (lightness < 0.5) temp2 = lightness * (1.0 + saturation); else temp2 = (lightness + saturation) - (lightness * saturation); temp1 = (lightness * 2.0) - temp2; // Convert hue to 0..1 hue /= 360.0; // Use the rgb array as workspace for our "temp3"s rgb[0] = hue + (1.0 / 3.0); rgb[1] = hue; rgb[2] = hue - (1.0 / 3.0); // Magic for (int i = 0; i < 3; i++) { if (rgb[i] < 0.0) rgb[i] += 1.0; else if (rgb[i] > 1.0) rgb[i] -= 1.0; if (6.0 * rgb[i] < 1.0) rgb[i] = temp1 + ((temp2 - temp1) * 6.0 * rgb[i]); else if (2.0 * rgb[i] < 1.0) rgb[i] = temp2; else if (3.0 * rgb[i] < 2.0) rgb[i] = temp1 + ((temp2 - temp1) * ((2.0 / 3.0) - rgb[i]) * 6.0); else rgb[i] = temp1; } } // Clamp to 0..1 and put into the return pile. for (int i = 0; i < 3; i++) { rgbComponents[i] = MAX (0.0, MIN (1.0, rgb[i])); } } // ConvertHSLToRGB