|
3 | 3 | import java.awt.Color;
|
4 | 4 |
|
5 | 5 | /**
|
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. |
8 | 8 | *
|
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. |
11 | 10 | *
|
12 | 11 | * @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> |
14 | 14 | * @author Seth Falco (https://elypia.org/)
|
15 | 15 | */
|
16 | 16 | public class ColorContrastRatio {
|
17 | 17 |
|
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(); |
27 | 30 |
|
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); |
30 | 33 |
|
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); |
33 | 36 |
|
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); |
36 | 39 |
|
37 |
| - System.out.println(contrastRatio); |
38 |
| - } |
| 40 | + System.out.println(contrastRatio); |
| 41 | + } |
39 | 42 |
|
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); |
50 | 53 |
|
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 | + } |
56 | 58 |
|
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()); |
68 | 71 |
|
69 |
| - return 0.2126 * red + 0.7152 * green + 0.0722 * blue; |
70 |
| - } |
| 72 | + return 0.2126 * red + 0.7152 * green + 0.0722 * blue; |
| 73 | + } |
71 | 74 |
|
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 | + } |
83 | 86 |
|
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 | + } |
94 | 96 | }
|
0 commit comments