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