Converts SAC HSLA (or HSL) LexicalUnit value to RGBA - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Converts SAC HSLA (or HSL) LexicalUnit value to RGBA

Demo Code


//package com.java2s;

public class Main {
    /**/*from   w  w w.ja  va2  s .  c  o m*/
     * Converts SAC HSLA (or HSL) LexicalUnit value to RGBA
     * @param value
     * @return
     */
    public static String RGBAFromHSLA(float h, float s, float l, float a) {
        int r, g, b;
        float m2;
        if (l <= 0.5)
            m2 = l * (s + 1);
        else
            m2 = l + s - l * s;

        float m1 = l * 2 - m2;
        r = (int) (HUEToRGB(m1, m2, h + 1 / 3F) * 255);
        g = (int) (HUEToRGB(m1, m2, h) * 255);
        b = (int) (HUEToRGB(m1, m2, h - 1 / 3F) * 255);

        return String.format("rgba(%s, %s, %s, %s)", r, g, b, a);

    }

    private static float HUEToRGB(float m1, float m2, float h) {
        if (h < 0)
            h++;
        if (h > 1)
            h--;
        if (h * 6 < 1)
            return m1 + (m2 - m1) * h * 6;
        if (h * 2 < 1)
            return m2;
        if (h * 3 < 2)
            return m1 + (m2 - m1) * (2 / 3F - h) * 6;
        return m1;
    }
}

Related Tutorials