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

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

Description

http://www.cs.rit.edu/~ncs/color/t_convert.html

License

Apache License

Parameter

Parameter Description
h a parameter
s a parameter
v a parameter

Declaration

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

Method Source Code

//package com.java2s;
/*/*from   w  w w. j  ava2 s  .  com*/
 * Copyright 2011 Mark McKay
 * 
 * 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 {
    /**
     * http://www.cs.rit.edu/~ncs/color/t_convert.html
     * 
     * @param h
     * @param s
     * @param v
     * @return
     */
    public static float[] HSVtoRGB(float h, float s, float v, float[] result) {
        if (result == null) {
            result = new float[3];
        }

        float r, g, b;
        int i;
        float f, p, q, t;

        if (s == 0) {
            // achromatic (grey)
            r = g = b = v;

            result[0] = r;
            result[1] = g;
            result[2] = b;
            return result;
        }

        //        h /= 60;         // sector 0 to 5
        h *= 6; // sector 0 to 5
        i = (int) Math.floor(h);
        f = h - i; // factorial part of h
        p = v * (1 - s);
        q = v * (1 - s * f);
        t = v * (1 - s * (1 - f));

        switch (i) {
        case 0:
            r = v;
            g = t;
            b = p;
            break;
        case 1:
            r = q;
            g = v;
            b = p;
            break;
        case 2:
            r = p;
            g = v;
            b = t;
            break;
        case 3:
            r = p;
            g = q;
            b = v;
            break;
        case 4:
            r = t;
            g = p;
            b = v;
            break;
        default: // case 5:
            r = v;
            g = p;
            b = q;
            break;
        }

        result[0] = r;
        result[1] = g;
        result[2] = b;
        return result;
    }
}

Related

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