7
7
* calculate contrast ratio between colors on the web. This is used to calculate
8
8
* the readability of a foreground color on top of a background color.
9
9
* @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>
12
11
* @author [Seth Falco](https://github.com/SethFalco)
13
12
*/
14
13
public class ColorContrastRatio {
@@ -34,8 +33,7 @@ public double getContrastRatio(Color a, Color b) {
34
33
* @brief Calculates the relative luminance of a given color.
35
34
* @param color Any color, used to get the red, green, and blue values.
36
35
* @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>
39
37
*/
40
38
public double getRelativeLuminance (Color color ) {
41
39
final double red = getColor (color .getRed ());
@@ -46,63 +44,21 @@ public double getRelativeLuminance(Color color) {
46
44
}
47
45
48
46
/**
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.
51
48
* @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.
54
50
*/
55
51
public double getColor (int color8Bit ) {
56
52
final double sRgb = getColorSRgb (color8Bit );
57
53
return (sRgb <= 0.03928 ) ? sRgb / 12.92 : Math .pow ((sRgb + 0.055 ) / 1.055 , 2.4 );
58
54
}
59
55
60
56
/**
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.
63
58
* @param color8Bit 8-bit representation of a color component value.
64
59
* @return A percentile value of the color component.
65
60
*/
66
61
private double getColorSRgb (double color8Bit ) {
67
62
return color8Bit / 255.0 ;
68
63
}
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
- }
108
64
}
0 commit comments