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

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

Description

Converts the components of a color, as specified by the default RGB model, to an equivalent set of values for hue, saturation, and lightness that are the three components of the HSL model.

License

Open Source License

Parameter

Parameter Description
r the red component of the color
g the green component of the color
b the blue component of the color

Return

an array of three elements containing the hue, saturation, and lightness (in that order), of the color with the indicated red, green, and blue components.

Declaration

public static float[] convertRGBtoHSL(int r, int g, int b) 

Method Source Code

//package com.java2s;
/**//from w  w  w . ja va2  s  .  c  o  m
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

public class Main {
    /**
     * Converts the components of a color, as specified by the default RGB model, to an equivalent set of values for hue,
     * saturation, and lightness that are the three components of the HSL model.
     *
     * @param r the red component of the color
     * @param g the green component of the color
     * @param b the blue component of the color
     * @return an array of three elements containing the hue, saturation, and lightness (in that order), of the color with the
     *         indicated red, green, and blue components.
     */
    public static float[] convertRGBtoHSL(int r, int g, int b) {
        float varR = (r / 255f);
        float varG = (g / 255f);
        float varB = (b / 255f);

        float varMin = Math.min(varR, Math.min(varG, varB)); // Min value of RGB
        float varMax = Math.max(varR, Math.max(varG, varB)); // Max value of RGB
        float delMax = varMax - varMin; // Delta RGB value

        float h = 0;
        float s = 0;
        float l = (varMax + varMin) / 2;

        if (delMax == 0 || l == 0) {
            s = 0;
        } else if (l == 1) {
            s = 1;
        } else if (l <= 0.5) {
            s = delMax / (2 * (1 - l));
        } else if (l > 0.5) {
            s = delMax / (2 * l);
        }

        if (delMax == 0) {
            h = 0;
        } else if (varMax == varR && g >= b) {
            h = 60 * (varG - varB) / delMax + 0;
        } else if (varMax == varR && varG < b) {
            h = 60 * (varG - varB) / delMax + 360;
        } else if (varMax == varG) {
            h = 60 * (varB - varR) / delMax + 120;
        } else if (varMax == varB) {
            h = 60 * (varR - varG) / delMax + 240;
        }

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

Related

  1. convertRgb888To565(int rgb888)
  2. convertRgbaToArgb(int rgba)
  3. convertRgbToByteArray(int[] rgb)
  4. convertRGBtoHSV(float[] rgb, float[] hsv)
  5. convertRGBtoYIQ(float[] rgb, float[] yiq)
  6. rgb24FloatArray(float[] array, int rgb24)
  7. rgb2arr(final int color, final int[] argbarr)