Created
September 16, 2014 16:12
-
-
Save mikefaille/1fad8682cea66fac2f08 to your computer and use it in GitHub Desktop.
HSVtoRGB and vice versa test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HSVtoRGB { | |
public static void main(String[] args) { | |
float []hsbVal = null; | |
hsbVal = RGBtoHSBfloat(180, 58, 58, hsbVal ); | |
int rgb = HSBtoRGB(hsbVal[0], hsbVal[1], hsbVal[2]); | |
int red = (rgb >> 16) & 0xFF; | |
int green = (rgb >> 8) & 0xFF; | |
int blue = rgb & 0xFF; | |
System.out.println(hsbVal[0] + "\n" + hsbVal[1] + "\n"+ hsbVal[2]); | |
System.out.println("red : " + red + "\nGreen : " + green + " \n blue : " +blue ); | |
int[] HSB = RGBintToHSBint(red, green, blue); | |
System.out.println("Hue : " + HSB[0] + "\nSaturation " + HSB[1] + "\nValue : "+ HSB[2] ); | |
} | |
private static float[] RGBtoHSBfloat(int r, int g, int b, float[] hsbvals) { | |
float hue, saturation, brightness; | |
if (hsbvals == null) { | |
hsbvals = new float[3]; | |
} | |
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; | |
} | |
public static int[] RGBintToHSBint(int red, int green, int blue) { | |
int []HSB = new int[3]; | |
float []hsbVal = null; | |
hsbVal = RGBtoHSBfloat(red, green, blue, hsbVal); | |
HSB[0] = (int) ((int)Math.floor(hsbVal[0] *255)); | |
HSB[1] = (int) ((int)Math.floor(hsbVal[1] *255)); | |
HSB[2] = (int) ((int)Math.floor(hsbVal[2] *255)); | |
//System.out.println((int)Math.floor(1*255));; | |
return HSB; | |
} | |
public static int HSBtoRGB(float hue, float saturation, float brightness) { | |
int r = 0, g = 0, b = 0; | |
if (saturation == 0) { | |
r = g = b = (int) (brightness * 255.0f + 0.5f); | |
} else { | |
float h = (hue - (float)Math.floor(hue)) * 6.0f; | |
float f = h - (float)java.lang.Math.floor(h); | |
float p = brightness * (1.0f - saturation); | |
float q = brightness * (1.0f - saturation * f); | |
float t = brightness * (1.0f - (saturation * (1.0f - f))); | |
switch ((int) h) { | |
case 0: | |
r = (int) (brightness * 255.0f + 0.5f); | |
g = (int) (t * 255.0f + 0.5f); | |
b = (int) (p * 255.0f + 0.5f); | |
break; | |
case 1: | |
r = (int) (q * 255.0f + 0.5f); | |
g = (int) (brightness * 255.0f + 0.5f); | |
b = (int) (p * 255.0f + 0.5f); | |
break; | |
case 2: | |
r = (int) (p * 255.0f + 0.5f); | |
g = (int) (brightness * 255.0f + 0.5f); | |
b = (int) (t * 255.0f + 0.5f); | |
break; | |
case 3: | |
r = (int) (p * 255.0f + 0.5f); | |
g = (int) (q * 255.0f + 0.5f); | |
b = (int) (brightness * 255.0f + 0.5f); | |
break; | |
case 4: | |
r = (int) (t * 255.0f + 0.5f); | |
g = (int) (p * 255.0f + 0.5f); | |
b = (int) (brightness * 255.0f + 0.5f); | |
break; | |
case 5: | |
r = (int) (brightness * 255.0f + 0.5f); | |
g = (int) (p * 255.0f + 0.5f); | |
b = (int) (q * 255.0f + 0.5f); | |
break; | |
} | |
} | |
return 0xff000000 | (r << 16) | (g << 8) | (b << 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment