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

Converts an RGB color value [0-255] to HSV [0-1].

License

Open Source License

Parameter

Parameter Description
b The blue color value

Return

double[] The HSV representation

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  w w  w.  j  a va  2 s  .c  o  m
     * Converts an RGB color value [0-255] to HSV [0-1].
     *
     * @param   Number  r       The red color value
     * @param   Number  g       The green color value
     * @param   Number  b       The blue color value
     * @return  double[]        The HSV representation
     */
    public static double[] rgbToHsv(int red, int green, int blue) {
        double r = (double) red / 255.0;
        double g = (double) green / 255.0;
        double b = (double) blue / 255.0;

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

        double h = 0;
        double s = 0;
        double v = max;

        double d = max - min;

        s = max == 0 ? 0 : d / max;

        if (max == min) {
            h = 0; // achromatic
        } else {
            if (max == r) {
                h = (g - b) / d + (g < b ? 6 : 0);
            } else if (max == g) {
                h = (b - r) / d + 2;
            } else if (max == b) {
                h = (r - g) / d + 4;
            }
            h /= 6;
        }

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

Related

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