Skip to content

Commit b4d104d

Browse files
SethFalcogithub-actionsayaankhan98
authored
Added algorithm for color contrast ratio. (TheAlgorithms#1794)
* Added algorithm for color contrast ratio. * Added comment and example usage in main. * Fixed calling method in main without creating object. * Formatted with Google Java Formatter * Add imports for ColorContrastRatio.java * updating DIRECTORY.md * Updated to follow repo conventions. Undid formatting of documents. * Follow repository conventions. * Formatted with Google Java Formatter * Added test method with assetions. * Formatted with Google Java Formatter * Update Misc/ColorContrastRatio.java Co-authored-by: Ayaan Khan <ayaankhan98@gmail.com> * updating DIRECTORY.md * Correct javadocs and parameter names. * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Ayaan Khan <ayaankhan98@gmail.com>
1 parent ca2e207 commit b4d104d

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

DIRECTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,14 @@
129129
* [Average](https://github.com/TheAlgorithms/Java/blob/master/Maths/Average.java)
130130
* [BinaryPow](https://github.com/TheAlgorithms/Java/blob/master/Maths/BinaryPow.java)
131131
* [Ceil](https://github.com/TheAlgorithms/Java/blob/master/Maths/Ceil.java)
132+
* [CircularConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/CircularConvolutionFFT.java)
132133
* [Combinations](https://github.com/TheAlgorithms/Java/blob/master/Maths/Combinations.java)
134+
* [Convolution](https://github.com/TheAlgorithms/Java/blob/master/Maths/Convolution.java)
135+
* [ConvolutionFFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/ConvolutionFFT.java)
133136
* [Factorial](https://github.com/TheAlgorithms/Java/blob/master/Maths/Factorial.java)
134137
* [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FactorialRecursion.java)
138+
* [FFT](https://github.com/TheAlgorithms/Java/blob/master/Maths/FFT.java)
139+
* [FFTBluestein](https://github.com/TheAlgorithms/Java/blob/master/Maths/FFTBluestein.java)
135140
* [FibonacciNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/FibonacciNumber.java)
136141
* [FindMax](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMax.java)
137142
* [FindMaxRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMaxRecursion.java)
@@ -165,6 +170,7 @@
165170
* [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/MinimizingLateness/MinimizingLateness.java)
166171

167172
## Misc
173+
* [ColorContrastRatio](https://github.com/TheAlgorithms/Java/blob/master/Misc/ColorContrastRatio.java)
168174
* [MedianOfRunningArray](https://github.com/TheAlgorithms/Java/blob/master/Misc/MedianOfRunningArray.java)
169175
* [PalindromePrime](https://github.com/TheAlgorithms/Java/blob/master/Misc/PalindromePrime.java)
170176
* [RangeInSortedArray](https://github.com/TheAlgorithms/Java/blob/master/Misc/RangeInSortedArray.java)

Maths/ConvolutionFFT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static ArrayList<FFT.Complex> convolutionFFT(
5353
convolved
5454
.subList(convolvedSize, convolved.size())
5555
.clear(); // Remove the remaining zeros after the convolvedSize. These extra zeros came from
56-
// paddingPowerOfTwo() method inside the fft() method.
56+
// paddingPowerOfTwo() method inside the fft() method.
5757

5858
return convolved;
5959
}

Misc/ColorContrastRatio.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package Misc;
2+
3+
import java.awt.Color;
4+
5+
/**
6+
* @brief A Java implementation of the offcial W3 documented procedure to calculate contrast ratio
7+
* between colors on the web. This is used to calculate the readability of a foreground color on
8+
* top of a background color.
9+
* @since 2020-10-15
10+
* @see [Color Contrast Ratio](https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure)
11+
* @author [Seth Falco](https://github.com/SethFalco)
12+
*/
13+
public class ColorContrastRatio {
14+
15+
/**
16+
* @brief Calculates the contrast ratio between two given colors.
17+
* @param a Any color, used to get the red, green, and blue values.
18+
* @param b Another color, which will be compared against the first color.
19+
* @return The contrast ratio between the two colors.
20+
*/
21+
public double getContrastRatio(Color a, Color b) {
22+
final double aColorLuminance = getRelativeLuminance(a);
23+
final double bColorLuminance = getRelativeLuminance(b);
24+
25+
if (aColorLuminance > bColorLuminance)
26+
return (aColorLuminance + 0.05) / (bColorLuminance + 0.05);
27+
28+
return (bColorLuminance + 0.05) / (aColorLuminance + 0.05);
29+
}
30+
31+
/**
32+
* @brief Calculates the relative luminance of a given color.
33+
* @param color Any color, used to get the red, green, and blue values.
34+
* @return The relative luminance of the color.
35+
* @see [More info on relative
36+
* luminance.](https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef)
37+
*/
38+
public double getRelativeLuminance(Color color) {
39+
final double red = getColor(color.getRed());
40+
final double green = getColor(color.getGreen());
41+
final double blue = getColor(color.getBlue());
42+
43+
return 0.2126 * red + 0.7152 * green + 0.0722 * blue;
44+
}
45+
46+
/**
47+
* @brief Calculates the final value for a color to be used in the relative luminance formula as
48+
* described in step 1.
49+
* @param color8Bit 8-bit representation of a color component value.
50+
* @return Value for the provided color component to be used in the relative luminance formula.
51+
*/
52+
public double getColor(int color8Bit) {
53+
final double sRgb = getColorSRgb(color8Bit);
54+
return (sRgb <= 0.03928) ? sRgb / 12.92 : Math.pow((sRgb + 0.055) / 1.055, 2.4);
55+
}
56+
57+
/**
58+
* @brief Calculates the Color sRGB value as denoted in step 1 of the procedure document.
59+
* @param color8Bit 8-bit representation of a color component value.
60+
* @return A percentile value of the color component.
61+
*/
62+
private double getColorSRgb(double color8Bit) {
63+
return color8Bit / 255.0;
64+
}
65+
66+
/**
67+
* You can check this example against another open-source implementation available on GitHub.
68+
*
69+
* @see [Online Contrast
70+
* Ratio](https://contrast-ratio.com/#rgb%28226%2C%20229%2C%20248-on-rgb%2823%2C%20103%2C%20154%29)
71+
* @see [GitHub Repository for Online Contrast Ratio](https://github.com/LeaVerou/contrast-ratio)
72+
*/
73+
private static void test() {
74+
final ColorContrastRatio algImpl = new ColorContrastRatio();
75+
76+
final Color black = Color.BLACK;
77+
final double blackLuminance = algImpl.getRelativeLuminance(black);
78+
assert blackLuminance == 0 : "Test 1 Failed - Incorrect relative luminance.";
79+
80+
final Color white = Color.WHITE;
81+
final double whiteLuminance = algImpl.getRelativeLuminance(white);
82+
assert whiteLuminance == 1 : "Test 2 Failed - Incorrect relative luminance.";
83+
84+
final double highestColorRatio = algImpl.getContrastRatio(black, white);
85+
assert highestColorRatio == 21 : "Test 3 Failed - Incorrect contrast ratio.";
86+
87+
final Color foreground = new Color(23, 103, 154);
88+
final double foregroundLuminance = algImpl.getRelativeLuminance(foreground);
89+
assert foregroundLuminance == 0.12215748057375966
90+
: "Test 4 Failed - Incorrect relative luminance.";
91+
92+
final Color background = new Color(226, 229, 248);
93+
final double backgroundLuminance = algImpl.getRelativeLuminance(background);
94+
assert backgroundLuminance == 0.7898468477881603
95+
: "Test 5 Failed - Incorrect relative luminance.";
96+
97+
final double contrastRatio = algImpl.getContrastRatio(foreground, background);
98+
assert contrastRatio == 4.878363954846178 : "Test 6 Failed - Incorrect contrast ratio.";
99+
}
100+
101+
public static void main(String args[]) {
102+
test();
103+
}
104+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy