Java RGB Color Convert To RGBToHSV(float... rgb)

Here you can find the source of RGBToHSV(float... rgb)

Description

RGB To HSV

License

Open Source License

Declaration

public static float[] RGBToHSV(float... rgb) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

public class Main {
    public static float[] RGBToHSV(float... rgb) {
        float r = Math.min(Math.max(rgb[0], 0f), 1f);
        float g = Math.min(Math.max(rgb[1], 0f), 1f);
        float b = Math.min(Math.max(rgb[2], 0f), 1f);

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

        float delta = max - min;

        if (max == 0f)
            return new float[] { 0f, 0f, 0f };

        float s = delta / max;
        float h = 0f;
        if (delta == 0f)
            h = 0f;//from ww  w  .  j  a  v a 2  s.  com
        else if (rgb[0] == max)
            h = (rgb[1] - rgb[2]) / delta;
        else if (rgb[1] == max)
            h = 2 + (rgb[2] - rgb[0]) / delta;
        else
            h = 4 + (rgb[0] - rgb[1]) / delta;

        h *= 60;
        if (h < 0)
            h += 360;
        return new float[] { h / 360f, s, max };
    }
}

Related

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