Java HSL Color Convert hsl2hex(int[] hsl)

Here you can find the source of hsl2hex(int[] hsl)

Description

hslhex

License

Apache License

Declaration

static String hsl2hex(int[] hsl) 

Method Source Code

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

public class Main {
    static String hsl2hex(int[] hsl) {
        return rgb2hex(hsl2rgb(hsl));
    }//from www  . j  a v  a  2  s .com

    static String hsl2hex(int h, int s, int l) {
        return hsl2hex(new int[] { h, s, l });
    }

    static String rgb2hex(int[] rgb) {
        return toHex(rgb[0]) + toHex(rgb[1]) + toHex(rgb[2]);
    }

    static String rgb2hex(int r, int g, int b) {
        return rgb2hex(new int[] { r, g, b });
    }

    static int[] hsl2rgb(int[] hsl) {
        double h = hsl[0] / 360d;
        double s = hsl[1] / 100d;
        double l = hsl[2] / 100d;
        double r = 0d;
        double g = 0d;
        double b;

        if (s > 0d) {
            if (h >= 1d) {
                h = 0d;
            }

            h = h * 6d;
            double f = h - Math.floor(h);
            double a = Math.round(l * 255d * (1d - s));
            b = Math.round(l * 255d * (1d - (s * f)));
            double c = Math.round(l * 255d * (1d - (s * (1d - f))));
            l = Math.round(l * 255d);

            switch ((int) Math.floor(h)) {
            case 0:
                r = l;
                g = c;
                b = a;
                break;
            case 1:
                r = b;
                g = l;
                b = a;
                break;
            case 2:
                r = a;
                g = l;
                b = c;
                break;
            case 3:
                r = a;
                g = b;
                b = l;
                break;
            case 4:
                r = c;
                g = a;
                b = l;
                break;
            case 5:
                r = l;
                g = a;
                break;
            }
            return new int[] { (int) Math.round(r), (int) Math.round(g), (int) Math.round(b) };
        }

        l = Math.round(l * 255d);
        return new int[] { (int) l, (int) l, (int) l };
    }

    static String toHex(int v) {
        v = Math.min(Math.max(v, 0), 255);
        return String.valueOf("0123456789abcdef".charAt(((v - v % 16) / 16))) + //$NON-NLS-1$
                String.valueOf("0123456789abcdef".charAt(v % 16)); //$NON-NLS-1$
    }
}

Related

  1. findHue(Color color)
  2. hsl2rgb(double h, double s, double l)
  3. hsl2rgb(float h, float s, float l)
  4. hsl2rgb(int[] hsl)
  5. hsla_hue(double h, double m1, double m2)