Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb4d104d

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.javaCo-authored-by: Ayaan Khan <ayaankhan98@gmail.com>* updating DIRECTORY.md* Correct javadocs and parameter names.* Formatted with Google Java FormatterCo-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>Co-authored-by: Ayaan Khan <ayaankhan98@gmail.com>
1 parentca2e207 commitb4d104d

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
returnconvolved;
5959
}

‎Misc/ColorContrastRatio.java‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
packageMisc;
2+
3+
importjava.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+
publicclassColorContrastRatio {
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+
publicdoublegetContrastRatio(Colora,Colorb) {
22+
finaldoubleaColorLuminance =getRelativeLuminance(a);
23+
finaldoublebColorLuminance =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+
publicdoublegetRelativeLuminance(Colorcolor) {
39+
finaldoublered =getColor(color.getRed());
40+
finaldoublegreen =getColor(color.getGreen());
41+
finaldoubleblue =getColor(color.getBlue());
42+
43+
return0.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+
publicdoublegetColor(intcolor8Bit) {
53+
finaldoublesRgb =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+
privatedoublegetColorSRgb(doublecolor8Bit) {
63+
returncolor8Bit /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+
privatestaticvoidtest() {
74+
finalColorContrastRatioalgImpl =newColorContrastRatio();
75+
76+
finalColorblack =Color.BLACK;
77+
finaldoubleblackLuminance =algImpl.getRelativeLuminance(black);
78+
assertblackLuminance ==0 :"Test 1 Failed - Incorrect relative luminance.";
79+
80+
finalColorwhite =Color.WHITE;
81+
finaldoublewhiteLuminance =algImpl.getRelativeLuminance(white);
82+
assertwhiteLuminance ==1 :"Test 2 Failed - Incorrect relative luminance.";
83+
84+
finaldoublehighestColorRatio =algImpl.getContrastRatio(black,white);
85+
asserthighestColorRatio ==21 :"Test 3 Failed - Incorrect contrast ratio.";
86+
87+
finalColorforeground =newColor(23,103,154);
88+
finaldoubleforegroundLuminance =algImpl.getRelativeLuminance(foreground);
89+
assertforegroundLuminance ==0.12215748057375966
90+
:"Test 4 Failed - Incorrect relative luminance.";
91+
92+
finalColorbackground =newColor(226,229,248);
93+
finaldoublebackgroundLuminance =algImpl.getRelativeLuminance(background);
94+
assertbackgroundLuminance ==0.7898468477881603
95+
:"Test 5 Failed - Incorrect relative luminance.";
96+
97+
finaldoublecontrastRatio =algImpl.getContrastRatio(foreground,background);
98+
assertcontrastRatio ==4.878363954846178 :"Test 6 Failed - Incorrect contrast ratio.";
99+
}
100+
101+
publicstaticvoidmain(Stringargs[]) {
102+
test();
103+
}
104+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp