Java HSV Color to HSB hsvToRGB(float hue, float saturation, float value)

Here you can find the source of hsvToRGB(float hue, float saturation, float value)

Description

hsv To RGB

License

LGPL

Declaration

public static int hsvToRGB(float hue, float saturation, float value) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static int hsvToRGB(float hue, float saturation, float value) {
        int i = (int) (hue * 6.0F) % 6;
        float f = hue * 6.0F - (float) i;
        float f1 = value * (1.0F - saturation);
        float f2 = value * (1.0F - f * saturation);
        float f3 = value * (1.0F - (1.0F - f) * saturation);
        float f4;
        float f5;
        float f6;

        switch (i) {
        case 0:/*w  w  w  . ja v a 2s  .  com*/
            f4 = value;
            f5 = f3;
            f6 = f1;
            break;

        case 1:
            f4 = f2;
            f5 = value;
            f6 = f1;
            break;

        case 2:
            f4 = f1;
            f5 = value;
            f6 = f3;
            break;

        case 3:
            f4 = f1;
            f5 = f2;
            f6 = value;
            break;

        case 4:
            f4 = f3;
            f5 = f1;
            f6 = value;
            break;

        case 5:
            f4 = value;
            f5 = f1;
            f6 = f2;
            break;

        default:
            throw new RuntimeException("Something went wrong when converting from HSV to RGB. Input was " + hue
                    + ", " + saturation + ", " + value);
        }

        int j = clamp((int) (f4 * 255.0F), 0, 255);
        int k = clamp((int) (f5 * 255.0F), 0, 255);
        int l = clamp((int) (f6 * 255.0F), 0, 255);
        return j << 16 | k << 8 | l;
    }

    /**
     * Returns the value of the first parameter, clamped to be within the lower and upper limits given by the second and
     * third parameters.
     */
    public static int clamp(int num, int min, int max) {
        if (num < min) {
            return min;
        } else {
            return num > max ? max : num;
        }
    }

    /**
     * Returns the value of the first parameter, clamped to be within the lower and upper limits given by the second and
     * third parameters
     */
    public static float clamp(float num, float min, float max) {
        if (num < min) {
            return min;
        } else {
            return num > max ? max : num;
        }
    }

    public static double clamp(double num, double min, double max) {
        if (num < min) {
            return min;
        } else {
            return num > max ? max : num;
        }
    }
}

Related

  1. HSVtoRGB(double hue, double saturation, double value)
  2. hsvToRgb(double[] hsv)
  3. HSVtoRGB(float h, float s, float v)
  4. HSVtoRGB(float h, float s, float v, float[] result)
  5. hsvToRgb(float h, float s, float v, int[] rgb)
  6. hsvToRgb(float hue, float saturation, float value)
  7. hsvToRGB(float p_181758_0_, float p_181758_1_, float p_181758_2_)
  8. HSVToRGB(float... hsv)
  9. HSVtoRGB(float[] hsv, float[] rgb)