Java RGB Color Convert To convertRGBtoHSV(float[] rgb, float[] hsv)

Here you can find the source of convertRGBtoHSV(float[] rgb, float[] hsv)

Description

Change an RGB color to HSV color.

License

LGPL

Parameter

Parameter Description
rgb The array of RGB components to convert
hsv An array to return the colour values with

Declaration

public static void convertRGBtoHSV(float[] rgb, float[] hsv) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 *                        J3D.org Copyright (c) 2000
 *                               Java Source
 *
 * This source is licensed under the GNU LGPL v2.1
 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
 *
 ****************************************************************************/

public class Main {
    /**/*from  w w  w  .j  a va 2  s  .c  o m*/
     * Change an RGB color to HSV color. We don't bother converting the alpha
     * as that stays the same regardless of color space.
     *
     * @param rgb The array of RGB components to convert
     * @param hsv An array to return the colour values with
     */
    public static void convertRGBtoHSV(float[] rgb, float[] hsv) {
        convertRGBtoHSV(rgb[0], rgb[1], rgb[2], hsv);
    }

    /**
     * Change an RGB color to HSV color. We don't bother converting the alpha
     * as that stays the same regardless of color space.
     *
     * @param r The r component of the color
     * @param g The g component of the color
     * @param b The b component of the color
     * @param hsv An array to return the HSV colour values in
     */
    public static void convertRGBtoHSV(float r, float g, float b, float[] hsv) {
        float h = 0;
        float s = 0;
        float v = 0;

        float max = (r > g) ? r : g;
        max = (max > b) ? max : b;

        float min = (r < g) ? r : g;
        min = (min < b) ? min : b;

        v = max; // this is the value v

        // Calculate the saturation s
        if (max == 0) {
            s = 0;
            h = Float.NaN; // h => UNDEFINED
        } else {
            // Chromatic case: Saturation is not 0, determine hue
            float delta = max - min;
            s = delta / max;

            if (r == max) {
                // resulting color is between yellow and magenta
                h = (g - b) / delta;
            } else if (g == max) {
                // resulting color is between cyan and yellow
                h = 2 + (b - r) / delta;
            } else if (b == max) {
                // resulting color is between magenta and cyan
                h = 4 + (r - g) / delta;
            }

            // convert hue to degrees and make sure it is non-negative
            h *= 60;
            if (h < 0)
                h += 360;
        }

        // now assign everything....
        hsv[0] = h;
        hsv[1] = s;
        hsv[2] = v;
    }
}

Related

  1. convertRgb888To565(int rgb888)
  2. convertRgbaToArgb(int rgba)
  3. convertRgbToByteArray(int[] rgb)
  4. convertRGBtoHSL(int r, int g, int b)
  5. convertRGBtoYIQ(float[] rgb, float[] yiq)
  6. rgb24FloatArray(float[] array, int rgb24)
  7. rgb2arr(final int color, final int[] argbarr)
  8. rgb2bilevel(int[] rgb)