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

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

Description

rgb To Hsv

License

Open Source License

Declaration

public static void rgbToHsv(byte[] rgb, float[] hsv) 

Method Source Code

//package com.java2s;
/*//from   w w  w .  j ava 2 s . c o  m
  ColorMapUtils.java
    
  (c) 2011-2013 Edward Swartz
    
  All rights reserved. This program and the accompanying materials
  are made available under the terms of the Eclipse Public License v1.0
  which accompanies this distribution, and is available at
  http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    public static void rgbToHsv(byte[] rgb, float[] hsv) {
        rgbToHsv(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff, hsv);
    }

    /**
     * Get hue/value/satuation.
     * 
     * @param r
     * @param g
     * @param b
     * @param hsv
     *            in 0-360, 0-1, 0-255
     */
    public static void rgbToHsv(int r, int g, int b, float[] hsv) {
        int theMin = Math.min(Math.min(r, g), b);
        int theMax = Math.max(Math.max(r, g), b);
        hsv[2] = theMax;
        float delta = (theMax - theMin);
        if (delta != 0)
            hsv[1] = delta / theMax;
        else {
            hsv[1] = 0;
            return;
        }
        if (r == theMax)
            hsv[0] = (g - b) / delta;
        else if (g == theMax)
            hsv[0] = 2 + (b - r) / delta;
        else
            hsv[0] = 4 + (r - g) / delta;
        hsv[0] *= 60.0;
        if (hsv[0] < 0)
            hsv[0] += 360.0;
    }

    /**
     * Get hue/value/satuation.
     * 
     * @param rgb
     * @param hsv
     *            in 0-360, 0-1, 0-255
     */
    public static void rgbToHsv(int[] rgb, float[] hsv) {
        rgbToHsv(rgb[0], rgb[1], rgb[2], hsv);
    }
}

Related

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