Here you can find the source of RGBtoHSB(int r, int g, int b, float[] hsbvals)
public static float[] RGBtoHSB(int r, int g, int b, float[] hsbvals)
//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; } }