forked from opentripplanner/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
12 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
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, | ||
} |
44 changes: 44 additions & 0 deletions
44
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,44 @@ | ||
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); | ||
//Calculates luminance based on https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color | ||
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
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 org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.FieldSource; | ||
|
||
public class ColorUtilsTest { | ||
|
||
static final Arguments[] brightnessExpectations = { | ||
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 | ||
@FieldSource("brightnessExpectations") | ||
void testBrightness(Color color, Brightness brightness) { | ||
assertEquals(computeBrightness(color), brightness); | ||
} | ||
} |