-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6308 from Jnction/gtfs-text-luminance
Use WCAG recommendation to fill in GTFS route text color if it is missing
- Loading branch information
Showing
5 changed files
with
98 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
utils/src/main/java/org/opentripplanner/utils/color/Brightness.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.opentripplanner.utils.color; | ||
|
||
public enum Brightness { | ||
DARK, | ||
LIGHT, | ||
} |
43 changes: 43 additions & 0 deletions
43
utils/src/main/java/org/opentripplanner/utils/color/ColorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.opentripplanner.utils.color; | ||
|
||
import java.awt.Color; | ||
|
||
public final class ColorUtils { | ||
|
||
private ColorUtils() {} | ||
|
||
/** | ||
* Calculates luminance according to | ||
* <a href="https://www.w3.org/TR/WCAG21/#dfn-relative-luminance">W3C Recommendation</a> | ||
*/ | ||
public static double computeLuminance(Color color) { | ||
//gets float of RED, GREEN, BLUE in range 0...1 | ||
float[] colorComponents = color.getRGBColorComponents(null); | ||
double r = linearizeColorComponent(colorComponents[0]); | ||
double g = linearizeColorComponent(colorComponents[1]); | ||
double b = linearizeColorComponent(colorComponents[2]); | ||
return 0.2126 * r + 0.7152 * g + 0.0722 * b; | ||
} | ||
|
||
private static double linearizeColorComponent(double srgb) { | ||
return srgb <= 0.04045 ? srgb / 12.92 : Math.pow((srgb + 0.055) / 1.055, 2.4); | ||
} | ||
|
||
/** | ||
* Determine if a color is light or dark | ||
* <p> | ||
* A light color is a color where the contrast ratio with black is larger than with white. | ||
* <p> | ||
* The contrast ratio is defined per Web Content Accessibility Guidelines (WCAG) 2.1. | ||
*/ | ||
public static Brightness computeBrightness(Color color) { | ||
// The contrast ratio between two colors is defined as (L1 + 0.05) / (L2 + 0.05) | ||
// where L1 is the lighter of the two colors. | ||
// | ||
// Therefore, the contrast ratio with black is (L + 0.05) / 0.05 and the contrast ratio with | ||
// white is 1.05 / (L + 0.05) | ||
// | ||
// Solving (L + 0.05) / 0.05 > 1.05 / (L + 0.05) gets L > 0.179 | ||
return computeLuminance(color) > 0.179 ? Brightness.LIGHT : Brightness.DARK; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
utils/src/test/java/org/opentripplanner/utils/color/ColorUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.opentripplanner.utils.color; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
import static org.opentripplanner.utils.color.ColorUtils.computeBrightness; | ||
|
||
import java.awt.Color; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class ColorUtilsTest { | ||
|
||
private static Stream<Arguments> brightnessExpectations() { | ||
return Stream.of( | ||
arguments(Color.black, Brightness.DARK), | ||
arguments(Color.green, Brightness.LIGHT), | ||
arguments(Color.blue, Brightness.DARK), | ||
arguments(Color.red, Brightness.LIGHT), | ||
arguments(Color.yellow, Brightness.LIGHT), | ||
arguments(Color.white, Brightness.LIGHT), | ||
arguments(Color.pink, Brightness.LIGHT), | ||
arguments(Color.orange, Brightness.LIGHT), | ||
arguments(Color.cyan, Brightness.LIGHT) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("brightnessExpectations") | ||
void testBrightness(Color color, Brightness brightness) { | ||
assertEquals(computeBrightness(color), brightness); | ||
} | ||
} |