Java RGB Color Convert To RGBtoHSV(int r, int g, int b, double hsv[])

Here you can find the source of RGBtoHSV(int r, int g, int b, double hsv[])

Description

Convert r,g,b to h,s,v

License

Apache License

Parameter

Parameter Description
r Red value
g Green value
b Blue value
hsv Array which will contain hsv values

Declaration

public static void RGBtoHSV(int r, int g, int b, double hsv[]) 

Method Source Code

//package com.java2s;
/*/*w w w  .j  a  v a 2 s .co m*/
 * Copyright 2015 ROLLUS Lo?c
 *
 * 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 r,g,b to h,s,v
     * @param r Red value
     * @param g Green value
     * @param b Blue value
     * @param hsv Array which will contain hsv values
     */
    public static void RGBtoHSV(int r, int g, int b, double hsv[]) {

        int min; //Min. value of RGB
        int max; //Max. value of RGB
        int delMax; //Delta RGB value

        if (r > g) {
            min = g;
            max = r;
        } else {
            min = r;
            max = g;
        }
        if (b > max) {
            max = b;
        }
        if (b < min) {
            min = b;
        }

        delMax = max - min;

        float H = 0, S;
        float V = max;

        if (delMax == 0) {
            H = 0;
            S = 0;
        } else {
            S = delMax / 255f;
            if (r == max) {
                H = ((g - b) / (float) delMax) * 60;
            } else if (g == max) {
                H = (2 + (b - r) / (float) delMax) * 60;
            } else if (b == max) {
                H = (4 + (r - g) / (float) delMax) * 60;
            }
        }

        hsv[3] = (H + 60) / 1.411764706d;
        hsv[4] = (S * 100);
        hsv[5] = V;
    }

    /**
     * Convert rgb value to h,s,v
     * @param rgb RGB value
     * @param hsv Array which will contain hsv values
     */
    public static void RGBToHSV(int rgb, double[] hsv) {
        RGBtoHSV((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF, hsv);
    }
}

Related

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