Java RGB Color Convert To RGBtoHSL(int r, int g, int b, float[] hsl)

Here you can find the source of RGBtoHSL(int r, int g, int b, float[] hsl)

Description

RG Bto HSL

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/*/*from w w  w.j a va 2 s  .  c  o m*/
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    static void RGBtoHSL(int r, int g, int b, float[] hsl) {
        final float rf = r / 255f;
        final float gf = g / 255f;
        final float bf = b / 255f;
        final float max = Math.max(rf, Math.max(gf, bf));
        final float min = Math.min(rf, Math.min(gf, bf));
        final float deltaMaxMin = max - min;
        float h, s;
        float l = (max + min) / 2f;
        if (max == min) {
            // Monochromatic
            h = s = 0f;
        } else {
            if (max == rf) {
                h = ((gf - bf) / deltaMaxMin) % 6f;
            } else if (max == gf) {
                h = ((bf - rf) / deltaMaxMin) + 2f;
            } else {
                h = ((rf - gf) / deltaMaxMin) + 4f;
            }
            s = deltaMaxMin / (1f - Math.abs(2f * l - 1f));
        }
        hsl[0] = (h * 60f) % 360f;
        hsl[1] = s;
        hsl[2] = l;
    }
}

Related

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