Java HSL Color Convert HSLtoRGB(float hue, float sat, float lum)

Here you can find the source of HSLtoRGB(float hue, float sat, float lum)

Description

all in [0,1] !

License

Apache License

Parameter

Parameter Description
hue a parameter
sat a parameter
lum a parameter

Declaration

public static int HSLtoRGB(float hue, float sat, float lum) 

Method Source Code

//package com.java2s;
/**/* w  w w  .j av  a  2s.c o m*/
 * 
 *    Copyright 2017 Florian Erhard
 *
 *   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 {
    /**
     * all in [0,1] !
     * @param hue
     * @param sat
     * @param lum
     * @return
     */
    public static int HSLtoRGB(float hue, float sat, float lum) {
        float v;
        float red, green, blue;
        float m;
        float sv;
        int sextant;
        float fract, vsf, mid1, mid2;

        red = lum; // default to gray
        green = lum;
        blue = lum;
        v = (lum <= 0.5f) ? (lum * (1.0f + sat)) : (lum + sat - lum * sat);
        m = lum + lum - v;
        sv = (v - m) / v;
        hue *= 6; //get into range 0..6
        sextant = (int) Math.floor(hue); // int32 rounds up or down.
        fract = hue - sextant;
        vsf = v * sv * fract;
        mid1 = m + vsf;
        mid2 = v - vsf;

        if (v > 0) {
            switch (sextant) {
            case 0:
                red = v;
                green = mid1;
                blue = m;
                break;
            case 1:
                red = mid2;
                green = v;
                blue = m;
                break;
            case 2:
                red = m;
                green = v;
                blue = mid1;
                break;
            case 3:
                red = m;
                green = mid2;
                blue = v;
                break;
            case 4:
                red = mid1;
                green = m;
                blue = v;
                break;
            case 5:
                red = v;
                green = m;
                blue = mid2;
                break;
            }
        }
        int r = (int) (red * 255);
        int g = (int) (green * 255);
        int b = (int) (blue * 255);
        return 0xff000000 | (r << 16) | (g << 8) | (b << 0);
    }
}

Related

  1. HSLtoRGB(double h, double s, double l)
  2. hslToRgb(double h, double s, double l)
  3. hslToRgb(double hue, double sat, double lum)
  4. HSLtoRGB(float h, float s, float l)
  5. HslToRgb(float h, float s, float l, float a)
  6. hslToRgb(float[] hsl)
  7. HSLtoRGB(float[] hsl)
  8. hslToRGB(int h, float s, float l)
  9. hueFromColor(Color c)