Java HSV Color to HSB HSVtoRGB(float h, float s, float v)

Here you can find the source of HSVtoRGB(float h, float s, float v)

Description

HS Vto RGB

License

LGPL

Declaration

public static float[] HSVtoRGB(float h, float s, float v) 

Method Source Code

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

public class Main {
    public static float[] HSVtoRGB(float h, float s, float v) {
        // H is given on [0->6] or -1. S and V are given on [0->1].
        // RGB are each returned on [0->1].
        float m, n, f;
        int i;/* w  w  w  .jav a2s. co m*/

        float[] hsv = new float[3];
        float[] rgb = new float[3];

        hsv[0] = h;
        hsv[1] = s;
        hsv[2] = v;

        if (hsv[0] == -1) {
            rgb[0] = rgb[1] = rgb[2] = hsv[2];
            return rgb;
        }
        i = (int) (Math.floor(hsv[0]));
        f = hsv[0] - i;
        if (i % 2 == 0) {
            f = 1 - f; // if i is even
        }
        m = hsv[2] * (1 - hsv[1]);
        n = hsv[2] * (1 - hsv[1] * f);
        switch (i) {
        case 6:
        case 0:
            rgb[0] = hsv[2];
            rgb[1] = n;
            rgb[2] = m;
            break;
        case 1:
            rgb[0] = n;
            rgb[1] = hsv[2];
            rgb[2] = m;
            break;
        case 2:
            rgb[0] = m;
            rgb[1] = hsv[2];
            rgb[2] = n;
            break;
        case 3:
            rgb[0] = m;
            rgb[1] = n;
            rgb[2] = hsv[2];
            break;
        case 4:
            rgb[0] = n;
            rgb[1] = m;
            rgb[2] = hsv[2];
            break;
        case 5:
            rgb[0] = hsv[2];
            rgb[1] = m;
            rgb[2] = n;
            break;
        }

        return rgb;

    }
}

Related

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