Java HSL Color Convert HSLtoRGB(float h, float s, float l)

Here you can find the source of HSLtoRGB(float h, float s, float l)

Description

HS Lto RGB

License

Open Source License

Declaration

private static int HSLtoRGB(float h, float s, float l) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2001-2006 Mathew A. Nelson and Robocode contributors
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.robocode.net/license/CPLv1.0.html
 * //from w w w. j  a  v  a2  s . co m
 * Contributors:
 *     Flemming N. Larsen
 *     - Initial implementation
 *******************************************************************************/

public class Main {
    private static int HSLtoRGB(float h, float s, float l) {
        float m2 = (l <= 0.5f) ? (l * (s + 1)) : (l + s - l * s);
        float m1 = 2 * l - m2;

        int r = (int) (255 * HUEtoRGB(m1, m2, h + (1f / 3)));
        int g = (int) (255 * HUEtoRGB(m1, m2, h));
        int b = (int) (255 * HUEtoRGB(m1, m2, h - (1f / 3)));

        return (((r << 8) | g) << 8) | b;
    }

    private static float HUEtoRGB(float m1, float m2, float h) {
        if (h < 0) {
            h++;
        }
        if (h > 1) {
            h--;
        }
        if ((h * 6) < 1) {
            return m1 + (m2 - m1) * h * 6;
        }
        if ((h * 2) < 1) {
            return m2;
        }
        if ((h * 3) < 2) {
            return m1 + (m2 - m1) * ((2f / 3) - h) * 6;
        }
        return m1;
    }
}

Related

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