/** * Convert RGB values to HSL (hue/saturation/lightness) * * @param rgb range 255 255 255 * @param doRound set to false when just using this for for RGB -- HSL -- HSL' -- RGB' conversion * @return the HSL as P3 range 360 100 100 * @author hansonr */ public static P3 rgbToHSL(P3 rgb, boolean doRound) { // adapted from http://tips4java.wordpress.com/2009/07/05/hsl-color/ // see http://en.wikipedia.org/wiki/HSL_color_space float r = rgb.x / 255; float g = rgb.y / 255; float b = rgb.z / 255; float min = Math.min(r, Math.min(g, b)); float max = Math.max(r, Math.max(g, b)); // lightness is just p * 50 float p = (max + min); float q = (max - min); float h = (60 * ((q == 0 ? 0 : max == r ? ((g - b) / q + 6) : max == g ? (b - r) / q + 2 : (r - g) / q + 4))) % 360; float s = q / (q == 0 ? 1 : p <= 1 ? p : 2 - p); // we round to tenths for HSL so that we can return enough // precision to get back 1-255 in RGB return (doRound ? P3.new3(Math.round(h * 10) / 10f, Math.round(s * 1000) / 10f, Math.round(p * 500) / 10f) : P3.new3(h, s * 100, p * 50)); }
/** * Convert HSL (hue/saturation/luninance) values to RGB * * @param hsl in the range 360, 100, 100 * @return the RGB as P3 range 0 to 255 * @author hansonr */ public static P3 hslToRGB(P3 hsl) { // adapted from http://tips4java.wordpress.com/2009/07/05/hsl-color/ // see http://en.wikipedia.org/wiki/HSL_color_space // highly condensed float h = Math.max(0, Math.min(360, hsl.x)) / 60; float s = Math.max(0, Math.min(100, hsl.y)) / 100; float l = Math.max(0, Math.min(100, hsl.z)) / 100; float p = l - (l < 0.5 ? l : 1 - l) * s; float q = 2 * (l - p); float r = toRGB(p, q, h + 2); float g = toRGB(p, q, h); float b = toRGB(p, q, h - 2); return P3.new3(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)); }