Here you can find the source of HSLtoRGB(float[] hsl)
Parameter | Description |
---|---|
hsl | the hsl values. |
public static int HSLtoRGB(float[] hsl)
//package com.java2s; public class Main { /**//from w w w . j a v a 2 s . c om * Converts from HSL color space to RGB color. * * @param hsl the hsl values. * * @return the RGB color. */ public static int HSLtoRGB(float[] hsl) { float r, g, b, h, s, l; //this function works with floats between 0 and 1 float temp1, temp2, tempr, tempg, tempb; h = hsl[0]; s = hsl[1]; l = hsl[2]; // Then follows a trivial case: if the saturation is 0, the color will be a grayscale color, // and the calculation is then very simple: r, g and b are all set to the lightness. //If saturation is 0, the color is a shade of gray if (s == 0) { r = g = b = l; } // If the saturation is higher than 0, more calculations are needed again. // red, green and blue are calculated with the formulas defined in the code. // If saturation > 0, more complex calculations are needed else { //Set the temporary values if (l < 0.5) temp2 = l * (1 + s); else temp2 = (l + s) - (l * s); temp1 = 2 * l - temp2; tempr = h + 1.0f / 3.0f; if (tempr > 1) tempr--; tempg = h; tempb = h - 1.0f / 3.0f; if (tempb < 0) tempb++; //Red if (tempr < 1.0 / 6.0) r = temp1 + (temp2 - temp1) * 6.0f * tempr; else if (tempr < 0.5) r = temp2; else if (tempr < 2.0 / 3.0) r = temp1 + (temp2 - temp1) * ((2.0f / 3.0f) - tempr) * 6.0f; else r = temp1; //Green if (tempg < 1.0 / 6.0) g = temp1 + (temp2 - temp1) * 6.0f * tempg; else if (tempg < 0.5) g = temp2; else if (tempg < 2.0 / 3.0) g = temp1 + (temp2 - temp1) * ((2.0f / 3.0f) - tempg) * 6.0f; else g = temp1; //Blue if (tempb < 1.0 / 6.0) b = temp1 + (temp2 - temp1) * 6.0f * tempb; else if (tempb < 0.5) b = temp2; else if (tempb < 2.0 / 3.0) b = temp1 + (temp2 - temp1) * ((2.0f / 3.0f) - tempb) * 6.0f; else b = temp1; } // And finally, the results are returned as integers between 0 and 255. int result = 0; result += ((int) (r * 255) & 0xFF) << 16; result += ((int) (g * 255) & 0xFF) << 8; result += ((int) (b * 255) & 0xFF); return result; } }