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

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

Description

r,g,b values are from 0 to 1 h = [0,360], s = [0,1], v = [0,1] if s == 0, then h = -1 (undefined)

License

Open Source License

Parameter

Parameter Description
h the hue value
s the saturation value
v the value

Return

the RGB to return

Declaration

public static Color HSVtoRGB(float h, float s, float v) 

Method Source Code


//package com.java2s;
/*//from w w w. j  a v  a2 s  .  c om
 *  ColorUtils.java 
 *
 *  Created by DFKI AV on 01.01.2012.
 *  Copyright (c) 2011-2012 DFKI GmbH, Kaiserslautern. All rights reserved.
 *  Use is subject to license terms.
 */

import java.awt.Color;

public class Main {
    /**
     * r,g,b values are from 0 to 1 h = [0,360], s = [0,1], v = [0,1] if s == 0,
     * then h = -1 (undefined)
     *
     * @param h the hue value
     * @param s the saturation value
     * @param v the value
     * @return the RGB {@link Color} to return
     */
    public static Color HSVtoRGB(float h, float s, float v) {
        int i;
        float f, p, q, t;
        float r, g, b;
        if (s == 0) {
            // achromatic (grey)
            r = g = b = v;
            return new Color(r, g, b);
        }

        h /= 60; // 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;
        }
        return new Color(r, g, b);
    }
}

Related

  1. HSVtoRGB(double[] data)
  2. hsvToRgb(float H, float S, float V)