Hsl Color To Rgb Color - Android Graphics

Android examples for Graphics:Color RGB Value

Description

Hsl Color To Rgb Color

Demo Code


//package com.java2s;
import android.graphics.Color;

public class Main {
    public static int HslToRgb(float h, float s, float l) {
        int r;/*w  w  w .  java2s .c o m*/
        int g;
        int b;
        if (s == 0) {
            b = (int) (255 * l);
            g = b;
            r = b;
        } else {
            float q;
            if (l < 0.5)
                q = l * (1 + s);
            else
                q = (l + s) - l * s;
            float p = 2 * l - q;
            float f = h / 360;
            double T[] = new double[] { 0.3333333F + f, f, f - 0.3333333F };
            for (int i = 0; i < 3; i++) {
                if (T[i] < 0)
                    T[i] = 1 + T[i];
                else if (T[i] > 1)
                    T[i] = T[i] - 1;
                if (6 * T[i] < 1) {
                    T[i] = p + 6 * (q - p) * T[i];
                    continue;
                }
                if (2 * T[i] < 1) {
                    T[i] = q;
                    continue;
                }
                if (3 * T[i] < 2)
                    T[i] = p + 6 * ((q - p) * (0.6666667F - T[i]));
                else
                    T[i] = p;
            }

            r = (int) (255 * T[0]);
            g = (int) (255 * T[1]);
            b = (int) (255 * T[2]);
        }
        return Color.rgb(r, g, b);
    }

    public static int HslToRgb(float hsl[]) {
        return HslToRgb(hsl[0], hsl[1], hsl[2]);
    }
}

Related Tutorials