Skip to content

Commit b6c738d

Browse files
committed
refactor: ColorContrastRatio
1 parent e782c7a commit b6c738d

File tree

2 files changed

+38
-49
lines changed

2 files changed

+38
-49
lines changed

src/main/java/com/thealgorithms/misc/ColorContrastRatio.java

+5-49
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
* calculate contrast ratio between colors on the web. This is used to calculate
88
* the readability of a foreground color on top of a background color.
99
* @since 2020-10-15
10-
* @see [Color Contrast
11-
* Ratio](https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure)
10+
* @see <a href="https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure">Color Contrast Ratio</a>
1211
* @author [Seth Falco](https://github.com/SethFalco)
1312
*/
1413
public class ColorContrastRatio {
@@ -34,8 +33,7 @@ public double getContrastRatio(Color a, Color b) {
3433
* @brief Calculates the relative luminance of a given color.
3534
* @param color Any color, used to get the red, green, and blue values.
3635
* @return The relative luminance of the color.
37-
* @see [More info on relative
38-
* luminance.](https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef)
36+
* @see <a href="https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef">More info on relative luminance.</a>
3937
*/
4038
public double getRelativeLuminance(Color color) {
4139
final double red = getColor(color.getRed());
@@ -46,63 +44,21 @@ public double getRelativeLuminance(Color color) {
4644
}
4745

4846
/**
49-
* @brief Calculates the final value for a color to be used in the relative
50-
* luminance formula as described in step 1.
47+
* @brief Calculates the final value for a color to be used in the relative luminance formula as described in step 1.
5148
* @param color8Bit 8-bit representation of a color component value.
52-
* @return Value for the provided color component to be used in the relative
53-
* luminance formula.
49+
* @return Value for the provided color component to be used in the relative luminance formula.
5450
*/
5551
public double getColor(int color8Bit) {
5652
final double sRgb = getColorSRgb(color8Bit);
5753
return (sRgb <= 0.03928) ? sRgb / 12.92 : Math.pow((sRgb + 0.055) / 1.055, 2.4);
5854
}
5955

6056
/**
61-
* @brief Calculates the Color sRGB value as denoted in step 1 of the
62-
* procedure document.
57+
* @brief Calculates the Color sRGB value as denoted in step 1 of the procedure document.
6358
* @param color8Bit 8-bit representation of a color component value.
6459
* @return A percentile value of the color component.
6560
*/
6661
private double getColorSRgb(double color8Bit) {
6762
return color8Bit / 255.0;
6863
}
69-
70-
/**
71-
* You can check this example against another open-source implementation
72-
* available on GitHub.
73-
*
74-
* @see [Online Contrast
75-
* Ratio](https://contrast-ratio.com/#rgb%28226%2C%20229%2C%20248-on-rgb%2823%2C%20103%2C%20154%29)
76-
* @see [GitHub Repository for Online Contrast
77-
* Ratio](https://github.com/LeaVerou/contrast-ratio)
78-
*/
79-
private static void test() {
80-
final ColorContrastRatio algImpl = new ColorContrastRatio();
81-
82-
final Color black = Color.BLACK;
83-
final double blackLuminance = algImpl.getRelativeLuminance(black);
84-
assert blackLuminance == 0 : "Test 1 Failed - Incorrect relative luminance.";
85-
86-
final Color white = Color.WHITE;
87-
final double whiteLuminance = algImpl.getRelativeLuminance(white);
88-
assert whiteLuminance == 1 : "Test 2 Failed - Incorrect relative luminance.";
89-
90-
final double highestColorRatio = algImpl.getContrastRatio(black, white);
91-
assert highestColorRatio == 21 : "Test 3 Failed - Incorrect contrast ratio.";
92-
93-
final Color foreground = new Color(23, 103, 154);
94-
final double foregroundLuminance = algImpl.getRelativeLuminance(foreground);
95-
assert foregroundLuminance == 0.12215748057375966 : "Test 4 Failed - Incorrect relative luminance.";
96-
97-
final Color background = new Color(226, 229, 248);
98-
final double backgroundLuminance = algImpl.getRelativeLuminance(background);
99-
assert backgroundLuminance == 0.7898468477881603 : "Test 5 Failed - Incorrect relative luminance.";
100-
101-
final double contrastRatio = algImpl.getContrastRatio(foreground, background);
102-
assert contrastRatio == 4.878363954846178 : "Test 6 Failed - Incorrect contrast ratio.";
103-
}
104-
105-
public static void main(String[] args) {
106-
test();
107-
}
10864
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.awt.Color;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
class ColorContrastRatioTest {
12+
private final ColorContrastRatio colorContrastRationCalculator = new ColorContrastRatio();
13+
14+
static Stream<Arguments> relativeLuminanceProvider() {
15+
return Stream.of(Arguments.of(Color.BLACK, 0.0), Arguments.of(Color.WHITE, 1.0), Arguments.of(new Color(23, 103, 154), 0.12215748057375966), Arguments.of(new Color(226, 229, 248), 0.7898468477881603));
16+
}
17+
18+
static Stream<Arguments> contrastRatioProvider() {
19+
return Stream.of(Arguments.of(Color.BLACK, Color.WHITE, 21.0), Arguments.of(new Color(23, 103, 154), new Color(226, 229, 248), 4.878363954846178));
20+
}
21+
22+
@ParameterizedTest
23+
@MethodSource("relativeLuminanceProvider")
24+
void testGetRelativeLuminance(Color color, double expectedLuminance) {
25+
assertEquals(expectedLuminance, colorContrastRationCalculator.getRelativeLuminance(color), 1e-10);
26+
}
27+
28+
@ParameterizedTest
29+
@MethodSource("contrastRatioProvider")
30+
void testGetContrastRatio(Color a, Color b, double expectedRatio) {
31+
assertEquals(expectedRatio, colorContrastRationCalculator.getContrastRatio(a, b), 1e-10);
32+
}
33+
}

0 commit comments

Comments
 (0)