Java RGB Color Convert To RGBtoHSL(int r, int g, int b)

Here you can find the source of RGBtoHSL(int r, int g, int b)

Description

RG Bto HSL

License

Open Source License

Declaration

private static float[] RGBtoHSL(int r, int g, int b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2001-2006 Mathew A. Nelson and Robocode contributors
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.robocode.net/license/CPLv1.0.html
 * /*from   ww w .ja va 2 s .  c om*/
 * Contributors:
 *     Flemming N. Larsen
 *     - Initial implementation
 *******************************************************************************/

public class Main {
    private static float[] RGBtoHSL(int r, int g, int b) {
        float R = (float) r / 255;
        float G = (float) g / 255;
        float B = (float) b / 255;

        float min = Math.min(Math.min(R, G), B); // Min. value of RGB
        float max = Math.max(Math.max(R, G), B); // Max. value of RGB
        float delta = max - min; // Delta RGB value

        float L = (max + min) / 2;

        float H, S;

        if (delta == 0) { // This is a gray, no chroma...
            H = 0;
            S = 0;
        } else { // Chromatic data...
            if (L < 0.5f) {
                S = delta / (max + min);
            } else {
                S = delta / (2 - max - min);
            }

            float deltaR = (((max - R) / 6) + (delta / 2)) / delta;
            float deltaG = (((max - G) / 6) + (delta / 2)) / delta;
            float deltaB = (((max - B) / 6) + (delta / 2)) / delta;

            if (R == max) {
                H = deltaB - deltaG;
            } else if (G == max) {
                H = (1f / 3) + deltaR - deltaB;
            } else {
                H = (2f / 3) + deltaG - deltaR;
            }

            if (H < 0) {
                H++;
            }
            if (H > 1) {
                H--;
            }
        }
        return new float[] { H, S, L };
    }
}

Related

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