Java HSL Color Convert hslToHsb(float[] inputHsl)

Here you can find the source of hslToHsb(float[] inputHsl)

Description

Convert a color in HSL color space to one in HSB color space.

License

Apache License

Parameter

Parameter Description
inputHsl HSL color in a array of three floats, 0 is Hue, 1 is Saturation and 2 is Lightness

Return

HSB color in array of three floats, 0 is Hue, 1 is Saturation and 2 is Brightness

Declaration

static float[] hslToHsb(float[] inputHsl) 

Method Source Code

//package com.java2s;
/*//from   w  w  w.j a va  2  s.  c  om
 * Copyright 2010 Google Inc.
 *
 * 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 {
    /** Index of Hue in HSB and HSL array. */
    static final int H = 0;
    /** Index of Saturation in HSB and HSL array. */
    static final int S = 1;
    /** Index of Lightness in HSL array. */
    static final int L = 2;

    /**
     * Convert a color in HSL color space to one in HSB color space.
     *
     * @param inputHsl HSL color in a array of three floats, 0 is Hue, 1 is
     *     Saturation and 2 is Lightness
     * @return HSB color in array of three floats, 0 is Hue, 1 is
     *     Saturation and 2 is Brightness
     */
    static float[] hslToHsb(float[] inputHsl) {
        float hHsl = inputHsl[H];
        float sHsl = inputHsl[S];
        float lHsl = inputHsl[L];

        float hHsb = hHsl;
        float bHsb = (2 * lHsl + sHsl * (1 - Math.abs(2 * lHsl - 1))) / 2;
        float sHsb = 2 * (bHsb - lHsl) / bHsb;

        float[] hsb = { hHsb, sHsb, bHsb };

        return hsb;
    }
}

Related

  1. hsl2hex(int[] hsl)
  2. hsl2rgb(double h, double s, double l)
  3. hsl2rgb(float h, float s, float l)
  4. hsl2rgb(int[] hsl)
  5. hsla_hue(double h, double m1, double m2)
  6. hslToRgb(double h, double s, double l)
  7. HSLtoRGB(double h, double s, double l)
  8. hslToRgb(double hue, double sat, double lum)
  9. HSLtoRGB(float h, float s, float l)