Java RGB Color Convert To RGBtoHSV(float r, float g, float b, float[] result)

Here you can find the source of RGBtoHSV(float r, float g, float b, float[] result)

Description

http://www.cs.rit.edu/~ncs/color/t_convert.html

License

Apache License

Parameter

Parameter Description
r a parameter
g a parameter
b a parameter

Declaration

public static float[] RGBtoHSV(float r, float g, float b, float[] result) 

Method Source Code

//package com.java2s;
/*/* ww w  . jav a2 s . c o  m*/
 * Copyright 2011 Mark McKay
 * 
 * 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.
 */

public class Main {
    /**
     * http://www.cs.rit.edu/~ncs/color/t_convert.html
     * 
     * @param r
     * @param g
     * @param b
     * @return
     */
    public static float[] RGBtoHSV(float r, float g, float b, float[] result) {
        if (result == null) {
            result = new float[3];
        }

        float h, s, v;
        float min, max, delta;

        min = min(r, g, b);
        max = max(r, g, b);
        v = max; // v

        delta = max - min;

        if (max != 0) {
            s = delta / max; // s
        } else {
            // r = g = b = 0      // s = 0, v is undefined
            s = 0;
            h = -1;

            //            result[0] = 0;
            //            result[1] = 0;
            result[2] = 0;
            return result;
        }

        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;
        }

        if (delta != 0) {
            //Only set hue if saturation is non-zero
            result[0] = h / 360;
        }
        result[1] = s;
        result[2] = v;
        return result;
    }

    public static float min(float a, float b, float c) {
        return a < b && a < c ? a : (b < c ? b : c);
    }

    public static float max(float a, float b, float c) {
        return a > b && a > c ? a : (b > c ? b : c);
    }
}

Related

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