Java HSL Color Convert HslToRgb(float h, float s, float l, float a)

Here you can find the source of HslToRgb(float h, float s, float l, float a)

Description

Hsl To Rgb

License

Apache License

Declaration

static float[] HslToRgb(float h, float s, float l, float a) 

Method Source Code

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

public class Main {
    static float[] HslToRgb(float h, float s, float l, float a) {
        float[] result = new float[4];
        if (s == 0) {
            result[0] = l;/*from  www.java  2s .c  om*/
            result[1] = l;
            result[2] = l;
            result[3] = a;
        } else {
            float q = (l < 0.5f) ? (l * (1.0f + s)) : (l + s - (l * s));
            float p = (2.0f * l) - q;

            float[] T = new float[3];
            T[0] = h + (1.0f / 3.0f); // Tr
            T[1] = h; // Tb
            T[2] = h - (1.0f / 3.0f); // Tg

            for (int i = 0; i < 3; i++) {
                if (T[i] < 0)
                    T[i] += 1.0;

                if (T[i] > 1)
                    T[i] -= 1.0;

                if ((T[i] * 6) < 1) {
                    T[i] = p + ((q - p) * 6.0f * T[i]);
                } else if ((T[i] * 2.0f) < 1) {
                    T[i] = q;
                } else if ((T[i] * 3.0f) < 2) {
                    T[i] = p + (q - p) * ((2.0f / 3.0f) - T[i]) * 6.0f;
                } else
                    T[i] = p;
            }

            result[0] = T[0];
            result[1] = T[1];
            result[2] = T[2];
            result[3] = a;
        }

        return result;
    }
}

Related

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