Java RGB Color Convert To RGB2HSL(int red, int green, int blue, float[] hslvals)

Here you can find the source of RGB2HSL(int red, int green, int blue, float[] hslvals)

Description

RGBHSL

License

Open Source License

Declaration

public static void RGB2HSL(int red, int green, int blue, float[] hslvals) 

Method Source Code

//package com.java2s;
/*/*from w  ww  .j  a v  a 2  s  .  c  o  m*/
 * Copyright (C) 2007, 2008 Quadduc <quadduc@gmail.com>
 * Copyright (C) 2007, 2011 IsmAvatar <IsmAvatar@gmail.com>
 * Copyright (C) 2007 Clam <clamisgood@gmail.com>
 * Copyright (C) 2013, 2014 Robert B. Colton
 *
 * This file is part of LateralGM.
 * LateralGM is free software and comes with ABSOLUTELY NO WARRANTY.
 * See LICENSE for details.
 */

public class Main {
    public static void RGB2HSL(int red, int green, int blue, float[] hslvals) {

        float r = red / 255.f;
        float g = green / 255.f;
        float b = blue / 255.f;
        float max = Math.max(Math.max(r, g), b);
        float min = Math.min(Math.min(r, g), b);
        float c = max - min;

        float h_ = 0.f;
        if (c == 0) {
            h_ = 0;
        } else if (max == r) {
            h_ = (float) (g - b) / c;
            if (h_ < 0)
                h_ += 6.f;
        } else if (max == g) {
            h_ = (float) (b - r) / c + 2.f;
        } else if (max == b) {
            h_ = (float) (r - g) / c + 4.f;
        }
        float h = 60.f * h_;

        float l = (max + min) * 0.5f;

        float s;
        if (c == 0) {
            s = 0.f;
        } else {
            s = c / (1 - Math.abs(2.f * l - 1.f));
        }

        hslvals[0] = h;
        hslvals[1] = s;
        hslvals[2] = l;
    }
}

Related

  1. rgb2grayscale(int[] rgb, int imageWidth, int imageHeight)
  2. rgb2grayscaleA(int[] rgb)
  3. rgb2Hex(int rgb)
  4. rgb2hex(int[] rgb)
  5. rgb2hsl(int colour)
  6. rgb2hsl(int[] rgb)
  7. rgb2hsv(int r, int g, int b)
  8. rgb2int(final int[] color)
  9. rgb2intval(int r, int g, int b)