diff --git a/src/main/java/com/thealgorithms/misc/ColorContrastRatio.java b/src/main/java/com/thealgorithms/misc/ColorContrastRatio.java
index 2d8371a9a53d..a7e3f651cc85 100644
--- a/src/main/java/com/thealgorithms/misc/ColorContrastRatio.java
+++ b/src/main/java/com/thealgorithms/misc/ColorContrastRatio.java
@@ -7,8 +7,7 @@
* calculate contrast ratio between colors on the web. This is used to calculate
* the readability of a foreground color on top of a background color.
* @since 2020-10-15
- * @see [Color Contrast
- * Ratio](https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure)
+ * @see Color Contrast Ratio
* @author [Seth Falco](https://github.com/SethFalco)
*/
public class ColorContrastRatio {
@@ -34,8 +33,7 @@ public double getContrastRatio(Color a, Color b) {
* @brief Calculates the relative luminance of a given color.
* @param color Any color, used to get the red, green, and blue values.
* @return The relative luminance of the color.
- * @see [More info on relative
- * luminance.](https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef)
+ * @see More info on relative luminance.
*/
public double getRelativeLuminance(Color color) {
final double red = getColor(color.getRed());
@@ -46,11 +44,9 @@ public double getRelativeLuminance(Color color) {
}
/**
- * @brief Calculates the final value for a color to be used in the relative
- * luminance formula as described in step 1.
+ * @brief Calculates the final value for a color to be used in the relative luminance formula as described in step 1.
* @param color8Bit 8-bit representation of a color component value.
- * @return Value for the provided color component to be used in the relative
- * luminance formula.
+ * @return Value for the provided color component to be used in the relative luminance formula.
*/
public double getColor(int color8Bit) {
final double sRgb = getColorSRgb(color8Bit);
@@ -58,51 +54,11 @@ public double getColor(int color8Bit) {
}
/**
- * @brief Calculates the Color sRGB value as denoted in step 1 of the
- * procedure document.
+ * @brief Calculates the Color sRGB value as denoted in step 1 of the procedure document.
* @param color8Bit 8-bit representation of a color component value.
* @return A percentile value of the color component.
*/
private double getColorSRgb(double color8Bit) {
return color8Bit / 255.0;
}
-
- /**
- * You can check this example against another open-source implementation
- * available on GitHub.
- *
- * @see [Online Contrast
- * Ratio](https://contrast-ratio.com/#rgb%28226%2C%20229%2C%20248-on-rgb%2823%2C%20103%2C%20154%29)
- * @see [GitHub Repository for Online Contrast
- * Ratio](https://github.com/LeaVerou/contrast-ratio)
- */
- private static void test() {
- final ColorContrastRatio algImpl = new ColorContrastRatio();
-
- final Color black = Color.BLACK;
- final double blackLuminance = algImpl.getRelativeLuminance(black);
- assert blackLuminance == 0 : "Test 1 Failed - Incorrect relative luminance.";
-
- final Color white = Color.WHITE;
- final double whiteLuminance = algImpl.getRelativeLuminance(white);
- assert whiteLuminance == 1 : "Test 2 Failed - Incorrect relative luminance.";
-
- final double highestColorRatio = algImpl.getContrastRatio(black, white);
- assert highestColorRatio == 21 : "Test 3 Failed - Incorrect contrast ratio.";
-
- final Color foreground = new Color(23, 103, 154);
- final double foregroundLuminance = algImpl.getRelativeLuminance(foreground);
- assert foregroundLuminance == 0.12215748057375966 : "Test 4 Failed - Incorrect relative luminance.";
-
- final Color background = new Color(226, 229, 248);
- final double backgroundLuminance = algImpl.getRelativeLuminance(background);
- assert backgroundLuminance == 0.7898468477881603 : "Test 5 Failed - Incorrect relative luminance.";
-
- final double contrastRatio = algImpl.getContrastRatio(foreground, background);
- assert contrastRatio == 4.878363954846178 : "Test 6 Failed - Incorrect contrast ratio.";
- }
-
- public static void main(String[] args) {
- test();
- }
}
diff --git a/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java b/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java
new file mode 100644
index 000000000000..6b80edf4b14c
--- /dev/null
+++ b/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java
@@ -0,0 +1,33 @@
+package com.thealgorithms.misc;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.awt.Color;
+import java.util.stream.Stream;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+class ColorContrastRatioTest {
+ private final ColorContrastRatio colorContrastRationCalculator = new ColorContrastRatio();
+
+ static Stream relativeLuminanceProvider() {
+ 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));
+ }
+
+ static Stream contrastRatioProvider() {
+ 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));
+ }
+
+ @ParameterizedTest
+ @MethodSource("relativeLuminanceProvider")
+ void testGetRelativeLuminance(Color color, double expectedLuminance) {
+ assertEquals(expectedLuminance, colorContrastRationCalculator.getRelativeLuminance(color), 1e-10);
+ }
+
+ @ParameterizedTest
+ @MethodSource("contrastRatioProvider")
+ void testGetContrastRatio(Color a, Color b, double expectedRatio) {
+ assertEquals(expectedRatio, colorContrastRationCalculator.getContrastRatio(a, b), 1e-10);
+ }
+}