Converts a HSV array back to an RGB array. - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Converts a HSV array back to an RGB array.

Demo Code

/**/*w w w. jav a  2s .  c  o  m*/
 * CamanJ - Java Image Manipulation
 * Ported from the CamanJS Javascript library
 *
 * Copyright 2011, Ryan LeFevre
 * Licensed under the new BSD License
 * See LICENSE for more info.
 * 
 * Project Home: http://github.com/meltingice/CamanJ
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] hsv = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,
                37.1234, 67.2344, 68.34534, 69.87700 };
        System.out.println(java.util.Arrays.toString(hsvToRgb(hsv)));
    }

    /**
     * Converts a HSV array back to an RGB array.
     * 
     * @param hsv
     *            The HSV color array
     * @return The RGB color array
     */
    public static int[] hsvToRgb(double[] hsv) {
        double r = 0, g = 0, b = 0;
        double i = Math.floor(hsv[0] * 6);
        double f = hsv[0] * 6 - i;
        double p = hsv[2] * (1 - hsv[1]);
        double q = hsv[2] * (1 - f * hsv[1]);
        double t = hsv[2] * (1 - (1 - f) * hsv[1]);

        int mod = (int) i % 6;
        if (mod == 0) {
            r = hsv[2];
            g = t;
            b = p;
        } else if (mod == 1) {
            r = q;
            g = hsv[2];
            b = p;
        } else if (mod == 2) {
            r = p;
            g = hsv[2];
            b = t;
        } else if (mod == 3) {
            r = p;
            g = q;
            b = hsv[2];
        } else if (mod == 4) {
            r = t;
            g = p;
            b = hsv[2];
        } else if (mod == 5) {
            r = hsv[2];
            g = p;
            b = q;
        }

        r *= 255;
        g *= 255;
        b *= 255;

        return new int[] { (int) r, (int) g, (int) b, 255 };
    }
}

Related Tutorials