Skip to content

Commit a65f278

Browse files
author
Seth Falco
committed
Merge branch 'color-contrast' of https://github.com/SethiPandi/Java into color-contrast
2 parents 7823da4 + 0e90554 commit a65f278

File tree

2 files changed

+74
-72
lines changed

2 files changed

+74
-72
lines changed

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: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,92 +3,94 @@
33
import java.awt.Color;
44

55
/**
6-
* A Java implementation of the offcial W3 documented procedure
7-
* to calculate contrast ratio between colors on the web.
6+
* A Java implementation of the offcial W3 documented procedure to calculate contrast ratio between
7+
* colors on the web.
88
*
9-
* This is used to calculate the readability of a foreground color
10-
* on top of a background color.
9+
* <p>This is used to calculate the readability of a foreground color on top of a background color.
1110
*
1211
* @since 2020-10-15
13-
* @see <a href="https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure">Color Contrast Ratio Procedure</a>
12+
* @see <a href="https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-procedure">Color Contrast Ratio
13+
* Procedure</a>
1414
* @author Seth Falco (https://elypia.org/)
1515
*/
1616
public class ColorContrastRatio {
1717

18-
/**
19-
* You can check this example against another open-source implementation available on GitHub.
20-
*
21-
* @see <a href="https://contrast-ratio.com/#rgb%28226%2C%20229%2C%20248-on-rgb%2823%2C%20103%2C%20154%29">Online Contrast Ratio</a>
22-
* @see <a href="https://github.com/LeaVerou/contrast-ratio">GitHub Repository for Online Contrast Ratio</a>
23-
* @param args
24-
*/
25-
public static void main(String args[]) {
26-
final ColorContrastRatio algorithmImpl = new ColorContrastRatio();
18+
/**
19+
* You can check this example against another open-source implementation available on GitHub.
20+
*
21+
* @see <a
22+
* href="https://contrast-ratio.com/#rgb%28226%2C%20229%2C%20248-on-rgb%2823%2C%20103%2C%20154%29">Online
23+
* Contrast Ratio</a>
24+
* @see <a href="https://github.com/LeaVerou/contrast-ratio">GitHub Repository for Online Contrast
25+
* Ratio</a>
26+
* @param args
27+
*/
28+
public static void main(String args[]) {
29+
final ColorContrastRatio algorithmImpl = new ColorContrastRatio();
2730

28-
// Relative Luminance: 0.12215748057375966
29-
final Color foreground = new Color(23, 103, 154);
31+
// Relative Luminance: 0.12215748057375966
32+
final Color foreground = new Color(23, 103, 154);
3033

31-
// Relative Luminance: 0.7898468477881603
32-
final Color background = new Color(226, 229, 248);
34+
// Relative Luminance: 0.7898468477881603
35+
final Color background = new Color(226, 229, 248);
3336

34-
// Contrast Ratio: 4.878363954846178
35-
final double contrastRatio = algorithmImpl.getContrastRatio(foreground, background);
37+
// Contrast Ratio: 4.878363954846178
38+
final double contrastRatio = algorithmImpl.getContrastRatio(foreground, background);
3639

37-
System.out.println(contrastRatio);
38-
}
40+
System.out.println(contrastRatio);
41+
}
3942

40-
/**
41-
* Calculates the contrast ratio between two given colors.
42-
*
43-
* @param a Any color, used to get the red, green, and blue values.
44-
* @param b Another color, which will be compared against the first color.
45-
* @return The contrast ratio between the two colors.
46-
*/
47-
public double getContrastRatio(Color a, Color b) {
48-
final double aColorLuminance = getRelativeLuminance(a);
49-
final double bColorLuminance = getRelativeLuminance(b);
43+
/**
44+
* Calculates the contrast ratio between two given colors.
45+
*
46+
* @param a Any color, used to get the red, green, and blue values.
47+
* @param b Another color, which will be compared against the first color.
48+
* @return The contrast ratio between the two colors.
49+
*/
50+
public double getContrastRatio(Color a, Color b) {
51+
final double aColorLuminance = getRelativeLuminance(a);
52+
final double bColorLuminance = getRelativeLuminance(b);
5053

51-
if (aColorLuminance > bColorLuminance)
52-
return (aColorLuminance + 0.05) / (bColorLuminance + 0.05);
53-
else
54-
return (bColorLuminance + 0.05) / (aColorLuminance + 0.05);
55-
}
54+
if (aColorLuminance > bColorLuminance)
55+
return (aColorLuminance + 0.05) / (bColorLuminance + 0.05);
56+
else return (bColorLuminance + 0.05) / (aColorLuminance + 0.05);
57+
}
5658

57-
/**
58-
* Calculates the relative luminance of a given color.
59-
*
60-
* @param color Any color, used to get the red, green, and blue values.
61-
* @return The relative luminance of the color.
62-
* @see <a href="https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef">More info on relative luminance.</a>
63-
*/
64-
public double getRelativeLuminance(Color color) {
65-
final double red = getColor(color.getRed());
66-
final double green = getColor(color.getGreen());
67-
final double blue = getColor(color.getBlue());
59+
/**
60+
* Calculates the relative luminance of a given color.
61+
*
62+
* @param color Any color, used to get the red, green, and blue values.
63+
* @return The relative luminance of the color.
64+
* @see <a href="https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef">More info
65+
* on relative luminance.</a>
66+
*/
67+
public double getRelativeLuminance(Color color) {
68+
final double red = getColor(color.getRed());
69+
final double green = getColor(color.getGreen());
70+
final double blue = getColor(color.getBlue());
6871

69-
return 0.2126 * red + 0.7152 * green + 0.0722 * blue;
70-
}
72+
return 0.2126 * red + 0.7152 * green + 0.0722 * blue;
73+
}
7174

72-
/**
73-
* Calculates the final value for a color to be used in the
74-
* relative luminance formula as described in step 1.
75-
*
76-
* @param color8Bit The 8-bit repretation of a color component value.
77-
* @return Value for the provided color component to be used in the relative luminance formula.
78-
*/
79-
public double getColor(int value) {
80-
final double sRgb = getColorSRgb(value);
81-
return (sRgb <= 0.03928) ? sRgb / 12.92 : Math.pow((sRgb + 0.055) / 1.055, 2.4);
82-
}
75+
/**
76+
* Calculates the final value for a color to be used in the relative luminance formula as
77+
* described in step 1.
78+
*
79+
* @param color8Bit The 8-bit repretation of a color component value.
80+
* @return Value for the provided color component to be used in the relative luminance formula.
81+
*/
82+
public double getColor(int value) {
83+
final double sRgb = getColorSRgb(value);
84+
return (sRgb <= 0.03928) ? sRgb / 12.92 : Math.pow((sRgb + 0.055) / 1.055, 2.4);
85+
}
8386

84-
/**
85-
* Calculates the Color sRGB value as denoted in step 1
86-
* of the procedure document.
87-
*
88-
* @param color8Bit The 8-bit repretation of a color component value.
89-
* @return A percentile value of the color component.
90-
*/
91-
private double getColorSRgb(double color8Bit) {
92-
return color8Bit / 255.0;
93-
}
87+
/**
88+
* Calculates the Color sRGB value as denoted in step 1 of the procedure document.
89+
*
90+
* @param color8Bit The 8-bit repretation of a color component value.
91+
* @return A percentile value of the color component.
92+
*/
93+
private double getColorSRgb(double color8Bit) {
94+
return color8Bit / 255.0;
95+
}
9496
}

0 commit comments

Comments
 (0)