-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create requiresLightText util (#311)
* Create requiresLightText util * Add CHANGELOG entry * Add review feedback
- Loading branch information
Showing
5 changed files
with
67 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { isColorDark } from './is_color_dark'; | ||
export { VISUALIZATION_COLORS } from './visualization_colors'; |
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,23 @@ | ||
/** | ||
* This function calculates if the specified color is "dark", which usually means | ||
* you need light text if you use it as a background color to fulfill WCAG contrast | ||
* requirement. | ||
* The color must be specified via its red, green and blue value in the range of | ||
* 0 to 255. | ||
* The formula is based on this Stackoverflow answer: https://stackoverflow.com/a/3943023 | ||
* which itself is based upon the WCAG recommendation for color contrast. | ||
* | ||
* @param {number} red The red component in the range 0 to 255 | ||
* @param {number} green The green component in the range 0 to 255 | ||
* @param {number} blue The blue component in the range 0 to 255 | ||
* @returns {boolean} True if the color is dark, false otherwise. | ||
*/ | ||
function isColorDark(red, green, blue) { | ||
const [r, g, b] = [red, green, blue] | ||
.map(c => c / 255.0) | ||
.map(c => c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4); | ||
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b; | ||
return luminance <= 0.179; | ||
} | ||
|
||
export { isColorDark }; |
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,37 @@ | ||
import { isColorDark } from './is_color_dark'; | ||
|
||
describe('isColorDark', () => { | ||
|
||
const DARK_COLORS = [ | ||
[0, 104, 55], | ||
[165, 0, 38], | ||
[0, 0, 0], | ||
[219, 19, 116], | ||
[73, 0, 146], | ||
[70, 26, 10], | ||
[146, 0, 0] | ||
]; | ||
|
||
const LIGHT_COLORS = [ | ||
[191, 161, 128], | ||
[249, 133, 16], | ||
[0, 179, 164], | ||
[212, 157, 170], | ||
[255, 255, 255], | ||
[254, 182, 219], | ||
[230, 194, 32] | ||
]; | ||
|
||
DARK_COLORS.forEach(color => { | ||
it(`should return true for dark color rgb(${color.join(', ')})`, () => { | ||
expect(isColorDark(...color)).toBe(true); | ||
}); | ||
}); | ||
|
||
LIGHT_COLORS.forEach(color => { | ||
it(`should return false for light color rgb(${color.join(', ')})`, () => { | ||
expect(isColorDark(...color)).toBe(false); | ||
}); | ||
}); | ||
|
||
}); |
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