From 5a78f44f2e1c30ac1b4b9e1bd410e25b6d852e76 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Thu, 15 Oct 2020 14:09:43 +0200 Subject: [PATCH 01/15] Added algorithm for color contrast ratio. --- Misc/ColorContrastRatio.java | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Misc/ColorContrastRatio.java diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java new file mode 100644 index 000000000000..c38fdf759448 --- /dev/null +++ b/Misc/ColorContrastRatio.java @@ -0,0 +1,67 @@ +package Misc; + +/** + * A Java implementation of the offcial W3 documented procedure + * to calculate contrast ratio between colors on the web. + * + * @since 2020-10-15 + * @see Color Contrast Ratio Procedure + * @author Seth Falco (https://elypia.org/) + */ +public class ColorContrastRatio { + + /** + * Calculates the contrast ratio between two given colors. + * + * @param a Any color, used to get the red, green, and blue values. + * @param b Another color, which will be compared against the first color. + * @return The contrast ratio between the two colors. + */ + public double getContrastRatio(Color a, Color b) { + final double aColorLuminance = getRelativeLuminance(a); + final double bColorLuminance = getRelativeLuminance(b); + + if (aColorLuminance > bColorLuminance) + return (aColorLuminance + 0.05) / (bColorLuminance + 0.05); + else + return (bColorLuminance + 0.05) / (aColorLuminance + 0.05); + } + + /** + * 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. + */ + public double getRelativeLuminance(Color color) { + final double red = getColor(color.getRed()); + final double green = getColor(color.getGreen()); + final double blue = getColor(color.getBlue()); + + return 0.2126 * red + 0.7152 * green + 0.0722 * blue; + } + + /** + * Calculates the final value for a color to be used in the + * relative luminance formula as described in step 1. + * + * @param color8Bit The 8-bit repretation of a color component value. + * @return Value for the provided color component to be used in the relative luminance formula. + */ + public double getColor(int value) { + final double sRgb = getColorSRgb(value); + return (sRgb <= 0.03928) ? sRgb / 12.92 : Math.pow((sRgb + 0.055) / 1.055, 2.4); + } + + /** + * Calculates the Color sRGB value as denoted in step 1 + * of the procedure document. + * + * @param color8Bit The 8-bit repretation of a color component value. + * @return A percentile value of the color component. + */ + private double getColorSRgb(double color8Bit) { + return color8Bit / 255.0; + } +} From 4d740f915fb505ed5f3a86c654569a7f5f65be14 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Thu, 15 Oct 2020 23:52:13 +0200 Subject: [PATCH 02/15] Added comment and example usage in main. --- Misc/ColorContrastRatio.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index c38fdf759448..12d1690202bf 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -4,12 +4,35 @@ * A Java implementation of the offcial W3 documented procedure * to 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 Procedure * @author Seth Falco (https://elypia.org/) */ public class ColorContrastRatio { + /** + * You can check this example against another open-source implementation available on GitHub. + * + * @see Online Contrast Ratio + * @see GitHub Repository for Online Contrast Ratio + * @param args + */ + public static void main(String args[]) { + // Relative Luminance: 0.12215748057375966 + final Color foreground = new Color(23, 103, 154); + + // Relative Luminance: 0.7898468477881603 + final Color background = new Color(226, 229, 248); + + // Contrast Ratio: 4.878363954846178 + final double contrastRatio = getContrastRatio(foreground, background); + + System.out.println(contrastRatio); + } + /** * Calculates the contrast ratio between two given colors. * From f925a719fb393121514a3f6307b834ce94eebc4d Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sat, 17 Oct 2020 01:48:58 +0200 Subject: [PATCH 03/15] Fixed calling method in main without creating object. --- Misc/ColorContrastRatio.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index 12d1690202bf..4da10c1e1737 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -21,6 +21,8 @@ public class ColorContrastRatio { * @param args */ public static void main(String args[]) { + final ColorContrastRatio algorithmImpl = new ColorContrastRatio(); + // Relative Luminance: 0.12215748057375966 final Color foreground = new Color(23, 103, 154); @@ -28,7 +30,7 @@ public static void main(String args[]) { final Color background = new Color(226, 229, 248); // Contrast Ratio: 4.878363954846178 - final double contrastRatio = getContrastRatio(foreground, background); + final double contrastRatio = algorithmImpl.getContrastRatio(foreground, background); System.out.println(contrastRatio); } From 0e9055460461b229c0162c0753ce48b6b753422f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 27 Feb 2021 06:06:16 +0000 Subject: [PATCH 04/15] Formatted with Google Java Formatter --- Maths/ConvolutionFFT.java | 2 +- Misc/ColorContrastRatio.java | 144 ++++++++++++++++++----------------- 2 files changed, 74 insertions(+), 72 deletions(-) diff --git a/Maths/ConvolutionFFT.java b/Maths/ConvolutionFFT.java index 49032ae83578..f1d595087142 100644 --- a/Maths/ConvolutionFFT.java +++ b/Maths/ConvolutionFFT.java @@ -53,7 +53,7 @@ public static ArrayList convolutionFFT( convolved .subList(convolvedSize, convolved.size()) .clear(); // Remove the remaining zeros after the convolvedSize. These extra zeros came from - // paddingPowerOfTwo() method inside the fft() method. + // paddingPowerOfTwo() method inside the fft() method. return convolved; } diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index 4da10c1e1737..4b3943fde23a 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -1,92 +1,94 @@ package Misc; /** - * A Java implementation of the offcial W3 documented procedure - * to calculate contrast ratio between colors on the web. + * A Java implementation of the offcial W3 documented procedure to 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. + *

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 Procedure + * @see Color Contrast Ratio + * Procedure * @author Seth Falco (https://elypia.org/) */ public class ColorContrastRatio { - /** - * You can check this example against another open-source implementation available on GitHub. - * - * @see Online Contrast Ratio - * @see GitHub Repository for Online Contrast Ratio - * @param args - */ - public static void main(String args[]) { - final ColorContrastRatio algorithmImpl = new ColorContrastRatio(); + /** + * You can check this example against another open-source implementation available on GitHub. + * + * @see Online + * Contrast Ratio + * @see GitHub Repository for Online Contrast + * Ratio + * @param args + */ + public static void main(String args[]) { + final ColorContrastRatio algorithmImpl = new ColorContrastRatio(); - // Relative Luminance: 0.12215748057375966 - final Color foreground = new Color(23, 103, 154); + // Relative Luminance: 0.12215748057375966 + final Color foreground = new Color(23, 103, 154); - // Relative Luminance: 0.7898468477881603 - final Color background = new Color(226, 229, 248); + // Relative Luminance: 0.7898468477881603 + final Color background = new Color(226, 229, 248); - // Contrast Ratio: 4.878363954846178 - final double contrastRatio = algorithmImpl.getContrastRatio(foreground, background); + // Contrast Ratio: 4.878363954846178 + final double contrastRatio = algorithmImpl.getContrastRatio(foreground, background); - System.out.println(contrastRatio); - } + System.out.println(contrastRatio); + } - /** - * Calculates the contrast ratio between two given colors. - * - * @param a Any color, used to get the red, green, and blue values. - * @param b Another color, which will be compared against the first color. - * @return The contrast ratio between the two colors. - */ - public double getContrastRatio(Color a, Color b) { - final double aColorLuminance = getRelativeLuminance(a); - final double bColorLuminance = getRelativeLuminance(b); + /** + * Calculates the contrast ratio between two given colors. + * + * @param a Any color, used to get the red, green, and blue values. + * @param b Another color, which will be compared against the first color. + * @return The contrast ratio between the two colors. + */ + public double getContrastRatio(Color a, Color b) { + final double aColorLuminance = getRelativeLuminance(a); + final double bColorLuminance = getRelativeLuminance(b); - if (aColorLuminance > bColorLuminance) - return (aColorLuminance + 0.05) / (bColorLuminance + 0.05); - else - return (bColorLuminance + 0.05) / (aColorLuminance + 0.05); - } + if (aColorLuminance > bColorLuminance) + return (aColorLuminance + 0.05) / (bColorLuminance + 0.05); + else return (bColorLuminance + 0.05) / (aColorLuminance + 0.05); + } - /** - * 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. - */ - public double getRelativeLuminance(Color color) { - final double red = getColor(color.getRed()); - final double green = getColor(color.getGreen()); - final double blue = getColor(color.getBlue()); + /** + * 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. + */ + public double getRelativeLuminance(Color color) { + final double red = getColor(color.getRed()); + final double green = getColor(color.getGreen()); + final double blue = getColor(color.getBlue()); - return 0.2126 * red + 0.7152 * green + 0.0722 * blue; - } + return 0.2126 * red + 0.7152 * green + 0.0722 * blue; + } - /** - * Calculates the final value for a color to be used in the - * relative luminance formula as described in step 1. - * - * @param color8Bit The 8-bit repretation of a color component value. - * @return Value for the provided color component to be used in the relative luminance formula. - */ - public double getColor(int value) { - final double sRgb = getColorSRgb(value); - return (sRgb <= 0.03928) ? sRgb / 12.92 : Math.pow((sRgb + 0.055) / 1.055, 2.4); - } + /** + * Calculates the final value for a color to be used in the relative luminance formula as + * described in step 1. + * + * @param color8Bit The 8-bit repretation of a color component value. + * @return Value for the provided color component to be used in the relative luminance formula. + */ + public double getColor(int value) { + final double sRgb = getColorSRgb(value); + return (sRgb <= 0.03928) ? sRgb / 12.92 : Math.pow((sRgb + 0.055) / 1.055, 2.4); + } - /** - * Calculates the Color sRGB value as denoted in step 1 - * of the procedure document. - * - * @param color8Bit The 8-bit repretation of a color component value. - * @return A percentile value of the color component. - */ - private double getColorSRgb(double color8Bit) { - return color8Bit / 255.0; - } + /** + * Calculates the Color sRGB value as denoted in step 1 of the procedure document. + * + * @param color8Bit The 8-bit repretation of a color component value. + * @return A percentile value of the color component. + */ + private double getColorSRgb(double color8Bit) { + return color8Bit / 255.0; + } } From 7823da44adbd6af43a54639703974c986a93fc0d Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sat, 27 Feb 2021 07:17:18 +0100 Subject: [PATCH 05/15] Add imports for ColorContrastRatio.java --- Misc/ColorContrastRatio.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index 4da10c1e1737..e52099aaa80c 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -1,5 +1,7 @@ package Misc; +import java.awt.Color; + /** * A Java implementation of the offcial W3 documented procedure * to calculate contrast ratio between colors on the web. From baa4f3da59e3215b2e5abb7e13b51a62a0da90d1 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 27 Feb 2021 06:18:15 +0000 Subject: [PATCH 06/15] updating DIRECTORY.md --- DIRECTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index ce1488681675..457a5d912792 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -129,9 +129,14 @@ * [Average](https://github.com/TheAlgorithms/Java/blob/master/Maths/Average.java) * [BinaryPow](https://github.com/TheAlgorithms/Java/blob/master/Maths/BinaryPow.java) * [Ceil](https://github.com/TheAlgorithms/Java/blob/master/Maths/Ceil.java) + * [CircularConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/CircularConvolutionFFT.java) * [Combinations](https://github.com/TheAlgorithms/Java/blob/master/Maths/Combinations.java) + * [Convolution](https://github.com/TheAlgorithms/Java/blob/master/Maths/Convolution.java) + * [ConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/ConvolutionFFT.java) * [Factorial](https://github.com/TheAlgorithms/Java/blob/master/Maths/Factorial.java) * [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FactorialRecursion.java) + * [FFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/FFT.java) + * [FFTBluestein](https://github.com/TheAlgorithms/Java/blob/master/Maths/FFTBluestein.java) * [FibonacciNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/FibonacciNumber.java) * [FindMax](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMax.java) * [FindMaxRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMaxRecursion.java) @@ -165,6 +170,7 @@ * [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/MinimizingLateness/MinimizingLateness.java) ## Misc + * [ColorContrastRatio](https://github.com/TheAlgorithms/Java/blob/master/Misc/ColorContrastRatio.java) * [MedianOfRunningArray](https://github.com/TheAlgorithms/Java/blob/master/Misc/MedianOfRunningArray.java) * [PalindromePrime](https://github.com/TheAlgorithms/Java/blob/master/Misc/PalindromePrime.java) * [RangeInSortedArray](https://github.com/TheAlgorithms/Java/blob/master/Misc/RangeInSortedArray.java) From abb12db0de16240ad502d911bc85ea7bf3fce024 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sat, 27 Feb 2021 07:34:54 +0100 Subject: [PATCH 07/15] Updated to follow repo conventions. Undid formatting of documents. --- DIRECTORY.md | 6 ------ Maths/ConvolutionFFT.java | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 457a5d912792..ce1488681675 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -129,14 +129,9 @@ * [Average](https://github.com/TheAlgorithms/Java/blob/master/Maths/Average.java) * [BinaryPow](https://github.com/TheAlgorithms/Java/blob/master/Maths/BinaryPow.java) * [Ceil](https://github.com/TheAlgorithms/Java/blob/master/Maths/Ceil.java) - * [CircularConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/CircularConvolutionFFT.java) * [Combinations](https://github.com/TheAlgorithms/Java/blob/master/Maths/Combinations.java) - * [Convolution](https://github.com/TheAlgorithms/Java/blob/master/Maths/Convolution.java) - * [ConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/ConvolutionFFT.java) * [Factorial](https://github.com/TheAlgorithms/Java/blob/master/Maths/Factorial.java) * [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FactorialRecursion.java) - * [FFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/FFT.java) - * [FFTBluestein](https://github.com/TheAlgorithms/Java/blob/master/Maths/FFTBluestein.java) * [FibonacciNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/FibonacciNumber.java) * [FindMax](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMax.java) * [FindMaxRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMaxRecursion.java) @@ -170,7 +165,6 @@ * [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/MinimizingLateness/MinimizingLateness.java) ## Misc - * [ColorContrastRatio](https://github.com/TheAlgorithms/Java/blob/master/Misc/ColorContrastRatio.java) * [MedianOfRunningArray](https://github.com/TheAlgorithms/Java/blob/master/Misc/MedianOfRunningArray.java) * [PalindromePrime](https://github.com/TheAlgorithms/Java/blob/master/Misc/PalindromePrime.java) * [RangeInSortedArray](https://github.com/TheAlgorithms/Java/blob/master/Misc/RangeInSortedArray.java) diff --git a/Maths/ConvolutionFFT.java b/Maths/ConvolutionFFT.java index f1d595087142..49032ae83578 100644 --- a/Maths/ConvolutionFFT.java +++ b/Maths/ConvolutionFFT.java @@ -53,7 +53,7 @@ public static ArrayList convolutionFFT( convolved .subList(convolvedSize, convolved.size()) .clear(); // Remove the remaining zeros after the convolvedSize. These extra zeros came from - // paddingPowerOfTwo() method inside the fft() method. + // paddingPowerOfTwo() method inside the fft() method. return convolved; } From 4ecf12ceea9c95b932913bcb9fe03c9cf8fa7ef6 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sat, 27 Feb 2021 07:35:12 +0100 Subject: [PATCH 08/15] Follow repository conventions. --- Misc/ColorContrastRatio.java | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index eef5bfc3185d..f211330f7f25 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -3,26 +3,20 @@ import java.awt.Color; /** - * A Java implementation of the offcial W3 documented procedure to 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. + * @brief A Java implementation of the offcial W3 documented procedure to 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 - * Procedure - * @author Seth Falco (https://elypia.org/) + * @see [Color Contrast Ratio](https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure) + * @author [Seth Falco](https://github.com/SethFalco) */ public class ColorContrastRatio { /** * You can check this example against another open-source implementation available on GitHub. * - * @see Online - * Contrast Ratio - * @see GitHub Repository for Online Contrast - * Ratio + * @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) * @param args */ public static void main(String args[]) { @@ -41,7 +35,7 @@ public static void main(String args[]) { } /** - * Calculates the contrast ratio between two given colors. + * @brief Calculates the contrast ratio between two given colors. * * @param a Any color, used to get the red, green, and blue values. * @param b Another color, which will be compared against the first color. @@ -57,12 +51,11 @@ public double getContrastRatio(Color a, Color b) { } /** - * Calculates the relative luminance of a given color. + * @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. + * @see [More info on relative luminance.](https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef) */ public double getRelativeLuminance(Color color) { final double red = getColor(color.getRed()); @@ -73,7 +66,7 @@ public double getRelativeLuminance(Color color) { } /** - * Calculates the final value for a color to be used in the relative luminance formula as + * @brief Calculates the final value for a color to be used in the relative luminance formula as * described in step 1. * * @param color8Bit The 8-bit repretation of a color component value. @@ -85,7 +78,7 @@ public double getColor(int value) { } /** - * 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 The 8-bit repretation of a color component value. * @return A percentile value of the color component. From caf723c75be3b34789e2f783231fa59f4722a910 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 27 Feb 2021 06:36:04 +0000 Subject: [PATCH 09/15] Formatted with Google Java Formatter --- Maths/ConvolutionFFT.java | 2 +- Misc/ColorContrastRatio.java | 18 ++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Maths/ConvolutionFFT.java b/Maths/ConvolutionFFT.java index 49032ae83578..f1d595087142 100644 --- a/Maths/ConvolutionFFT.java +++ b/Maths/ConvolutionFFT.java @@ -53,7 +53,7 @@ public static ArrayList convolutionFFT( convolved .subList(convolvedSize, convolved.size()) .clear(); // Remove the remaining zeros after the convolvedSize. These extra zeros came from - // paddingPowerOfTwo() method inside the fft() method. + // paddingPowerOfTwo() method inside the fft() method. return convolved; } diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index f211330f7f25..1066c1836d88 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -3,9 +3,9 @@ import java.awt.Color; /** - * @brief A Java implementation of the offcial W3 documented procedure to 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. - * + * @brief A Java implementation of the offcial W3 documented procedure to 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) * @author [Seth Falco](https://github.com/SethFalco) @@ -15,7 +15,8 @@ public class ColorContrastRatio { /** * 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 [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) * @param args */ @@ -36,7 +37,6 @@ public static void main(String args[]) { /** * @brief Calculates the contrast ratio between two given colors. - * * @param a Any color, used to get the red, green, and blue values. * @param b Another color, which will be compared against the first color. * @return The contrast ratio between the two colors. @@ -52,10 +52,10 @@ 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.](https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef) */ public double getRelativeLuminance(Color color) { final double red = getColor(color.getRed()); @@ -67,8 +67,7 @@ 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. - * + * described in step 1. * @param color8Bit The 8-bit repretation of a color component value. * @return Value for the provided color component to be used in the relative luminance formula. */ @@ -79,7 +78,6 @@ public double getColor(int value) { /** * @brief Calculates the Color sRGB value as denoted in step 1 of the procedure document. - * * @param color8Bit The 8-bit repretation of a color component value. * @return A percentile value of the color component. */ From 43af91404d78bab59153d73d5daacbcd1c7cfbab Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sat, 27 Feb 2021 15:34:45 +0100 Subject: [PATCH 10/15] Added test method with assetions. --- Misc/ColorContrastRatio.java | 58 ++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index f211330f7f25..269ca7ae9b7a 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -12,28 +12,6 @@ */ public class ColorContrastRatio { - /** - * 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) - * @param args - */ - public static void main(String args[]) { - final ColorContrastRatio algorithmImpl = new ColorContrastRatio(); - - // Relative Luminance: 0.12215748057375966 - final Color foreground = new Color(23, 103, 154); - - // Relative Luminance: 0.7898468477881603 - final Color background = new Color(226, 229, 248); - - // Contrast Ratio: 4.878363954846178 - final double contrastRatio = algorithmImpl.getContrastRatio(foreground, background); - - System.out.println(contrastRatio); - } - /** * @brief Calculates the contrast ratio between two given colors. * @@ -86,4 +64,40 @@ public double getColor(int value) { private double getColorSRgb(double color8Bit) { return color8Bit / 255.0; } + + /** + * You can check these examples 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(); + } } From 644fb3068ac55ec38b2dc2e90d7a5c97c8a9924e Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 27 Feb 2021 14:38:29 +0000 Subject: [PATCH 11/15] Formatted with Google Java Formatter --- Misc/ColorContrastRatio.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index ed4da758af96..622afda21187 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -85,11 +85,13 @@ private static void test() { final Color foreground = new Color(23, 103, 154); final double foregroundLuminance = algImpl.getRelativeLuminance(foreground); - assert foregroundLuminance == 0.12215748057375966 : "Test 4 Failed - Incorrect relative luminance."; + 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."; + 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."; From 9dfc02bd998f2814fc45d26140dbf1dd331ad289 Mon Sep 17 00:00:00 2001 From: Seth Date: Sat, 27 Feb 2021 16:35:40 +0100 Subject: [PATCH 12/15] Update Misc/ColorContrastRatio.java Co-authored-by: Ayaan Khan --- Misc/ColorContrastRatio.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index 622afda21187..e0f025509207 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -24,7 +24,7 @@ public double getContrastRatio(Color a, Color b) { if (aColorLuminance > bColorLuminance) return (aColorLuminance + 0.05) / (bColorLuminance + 0.05); - else return (bColorLuminance + 0.05) / (aColorLuminance + 0.05); + return (bColorLuminance + 0.05) / (aColorLuminance + 0.05); } /** From d0cc637e1137b9d930f98c3c9490e0be79a8c8eb Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 27 Feb 2021 15:36:05 +0000 Subject: [PATCH 13/15] updating DIRECTORY.md --- DIRECTORY.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index ce1488681675..457a5d912792 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -129,9 +129,14 @@ * [Average](https://github.com/TheAlgorithms/Java/blob/master/Maths/Average.java) * [BinaryPow](https://github.com/TheAlgorithms/Java/blob/master/Maths/BinaryPow.java) * [Ceil](https://github.com/TheAlgorithms/Java/blob/master/Maths/Ceil.java) + * [CircularConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/CircularConvolutionFFT.java) * [Combinations](https://github.com/TheAlgorithms/Java/blob/master/Maths/Combinations.java) + * [Convolution](https://github.com/TheAlgorithms/Java/blob/master/Maths/Convolution.java) + * [ConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/ConvolutionFFT.java) * [Factorial](https://github.com/TheAlgorithms/Java/blob/master/Maths/Factorial.java) * [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FactorialRecursion.java) + * [FFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/FFT.java) + * [FFTBluestein](https://github.com/TheAlgorithms/Java/blob/master/Maths/FFTBluestein.java) * [FibonacciNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/FibonacciNumber.java) * [FindMax](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMax.java) * [FindMaxRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMaxRecursion.java) @@ -165,6 +170,7 @@ * [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/MinimizingLateness/MinimizingLateness.java) ## Misc + * [ColorContrastRatio](https://github.com/TheAlgorithms/Java/blob/master/Misc/ColorContrastRatio.java) * [MedianOfRunningArray](https://github.com/TheAlgorithms/Java/blob/master/Misc/MedianOfRunningArray.java) * [PalindromePrime](https://github.com/TheAlgorithms/Java/blob/master/Misc/PalindromePrime.java) * [RangeInSortedArray](https://github.com/TheAlgorithms/Java/blob/master/Misc/RangeInSortedArray.java) From f851223a4ffddebffd25f313308201a1b7abd3d7 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sat, 27 Feb 2021 16:45:01 +0100 Subject: [PATCH 14/15] Correct javadocs and parameter names. --- Misc/ColorContrastRatio.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index e0f025509207..a923c92f6384 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -24,6 +24,7 @@ public double getContrastRatio(Color a, Color b) { if (aColorLuminance > bColorLuminance) return (aColorLuminance + 0.05) / (bColorLuminance + 0.05); + return (bColorLuminance + 0.05) / (aColorLuminance + 0.05); } @@ -45,17 +46,17 @@ 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. - * @param color8Bit The 8-bit repretation of a color component value. + * @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. */ - public double getColor(int value) { - final double sRgb = getColorSRgb(value); + public double getColor(int color8Bit) { + final double sRgb = getColorSRgb(color8Bit); return (sRgb <= 0.03928) ? sRgb / 12.92 : Math.pow((sRgb + 0.055) / 1.055, 2.4); } /** * @brief Calculates the Color sRGB value as denoted in step 1 of the procedure document. - * @param color8Bit The 8-bit repretation of a color component value. + * @param color8Bit 8-bit representation of a color component value. * @return A percentile value of the color component. */ private double getColorSRgb(double color8Bit) { From 046c81d23d23426b49151a2230dad656309e3e47 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 27 Feb 2021 15:45:49 +0000 Subject: [PATCH 15/15] Formatted with Google Java Formatter --- Misc/ColorContrastRatio.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/ColorContrastRatio.java b/Misc/ColorContrastRatio.java index a923c92f6384..b3dcf12267cb 100644 --- a/Misc/ColorContrastRatio.java +++ b/Misc/ColorContrastRatio.java @@ -24,7 +24,7 @@ public double getContrastRatio(Color a, Color b) { if (aColorLuminance > bColorLuminance) return (aColorLuminance + 0.05) / (bColorLuminance + 0.05); - + return (bColorLuminance + 0.05) / (aColorLuminance + 0.05); }