Java RGB Color Create toRGB(float h, float s, float l)

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

Description

to RGB

License

Open Source License

Declaration

private static byte[] toRGB(float h, float s, float l) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static byte[] toRGB(float h, float s, float l) {
        h = h % 360.0f;/*from  w w  w  .j  a  v  a 2 s . com*/
        h /= 360f;
        s /= 100f;
        l /= 100f;

        float q = 0;

        if (l < 0.5)
            q = l * (1 + s);
        else
            q = (l + s) - (s * l);

        float p = 2 * l - q;

        float r = Math.max(0, HueToRGB(p, q, h + (1.0f / 3.0f)));
        float g = Math.max(0, HueToRGB(p, q, h));
        float b = Math.max(0, HueToRGB(p, q, h - (1.0f / 3.0f)));

        r = Math.min(r, 1.0f);
        g = Math.min(g, 1.0f);
        b = Math.min(b, 1.0f);

        byte r_byte = (byte) Math.round(r * 255);
        byte g_byte = (byte) Math.round(g * 255);
        byte b_byte = (byte) Math.round(b * 255);

        return new byte[] { r_byte, g_byte, b_byte };
    }

    private static float HueToRGB(float p, float q, float h) {
        if (h < 0)
            h += 1;

        if (h > 1)
            h -= 1;

        if (6 * h < 1) {
            return p + ((q - p) * 6 * h);
        }

        if (2 * h < 1) {
            return q;
        }

        if (3 * h < 2) {
            return p + ((q - p) * 6 * ((2.0f / 3.0f) - h));
        }

        return p;
    }
}

Related

  1. rgbDistance(int color1, int color2)
  2. rgbFromColorInt(int c)
  3. rgbFromGrayscale(int grayscale)
  4. toRGB(byte r, byte g, byte b, byte a)
  5. toRGB(final int r, final int g, final int b, final int a)
  6. toRGB(int alpha, int red, int green, int blue)
  7. toRGB(int color)
  8. toRgb(int r, int g, int b)
  9. toRGB(String hexString)