Java RGB Color Convert To RgbToHsl(float r, float g, float b, float a)

Here you can find the source of RgbToHsl(float r, float g, float b, float a)

Description

Rgb To Hsl

License

Apache License

Declaration

static float[] RgbToHsl(float r, float g, float b, float a) 

Method Source Code

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

public class Main {
    static float[] RgbToHsl(float r, float g, float b, float a) {
        float h = 0, s = 0, l;

        float max = Math.max(r, Math.max(g, b));
        float min = Math.min(r, Math.min(g, b));

        // hue//from   www .j a va  2 s. co m
        if (max == min) {
            h = 0; // undefined
        } else if (max == r && g >= b) {
            h = 1.0f / 6.0f * (g - b) / (max - min);
        } else if (max == r && g < b) {
            h = 1.0f / 6.0f * (g - b) / (max - min) + 1.0f;
        } else if (max == g) {
            h = 1.0f / 6.0f * (b - r) / (max - min) + 1.0f / 3.0f;
        } else if (max == b) {
            h = 1.0f / 6.0f * (r - g) / (max - min) + 2.0f / 3.0f;
        }

        // luminance
        l = (max + min) / 2.0f;

        // saturation
        if (l == 0 || max == min) {
            s = 0;
        } else if (0 < l && l <= 0.5) {
            s = (max - min) / (max + min);
        } else if (l > 0.5) {
            s = (max - min) / (2 - (max + min)); //(max-min > 0)?
        }

        float[] result = new float[4];
        result[0] = h;
        result[1] = s;
        result[2] = l;
        result[3] = a;

        return result;
    }
}

Related

  1. rgbToHex(String color)
  2. RGBtoHSB(int r, int g, int b, float hsbvals[])
  3. RGBtoHSB(int r, int g, int b, float[] hsbvals)
  4. RGBtoHSB(int red, int green, int blue, float hsb[])
  5. rgbToHsl(byte red, byte green, byte blue)
  6. rgbToHsl(float[] rgb)
  7. RGBtoHSL(int r, int g, int b)
  8. RGBtoHSL(int r, int g, int b)
  9. RGBtoHSL(int r, int g, int b, float[] hsl)