Java HSV Color to RGB Color HSVtoRGB(double[] data)

Here you can find the source of HSVtoRGB(double[] data)

Description

<p>Returns the color object represented by the HSV.</p> <p>The code is taken from Twyst <http://www.colormixers.com/>.<br /> I've just translated it into Java.</p>

License

Apache License

Parameter

Parameter Description
data An double[] with three items (0=h; s=1; 2=v)

Return

The Color object based on the HSV values

Declaration

public static Color HSVtoRGB(double[] data) 

Method Source Code


//package com.java2s;
/*//from  w  w w .j  a va2  s . co  m
 * Copyright 2005 Patrick Gotthardt
 *
 * 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.
 */

import java.awt.*;

public class Main {
    /**
     * <p>Returns the color object represented by the HSV.</p>
     * <p>The code is taken from Twyst <http://www.colormixers.com/>.<br />
     * I've just translated it into Java.</p>
     *
     * @author Twyst, Patrick Gotthardt
     * @param data An double[] with three items (0=h; s=1; 2=v)
     * @return The Color object based on the HSV values
     */
    public static Color HSVtoRGB(double[] data) {
        if (data == null || data.length != 3) {
            throw new IllegalArgumentException("data must be an array of 3 items and must not be null!");
        }
        return HSVtoRGB(data[0], data[1], data[2]);
    }

    /**
     * <p>Returns the color object represented by the HSV.</p>
     * <p>The code is taken from Twyst <http://www.colormixers.com/>.<br />
     * I've just translated it into Java.</p>
     * <p>All values must be between 0 and 255!.</p>
     *
     * @author Twyst, Patrick Gotthardt
     * @param h The "H"-value of the color.
     * @param s The "S"-value of the color.
     * @param v The "V"-value of the color.
     * @return The Color object based on the HSV values
     */
    public static Color HSVtoRGB(double h, double s, double v) {
        int r = 0, g = 0, b = 0;

        if (s == 0) {
            r = g = b = (int) Math.round(v * 2.55);
        } else {
            h = h / 60;
            s = s / 100;
            v = v / 100;

            double i = Math.floor(h);
            double f = h - i;

            int p = (int) Math.round(255 * (v * (1 - s)));
            int q = (int) Math.round(255 * (v * (1 - s * f)));
            int t = (int) Math.round(255 * (v * (1 - s * (1 - f))));
            int v2 = (int) Math.round(255 * v);

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

        return new Color(r, g, b);
    }
}

Related

  1. HSVtoRGB(float h, float s, float v)
  2. hsvToRgb(float H, float S, float V)