Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create requiresLightText util #311

Merged
merged 4 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# [`master`](https://github.com/elastic/eui/tree/master)

- Added `requiresLightText` color util [(#311)](https://github.com/elastic/eui/pull/311)
- EuiButton, EuiButtonEmpty and EuiButtonIcon can now take an `href` [(#316)](https://github.com/elastic/eui/pull/316)

**Bug fixes**
Expand Down
2 changes: 2 additions & 0 deletions src/services/colors/index.js
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';
23 changes: 23 additions & 0 deletions src/services/colors/is_color_dark.js
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 };
37 changes: 37 additions & 0 deletions src/services/colors/is_color_dark.test.js
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);
});
});

});
4 changes: 4 additions & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export {
RIGHT_ALIGNMENT,
} from './alignment';

export {
isColorDark
} from './colors';

export {
Pager,
} from './paging';
Expand Down