Java HSV Color to HSB HSVtoRGB(double hue, double saturation, double value)

Here you can find the source of HSVtoRGB(double hue, double saturation, double value)

Description

Method returns three integers representing the RGB values of a HSV input Values in input must be between 0 and 1 Values in output are between 0 and 255.

License

Apache License

Parameter

Parameter Description
hue a parameter
saturation a parameter
value a parameter

Declaration

public static int[] HSVtoRGB(double hue, double saturation, double value) 

Method Source Code

//package com.java2s;
/*//from ww w . jav a2  s . com
 * Copyright 2007 Robert Hanson <iamroberthanson AT gmail.com>
 * 
 * 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 {
    /**
     * Method returns three integers representing the RGB values of a HSV input
     * Values in input must be between 0 and 1
     * Values in output are between 0 and 255.
     * 
     * See http://www.easyrgb.com/math.php?MATH=M21#text21 for details of algorithm used.
     * 
     * @param hue
     * @param saturation
     * @param value
     * @return
     */
    public static int[] HSVtoRGB(double hue, double saturation, double value) {
        int R, G, B;
        //String sR="ff",sG="ff",sB="ff";
        if (saturation == 0) //HSV values = 0 / 1
        {
            R = new Double(value * 255).intValue();
            G = new Double(value * 255).intValue();
            B = new Double(value * 255).intValue();
        } else {
            double var_h = hue * 6;
            if (var_h == 6)
                var_h = 0; //H must be < 1
            int var_i = new Double(Math.floor(var_h)).intValue(); //Or ... var_i = floor( var_h )
            double var_1 = value * (1 - saturation);
            double var_2 = value * (1 - saturation * (var_h - var_i));
            double var_3 = value * (1 - saturation * (1 - (var_h - var_i)));
            double var_r;
            double var_g;
            double var_b;
            if (var_i == 0) {
                var_r = value;
                var_g = var_3;
                var_b = var_1;
            } else if (var_i == 1) {
                var_r = var_2;
                var_g = value;
                var_b = var_1;
            } else if (var_i == 2) {
                var_r = var_1;
                var_g = value;
                var_b = var_3;
            } else if (var_i == 3) {
                var_r = var_1;
                var_g = var_2;
                var_b = value;
            } else if (var_i == 4) {
                var_r = var_3;
                var_g = var_1;
                var_b = value;
            } else {
                var_r = value;
                var_g = var_1;
                var_b = var_2;
            }
            R = new Double(var_r * 255).intValue(); //RGB results = 0 / 255
            G = new Double(var_g * 255).intValue();
            B = new Double(var_b * 255).intValue();
        }
        int[] returnArray = new int[3];
        returnArray[0] = R;
        returnArray[1] = G;
        returnArray[2] = B;
        return returnArray;
    }
}

Related

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