Java RGB Color Convert To RGBtoHSB(int r, int g, int b, float[] hsbvals)

Here you can find the source of RGBtoHSB(int r, int g, int b, float[] hsbvals)

Description

RG Bto HSB

License

Open Source License

Declaration

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

Method Source Code

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

public class Main {
    public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals) {
        float hue, saturation, brightness;
        if (hsbvals == null) {
            hsbvals = new float[3];
        }//from   ww w.  j  a va2 s. c  o  m
        int cmax = (r > g) ? r : g;
        if (b > cmax)
            cmax = b;
        int cmin = (r < g) ? r : g;
        if (b < cmin)
            cmin = b;
        brightness = ((float) cmax) / 255.0f;
        if (cmax != 0)
            saturation = ((float) (cmax - cmin)) / ((float) cmax);
        else
            saturation = 0;
        if (saturation == 0)
            hue = 0;
        else {
            float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
            float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
            float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
            if (r == cmax)
                hue = bluec - greenc;
            else if (g == cmax)
                hue = 2.0f + redc - bluec;
            else
                hue = 4.0f + greenc - redc;
            hue = hue / 6.0f;
            if (hue < 0)
                hue = hue + 1.0f;
        }
        hsbvals[0] = hue;
        hsbvals[1] = saturation;
        hsbvals[2] = brightness;
        return hsbvals;
    }
}

Related

  1. RGBtoHex(int r, int g, int b)
  2. rgbToHex(int[] rgb)
  3. RGBToHex(int[] rgb)
  4. rgbToHex(String color)
  5. RGBtoHSB(int r, int g, int b, float hsbvals[])
  6. RGBtoHSB(int red, int green, int blue, float hsb[])
  7. rgbToHsl(byte red, byte green, byte blue)
  8. RgbToHsl(float r, float g, float b, float a)
  9. rgbToHsl(float[] rgb)