Skip to content

Commit b95ba6c

Browse files
authored
RGB-HSV conversion (TheAlgorithms#2175)
* readded EulerMethod.java after sync * add RgbHsvConversion.java * Delete EulerMethod.java * add package
1 parent 5c3b3e7 commit b95ba6c

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

Conversions/RgbHsvConversion.java

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package Conversions;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* The RGB color model is an additive color model in which red, green, and blue light are added
7+
* together in various ways to reproduce a broad array of colors. The name of the model comes from
8+
* the initials of the three additive primary colors, red, green, and blue. Meanwhile, the HSV
9+
* representation models how colors appear under light. In it, colors are represented using three
10+
* components: hue, saturation and (brightness-)value. This class provides methods for converting
11+
* colors from one representation to the other. (description adapted from
12+
* https://en.wikipedia.org/wiki/RGB_color_model and https://en.wikipedia.org/wiki/HSL_and_HSV).
13+
*/
14+
public class RgbHsvConversion {
15+
16+
public static void main(String[] args) {
17+
// Expected RGB-values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html
18+
19+
// Test hsvToRgb-method
20+
assert Arrays.equals(hsvToRgb(0, 0, 0), new int[] {0, 0, 0});
21+
assert Arrays.equals(hsvToRgb(0, 0, 1), new int[] {255, 255, 255});
22+
assert Arrays.equals(hsvToRgb(0, 1, 1), new int[] {255, 0, 0});
23+
assert Arrays.equals(hsvToRgb(60, 1, 1), new int[] {255, 255, 0});
24+
assert Arrays.equals(hsvToRgb(120, 1, 1), new int[] {0, 255, 0});
25+
assert Arrays.equals(hsvToRgb(240, 1, 1), new int[] {0, 0, 255});
26+
assert Arrays.equals(hsvToRgb(300, 1, 1), new int[] {255, 0, 255});
27+
assert Arrays.equals(hsvToRgb(180, 0.5, 0.5), new int[] {64, 128, 128});
28+
assert Arrays.equals(hsvToRgb(234, 0.14, 0.88), new int[] {193, 196, 224});
29+
assert Arrays.equals(hsvToRgb(330, 0.75, 0.5), new int[] {128, 32, 80});
30+
31+
// Test rgbToHsv-method
32+
// approximate-assertions needed because of small deviations due to converting between
33+
// int-values and double-values.
34+
assert approximatelyEqualHsv(rgbToHsv(0, 0, 0), new double[] {0, 0, 0});
35+
assert approximatelyEqualHsv(rgbToHsv(255, 255, 255), new double[] {0, 0, 1});
36+
assert approximatelyEqualHsv(rgbToHsv(255, 0, 0), new double[] {0, 1, 1});
37+
assert approximatelyEqualHsv(rgbToHsv(255, 255, 0), new double[] {60, 1, 1});
38+
assert approximatelyEqualHsv(rgbToHsv(0, 255, 0), new double[] {120, 1, 1});
39+
assert approximatelyEqualHsv(rgbToHsv(0, 0, 255), new double[] {240, 1, 1});
40+
assert approximatelyEqualHsv(rgbToHsv(255, 0, 255), new double[] {300, 1, 1});
41+
assert approximatelyEqualHsv(rgbToHsv(64, 128, 128), new double[] {180, 0.5, 0.5});
42+
assert approximatelyEqualHsv(rgbToHsv(193, 196, 224), new double[] {234, 0.14, 0.88});
43+
assert approximatelyEqualHsv(rgbToHsv(128, 32, 80), new double[] {330, 0.75, 0.5});
44+
}
45+
46+
/**
47+
* Conversion from the HSV-representation to the RGB-representation.
48+
*
49+
* @param hue Hue of the color.
50+
* @param saturation Saturation of the color.
51+
* @param value Brightness-value of the color.
52+
* @return The tuple of RGB-components.
53+
*/
54+
public static int[] hsvToRgb(double hue, double saturation, double value) {
55+
if (hue < 0 || hue > 360) {
56+
throw new IllegalArgumentException("hue should be between 0 and 360");
57+
}
58+
59+
if (saturation < 0 || saturation > 1) {
60+
throw new IllegalArgumentException("saturation should be between 0 and 1");
61+
}
62+
63+
if (value < 0 || value > 1) {
64+
throw new IllegalArgumentException("value should be between 0 and 1");
65+
}
66+
67+
double chroma = value * saturation;
68+
double hueSection = hue / 60;
69+
double secondLargestComponent = chroma * (1 - Math.abs(hueSection % 2 - 1));
70+
double matchValue = value - chroma;
71+
72+
return getRgbBySection(hueSection, chroma, matchValue, secondLargestComponent);
73+
}
74+
75+
/**
76+
* Conversion from the RGB-representation to the HSV-representation.
77+
*
78+
* @param red Red-component of the color.
79+
* @param green Green-component of the color.
80+
* @param blue Blue-component of the color.
81+
* @return The tuple of HSV-components.
82+
*/
83+
public static double[] rgbToHsv(int red, int green, int blue) {
84+
if (red < 0 || red > 255) {
85+
throw new IllegalArgumentException("red should be between 0 and 255");
86+
}
87+
88+
if (green < 0 || green > 255) {
89+
throw new IllegalArgumentException("green should be between 0 and 255");
90+
}
91+
92+
if (blue < 0 || blue > 255) {
93+
throw new IllegalArgumentException("blue should be between 0 and 255");
94+
}
95+
96+
double dRed = (double) red / 255;
97+
double dGreen = (double) green / 255;
98+
double dBlue = (double) blue / 255;
99+
double value = Math.max(Math.max(dRed, dGreen), dBlue);
100+
double chroma = value - Math.min(Math.min(dRed, dGreen), dBlue);
101+
double saturation = value == 0 ? 0 : chroma / value;
102+
double hue;
103+
104+
if (chroma == 0) {
105+
hue = 0;
106+
} else if (value == dRed) {
107+
hue = 60 * (0 + (dGreen - dBlue) / chroma);
108+
} else if (value == dGreen) {
109+
hue = 60 * (2 + (dBlue - dRed) / chroma);
110+
} else {
111+
hue = 60 * (4 + (dRed - dGreen) / chroma);
112+
}
113+
114+
hue = (hue + 360) % 360;
115+
116+
return new double[] {hue, saturation, value};
117+
}
118+
119+
private static boolean approximatelyEqualHsv(double[] hsv1, double[] hsv2) {
120+
boolean bHue = Math.abs(hsv1[0] - hsv2[0]) < 0.2;
121+
boolean bSaturation = Math.abs(hsv1[1] - hsv2[1]) < 0.002;
122+
boolean bValue = Math.abs(hsv1[2] - hsv2[2]) < 0.002;
123+
124+
return bHue && bSaturation && bValue;
125+
}
126+
127+
private static int[] getRgbBySection(
128+
double hueSection, double chroma, double matchValue, double secondLargestComponent) {
129+
int red;
130+
int green;
131+
int blue;
132+
133+
if (hueSection >= 0 && hueSection <= 1) {
134+
red = convertToInt(chroma + matchValue);
135+
green = convertToInt(secondLargestComponent + matchValue);
136+
blue = convertToInt(matchValue);
137+
} else if (hueSection > 1 && hueSection <= 2) {
138+
red = convertToInt(secondLargestComponent + matchValue);
139+
green = convertToInt(chroma + matchValue);
140+
blue = convertToInt(matchValue);
141+
} else if (hueSection > 2 && hueSection <= 3) {
142+
red = convertToInt(matchValue);
143+
green = convertToInt(chroma + matchValue);
144+
blue = convertToInt(secondLargestComponent + matchValue);
145+
} else if (hueSection > 3 && hueSection <= 4) {
146+
red = convertToInt(matchValue);
147+
green = convertToInt(secondLargestComponent + matchValue);
148+
blue = convertToInt(chroma + matchValue);
149+
} else if (hueSection > 4 && hueSection <= 5) {
150+
red = convertToInt(secondLargestComponent + matchValue);
151+
green = convertToInt(matchValue);
152+
blue = convertToInt(chroma + matchValue);
153+
} else {
154+
red = convertToInt(chroma + matchValue);
155+
green = convertToInt(matchValue);
156+
blue = convertToInt(secondLargestComponent + matchValue);
157+
}
158+
159+
return new int[] {red, green, blue};
160+
}
161+
162+
private static int convertToInt(double input) {
163+
return (int) Math.round(255 * input);
164+
}
165+
}

0 commit comments

Comments
 (0)