Java HSL Color Convert HSLtoRGB(double h, double s, double l)

Here you can find the source of HSLtoRGB(double h, double s, double l)

Description

Converts HSL to RGB.

License

LGPL

Parameter

Parameter Description
h red value
s green value
l blue value

Return

Array containing RGB values in the format {r,g,b}

Declaration

public static int[] HSLtoRGB(double h, double s, double l) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**/*from   w  ww. j a v a  2s.c om*/
     * Converts HSL to RGB.
     * HSL values are in decimal form: <br>
     * H: 360 = 1, <br>
     * S: 100 = 1, <br>
     * L: 100 = 1. <br>
     * Thanks to http://serennu.com/colour/rgbtohsl.php for help on this calculation.
     *
     * @param h red value
     * @param s green value
     * @param l blue value
     * @return Array containing RGB values in the format {r,g,b}
     */
    public static int[] HSLtoRGB(double h, double s, double l) {
        int r;
        int g;
        int b;

        double var1;
        double var2;

        if (s == 0) {
            r = (int) (l * 255);
            g = (int) (l * 255);
            b = (int) (l * 255);
        } else {
            if (l < 0.5) {
                var2 = l * (1 + s);
            } else {
                var2 = (l + s) - (s * l);
            }

            var1 = 2 * l - var2;
            r = (int) (255 * HueToRGB(var1, var2, h + (1.0 / 3.0)));
            g = (int) (255 * HueToRGB(var1, var2, h));
            b = (int) (255 * HueToRGB(var1, var2, h - (1.0 / 3.0)));
        }

        int[] rgb = new int[3];
        rgb[0] = r;
        rgb[1] = g;
        rgb[2] = b;
        return rgb;
    }

    public static double HueToRGB(double v1, double v2, double vh) {
        if (vh < 0) {
            vh += 1;
        }
        if (vh > 1) {
            vh -= 1;
        }
        if ((6 * vh) < 1) {
            return (v1 + (v2 - v1) * 6 * vh);
        }
        if ((2 * vh) < 1) {
            return (v2);
        }
        if ((3 * vh) < 2) {
            return (v1 + (v2 - v1) * ((2.0 / 3.0 - vh) * 6));
        }
        return v1;
    }
}

Related

  1. hsl2rgb(double h, double s, double l)
  2. hsl2rgb(float h, float s, float l)
  3. hsl2rgb(int[] hsl)
  4. hsla_hue(double h, double m1, double m2)
  5. hslToHsb(float[] inputHsl)
  6. hslToRgb(double h, double s, double l)
  7. hslToRgb(double hue, double sat, double lum)
  8. HSLtoRGB(float h, float s, float l)
  9. HslToRgb(float h, float s, float l, float a)