Java RGB Color Convert To RGBToHSV(int red, int green, int blue, float[] hsv)

Here you can find the source of RGBToHSV(int red, int green, int blue, float[] hsv)

Description

Convert RGB components to HSV (hue-saturation-value).

License

Apache License

Parameter

Parameter Description
r red component value [0..255]
g green component value [0..255]
b blue component value [0..255]
hsv 3 element array which holds the resulting HSV components.

Declaration

public static void RGBToHSV(int red, int green, int blue, float[] hsv) 

Method Source Code

//package com.java2s;
/*/*from  w ww. j  av a  2  s  .c  om*/
 * Copyright 2015 The Android Open Source Project
 *
 * 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 {
    /**
     * Convert RGB components to HSV (hue-saturation-value).
     * <ul>
     * <li>hsl[0] is Hue [0 .. 360)</li>
     * <li>hsl[1] is Saturation [0...1]</li>
     * <li>hsl[2] is Value [0...1]</li>
     * </ul>
     *
     * @param r   red component value [0..255]
     * @param g   green component value [0..255]
     * @param b   blue component value [0..255]
     * @param hsv 3 element array which holds the resulting HSV components.
     */
    public static void RGBToHSV(int red, int green, int blue, float[] hsv) {
        float min, max, delta;

        min = Math.min(red, Math.min(green, blue));
        max = Math.max(red, Math.max(green, blue));
        hsv[2] = max; // v

        delta = max - min;

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

        if (red == max)
            hsv[0] = (green - blue) / delta; // between yellow & magenta
        else if (green == max)
            hsv[0] = 2 + (blue - red) / delta; // between cyan & yellow
        else
            hsv[0] = 4 + (red - green) / delta; // between magenta & cyan

        hsv[0] *= 60; // degrees
        if (hsv[0] < 0)
            hsv[0] += 360;
    }
}

Related

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