Java RGB Color Convert To RGBtoHSV(double r, double g, double b)

Here you can find the source of RGBtoHSV(double r, double g, double b)

Description

Calculates the HSV color value for a given RGB value.

License

Open Source License

Parameter

Parameter Description
r value is from 0 to 1
g value is from 0 to 1
b value is from 0 to 1

Return

the calculated hsv value to return.

Declaration

public static double[] RGBtoHSV(double r, double g, double b) 

Method Source Code

//package com.java2s;
/*/*from   ww  w.ja v  a 2s .  co m*/
 *  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.
 */

public class Main {
    /**
     * Calculates the HSV color value for a given RGB value. The return values
     * are h = [0,360], s = [0,1], v = [0,1]. If s == 0, then h = -1 (undefined)
     *
     * @param r value is from 0 to 1
     * @param g value is from 0 to 1
     * @param b value is from 0 to 1
     * @return the calculated hsv value to return.
     */
    public static double[] RGBtoHSV(double r, double g, double b) {

        double h, s, v;

        double min, max, delta;

        min = Math.min(Math.min(r, g), b);
        max = Math.max(Math.max(r, g), b);

        // V
        v = max;

        delta = max - min;

        // S
        if (max != 0) {
            s = delta / max;
        } else {
            s = 0;
            h = -1;
            return new double[] { h, s, v };
        }

        // H
        if (r == max) {
            h = (g - b) / delta; // between yellow & magenta
        } else if (g == max) {
            h = 2 + (b - r) / delta; // between cyan & yellow
        } else {
            h = 4 + (r - g) / delta; // between magenta & cyan
        }
        h *= 60; // degrees

        if (h < 0) {
            h += 360;
        }

        return new double[] { h, s, v };
    }
}

Related

  1. RGBtoHSL(int r, int g, int b)
  2. RGBtoHSL(int r, int g, int b)
  3. RGBtoHSL(int r, int g, int b, float[] hsl)
  4. rgbToHsl(int red, int green, int blue)
  5. rgbToHsv(byte[] rgb, float[] hsv)
  6. RGBtoHSV(float r, float g, float b, float[] result)
  7. RGBToHSV(float... rgb)
  8. RGBtoHSV(float[] hsv, float[] rgb)
  9. RGBtoHSV(int r, int g, int b, double hsv[])