Java RGB Color Convert To rgb2hsl(int colour)

Here you can find the source of rgb2hsl(int colour)

Description

rgbhsl

License

Open Source License

Declaration

public static double[] rgb2hsl(int colour) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static double[] rgb2hsl(int r, int g, int b) {
        double dr = r / 255.0;
        double dg = g / 255.0;
        double db = b / 255.0;
        double max = Math.max(dr, Math.max(dg, db));
        double min = Math.min(dr, Math.min(dg, db));

        double h, s;
        double l = (max + min) * 0.5;

        if (max == min) {
            h = 0;//www.j av  a2 s  . co  m
            s = 0;
        } else {
            double d = max - min;
            s = l > 0.5 ? d / (2.0 - max - min) : d / (max + min);

            if (max == dr) {
                h = (dg - db) / d + (dg < db ? 6 : 0);
            } else if (max == dg) {
                h = (db - dr) / d + 2;
            } else {
                h = (dr - dg) / d + 4;
            }
            h /= 6.0;
        }

        return new double[] { h, s, l };
    }

    public static double[] rgb2hsl(int colour) {
        return rgb2hsl(red(colour), green(colour), blue(colour));
    }

    public static int red(int c) {
        return (c >> 16) & 0xFF;
    }

    public static int green(int c) {
        return (c >> 8) & 0xFF;
    }

    public static int blue(int c) {
        return (c) & 0xFF;
    }
}

Related

  1. rgb2gray(int r, int g, int b)
  2. rgb2grayscale(int[] rgb, int imageWidth, int imageHeight)
  3. rgb2grayscaleA(int[] rgb)
  4. rgb2Hex(int rgb)
  5. rgb2hex(int[] rgb)
  6. RGB2HSL(int red, int green, int blue, float[] hslvals)
  7. rgb2hsl(int[] rgb)
  8. rgb2hsv(int r, int g, int b)
  9. rgb2int(final int[] color)