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 */
1413public 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}
0 commit comments