Java RGB Color Convert To rgb2hsv(int r, int g, int b)

Here you can find the source of rgb2hsv(int r, int g, int b)

Description

rgbhsv

License

Open Source License

Declaration

public static float[] rgb2hsv(int r, int g, int b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static float[] rgb2hsv(int r, int g, int b) {
        float r1 = ((float) r) / 255;
        float g1 = ((float) g) / 255;
        float b1 = ((float) b) / 255;

        float cMax = max(r1, g1, b1);
        float cMin = min(r1, g1, b1);

        float delta = cMax - cMin;

        float h = 0, s = 0, v = 0;

        if (delta == 0) {
            h = 0;//from  w  w  w. j a v  a  2 s.  c o  m
        } else if (cMax == r1) {
            h = 60 * (((g1 - b1) / delta) % 6);
        } else if (cMax == g1) {
            h = 60 * (((b1 - r1) / delta) + 2);
        } else if (cMax == b1) {
            h = 60 * (((r1 - g1) / delta) + 4);
        }

        if (h < 0)
            h += 360;

        if (cMax == 0) {
            s = 0;
        } else {
            s = delta / cMax;
        }

        v = cMax;

        return new float[] { h, s, v };
    }

    private static float max(float... f) {
        float max = Float.MIN_VALUE;
        for (float n : f) {
            if (max < n)
                max = n;
        }
        return max;
    }

    private static float min(float... f) {
        float min = Float.MAX_VALUE;
        for (float n : f) {
            if (min > n)
                min = n;
        }
        return min;
    }
}

Related

  1. rgb2Hex(int rgb)
  2. rgb2hex(int[] rgb)
  3. rgb2hsl(int colour)
  4. RGB2HSL(int red, int green, int blue, float[] hslvals)
  5. rgb2hsl(int[] rgb)
  6. rgb2int(final int[] color)
  7. rgb2intval(int r, int g, int b)
  8. rgb2lab(int R, int G, int B)
  9. rgb2luv(double[] rgb, double[] luv)