Java RGB Color Convert To RGBtoHSV(int red, int green, int blue)

Here you can find the source of RGBtoHSV(int red, int green, int blue)

Description

Method returns three doubles representing the HSV values of a RGB input Values in input must be between 0 and 255 Values in output are between 0 and 1.

License

Apache License

Parameter

Parameter Description
red a parameter
green a parameter
blue a parameter

Declaration

public static double[] RGBtoHSV(int red, int green, int blue) 

Method Source Code

//package com.java2s;
/*/*www  .  j a  v a 2 s  .  c  o m*/
 * Copyright 2007 Robert Hanson <iamroberthanson AT gmail.com>
 * 
 * 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 {
    /**
     * Method returns three doubles representing the HSV values of a RGB input
     * Values in input must be between 0 and 255
     * Values in output are between 0 and 1.
     * 
     * See http://www.easyrgb.com/math.php?MATH=M21#text21 for details of algorithm used.
     * 
     * @param red
     * @param green
     * @param blue
     * @return
     */
    public static double[] RGBtoHSV(int red, int green, int blue) {
        double H = 0, S = 0, V = 0;

        double R = red / 255.0;
        double G = green / 255.0;
        double B = blue / 255.0;

        double var_Min = Math.min(Math.min(R, G), B); //Min. value of RGB
        double var_Max = Math.max(Math.max(R, G), B); //Max. value of RGB
        double del_Max = var_Max - var_Min; //Delta RGB value

        V = var_Max;

        if (del_Max == 0) {
            // This is a gray, no chroma...
            H = 0; // HSV results = 0 / 1
            S = 0;
        } else {
            // Chromatic data...
            S = del_Max / var_Max;

            double del_R = (((var_Max - R) / 6) + (del_Max / 2)) / del_Max;
            double del_G = (((var_Max - G) / 6) + (del_Max / 2)) / del_Max;
            double del_B = (((var_Max - B) / 6) + (del_Max / 2)) / del_Max;

            if (R == var_Max) {
                H = del_B - del_G;
            } else if (G == var_Max) {
                H = (1 / 3) + del_R - del_B;
            } else if (B == var_Max) {
                H = (2 / 3) + del_G - del_R;
            }
            if (H < 0) {
                H += 1;
            }
            if (H > 1) {
                H -= 1;
            }
        }

        return new double[] { H, S, V };
    }
}

Related

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