Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Color; public class Main { /** * Convert HSL values to a RGB Color with a default alpha value of 1. * * <li>H (Hue) is specified as degrees in the range 0 - 360.</li> * <li>S (Saturation) is specified as a percentage in the range 0 - 100.</li> * <li>L (Lumanance) is specified as a percentage in the range 0 - 100.</li> * * @param hsl an array containing the 3 HSL values * @return color int in rgb value */ public static int hslToColor(int[] hsl) { return hslToRGB(hsl[0], hsl[1], hsl[2]); } /** * Convert HSL values to a RGB Color with a default alpha value of 1. * * <li>H (Hue) is specified as degrees in the range 0 - 360.</li> * <li>S (Saturation) is specified as a percentage in the range 0 - 100.</li> * <li>L (Lumanance) is specified as a percentage in the range 0 - 100.</li> * * @param h Hue * @param s Saturation * @param l Lumanance * @return color int in rgb value */ public static int hslToColor(int h, int s, int l) { return hslToRGB(h, s, l); } /** * Convert HSL values to a RGB Color. * * @param h Hue is specified as degrees in the range 0 - 360. * @param s Saturation is specified as a percentage in the range 1 - 100. * @param l Lumanance is specified as a percentage in the range 1 - 100. */ private static int hslToRGB(int hInt, int sInt, int lInt) { float h, s, l; h = (float) hInt; s = (float) sInt; l = (float) lInt; if (s < 0.0f || s > 100.0f) { String message = "Color parameter outside of expected range - Saturation"; throw new IllegalArgumentException(message); } if (l < 0.0f || l > 100.0f) { String message = "Color parameter outside of expected range - Luminance"; throw new IllegalArgumentException(message); } // Formula needs all values between 0 - 1. h = h % 360.0f; h /= 360f; s /= 100f; l /= 100f; float q = 0; if (l < 0.5) q = l * (1 + s); else q = (l + s) - (s * l); float p = 2 * l - q; int r = (int) (Math.max(0, HUEtoRGB(p, q, h + (1.0f / 3.0f))) * 255); int g = (int) (Math.max(0, HUEtoRGB(p, q, h)) * 255); int b = (int) (Math.max(0, HUEtoRGB(p, q, h - (1.0f / 3.0f))) * 255); return Color.rgb(r, g, b); } private static float HUEtoRGB(float p, float q, float h) { if (h < 0) h += 1; if (h > 1) h -= 1; if (6 * h < 1) { return p + ((q - p) * 6 * h); } if (2 * h < 1) { return q; } if (3 * h < 2) { return p + ((q - p) * 6 * ((2.0f / 3.0f) - h)); } return p; } }