Java HSV Color to HSB hsv2rgb(float h, float s, float v, float[] rgb)

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

Description

hsvrgb

License

Open Source License

Declaration

public static void hsv2rgb(float h, float s, float v, float[] rgb) 

Method Source Code

//package com.java2s;
/*/*  ww w. java2  s . c o  m*/
 * Copyright (c) 2004-2016 Universidade do Porto - Faculdade de Engenharia
 * Laborat?rio de Sistemas e Tecnologia Subaqu?tica (LSTS)
 * All rights reserved.
 * Rua Dr. Roberto Frias s/n, sala I203, 4200-465 Porto, Portugal
 *
 * This file is part of Neptus, Command and Control Framework.
 *
 * Commercial Licence Usage
 * Licencees holding valid commercial Neptus licences may use this file
 * in accordance with the commercial licence agreement provided with the
 * Software or, alternatively, in accordance with the terms contained in a
 * written agreement between you and Universidade do Porto. For licensing
 * terms, conditions, and further information contact lsts@fe.up.pt.
 *
 * European Union Public Licence - EUPL v.1.1 Usage
 * Alternatively, this file may be used under the terms of the EUPL,
 * Version 1.1 only (the "Licence"), appearing in the file LICENCE.md
 * included in the packaging of this file. You may not use this work
 * except in compliance with the Licence. Unless required by applicable
 * law or agreed to in writing, software distributed under the Licence is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the Licence for the specific
 * language governing permissions and limitations at
 * http://ec.europa.eu/idabc/eupl.html.
 *
 * For more information please see <http://lsts.fe.up.pt/neptus>.
 *
 * Author: Paulo Dias
 * 2010/05/19
 */

public class Main {
    public static void hsv2rgb(float h, float s, float v, float[] rgb) {
        // H is given on [0->6] or -1. S and V are given on [0->1].
        // RGB are each returned on [0->1].
        float m, n, f;
        int i;

        float[] hsv = new float[3];

        hsv[0] = h;
        hsv[1] = s;
        hsv[2] = v;
        System.out.println("H: " + h + " S: " + s + " V:" + v);
        if (hsv[0] == -1) {
            rgb[0] = rgb[1] = rgb[2] = hsv[2];
            return;
        }
        i = (int) (Math.floor(hsv[0]));
        f = hsv[0] - i;
        if (i % 2 == 0)
            f = 1 - f; // if i is even
        m = hsv[2] * (1 - hsv[1]);
        n = hsv[2] * (1 - hsv[1] * f);
        switch (i) {
        case 6:
        case 0:
            rgb[0] = hsv[2];
            rgb[1] = n;
            rgb[2] = m;
            break;
        case 1:
            rgb[0] = n;
            rgb[1] = hsv[2];
            rgb[2] = m;
            break;
        case 2:
            rgb[0] = m;
            rgb[1] = hsv[2];
            rgb[2] = n;
            break;
        case 3:
            rgb[0] = m;
            rgb[1] = n;
            rgb[2] = hsv[2];
            break;
        case 4:
            rgb[0] = n;
            rgb[1] = m;
            rgb[2] = hsv[2];
            break;
        case 5:
            rgb[0] = hsv[2];
            rgb[1] = m;
            rgb[2] = n;
            break;
        }
    }
}

Related

  1. hsv2rgb(float h, float s, float v)
  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)