Created
October 8, 2016 10:47
-
-
Save WanghongLin/4e4c457db5c63741a915280bcf78e1b7 to your computer and use it in GitHub Desktop.
Conversion between RGB and HSV
This file contains 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
import java.awt.Color; | |
import java.util.Arrays; | |
/** | |
* Created by wanghong on 10/8/16. | |
*/ | |
public class ColorSpace { | |
public static void main(String[] args) { | |
System.out.println("Our implementation"); | |
System.out.println(rgb2hsv(new Rgb(0.5, 0.6, 0.3))); | |
System.out.println(hsv2rgb(new Hsv(80, 0.5, 0.6))); | |
System.out.println(); | |
System.out.println("AWT implementation"); | |
float[] hsb = new float[3]; | |
Color.RGBtoHSB((int) (0.5 * 255), (int) (0.6 * 255), (int) (0.3 * 255), hsb); | |
hsb[0] *= 360; | |
System.out.println(Arrays.toString(hsb)); | |
int color = Color.HSBtoRGB(80.0f / 360.0f, 0.5f, 0.6f); | |
System.out.println("[r = " + (color >> 16 & 0xff) / 255.0 + | |
", g = " + (color >> 8 & 0xff) / 255.0 + | |
", b = " + (color & 0xff) / 255.0 + "]"); | |
} | |
public static Rgb hsv2rgb(Hsv hsv) { | |
Rgb rgb = new Rgb(); | |
double hh = hsv.h / 60; | |
int i = ((int) hh) % 6; | |
double f = hh - i; | |
double p = hsv.v * (1 - hsv.s); | |
double q = hsv.v * (1 - f * hsv.s); | |
double t = hsv.v * (1 - (1 - f) * hsv.s); | |
switch (i) { | |
case 0: | |
rgb.r = hsv.v; rgb.g = t; rgb.b = q; break; | |
case 1: | |
rgb.r = q; rgb.g = hsv.v; rgb.b = p; break; | |
case 2: | |
rgb.r = p; rgb.g = hsv.v; rgb.b = t; break; | |
case 3: | |
rgb.r = p; rgb.g = q; rgb.b = hsv.v; break; | |
case 4: | |
rgb.r = t; rgb.g = p; rgb.b = hsv.v; break; | |
case 5: | |
rgb.r = hsv.v; rgb.g = p; rgb.b = q; break; | |
default: | |
} | |
return rgb; | |
} | |
public static Hsv rgb2hsv(Rgb rgb) { | |
Hsv hsv = new Hsv(); | |
double max = Math.max(Math.max(rgb.r, rgb.g), rgb.b); | |
double min = Math.min(Math.min(rgb.r, rgb.g), rgb.b); | |
double delta = max - min; | |
if (delta == 0) { | |
hsv.h = 360; | |
hsv.s = 0; | |
hsv.v = max; | |
return hsv; | |
} | |
if (max == rgb.r) { | |
hsv.h = (rgb.g - rgb.b) / delta % 6; | |
} else if (max == rgb.g) { | |
hsv.h = (rgb.b - rgb.r) / delta + 2; | |
} else { | |
hsv.h = (rgb.r - rgb.g) / delta + 4; | |
} | |
hsv.h *= 60; | |
if (max == 0) { | |
hsv.s = 0; | |
} else { | |
hsv.s = delta / max; | |
} | |
hsv.v = max; | |
return hsv; | |
} | |
private static class Hsv { | |
double h; /* 0 ~ 360 degree */ | |
double s; /* 0.0 ~ 1.0 */ | |
double v; /* 0.0 ~ 1.0 */ | |
public Hsv() { | |
} | |
public Hsv(double h, double s, double v) { | |
this.h = h; | |
this.s = s; | |
this.v = v; | |
} | |
@Override | |
public String toString() { | |
return "[h = " + h + ", s = " + s + ", v = " + v + "]"; | |
} | |
} | |
private static class Rgb { | |
double r; /* 0.0 ~ 1.0 */ | |
double g; /* 0.0 ~ 1.0 */ | |
double b; /* 0.0 ~ 1.0 */ | |
public Rgb() { | |
} | |
public Rgb(double r, double g, double b) { | |
this.r = r; | |
this.g = g; | |
this.b = b; | |
} | |
@Override | |
public String toString() { | |
return "[r = " + r + ", g = " + g + ", b = " + b + "]"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment