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

Conversation

timroes
Copy link
Contributor

@timroes timroes commented Jan 17, 2018

This PR introduces a util function to calculate whether we need to draw light or dark text on a specific background color. We need this in visualization, so we can determine what text color we should use on specific background colors for metrics and heatmap.

Since I think this function can be of value for other teams too and it's related to design, I think the EUI services are a good place to implement it.

The forumlar to calculate luminance is described in this stackoverflow answer ans is based on the WCAG guidelines.

For the case anyone wonders what the colors in the tests are: those are our visualization colors from visualization_colors.js and some random colors from the color palettes uses in visualizations at the moment.

Copy link
Contributor

@cjcenizal cjcenizal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! Just had a couple suggestions.

* be light or dark for better readability.
* The color must be specified via its red, green and blue value in the range of
* 0 to 255.
* The formular is based on this Stackoverflow answer: https://stackoverflow.com/a/3943023
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: formular -> formula

* @param {number} blue The blue component in the range 0 to 255
* @returns {boolean} True if the background requires light text on it, false otherwise.
*/
function requiresLightText(red, green, blue) {
Copy link
Contributor

@cjcenizal cjcenizal Jan 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this function isColorDark? This way the function name is specific to the input, without any presumptions about how it's being used.

And how about we do some ducktyping with the params to support a hex value input? We can use the conversion functions from https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb.

function isColorDark(redOrHex, green, blue) {
  let [r, g, b];

  if (typeof redOrHex === 'string') {
    [r, g, b] = hexToRgb(redOrHex);
  } else {
    [r, g, b] = [red, green, blue];
  }  

  /* ... */
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have to update the tests to reflect this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How strong are your feelings for using the red or hex. I would rather keep this method as simple as possible. Also currently we don't need this with hex. So I would rather implement it if we really need it (to not produce dead code right from the beginning).

return luminance <= 0.179;
}

export { requiresLightText };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this file to is_color_dark.js to match the function name? And can we name the folder color (singular)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't create that folder, so I won't rename it in this PR, since it means updating a lot of other places, too, what would be unrelated to this PR. But I don't mind if you want to rename it in another PR :D

@cjcenizal
Copy link
Contributor

cjcenizal commented Jan 17, 2018 via email

Copy link
Contributor

@cjcenizal cjcenizal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Member

@ppisljar ppisljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool! LGTM

@timroes timroes merged commit 93ee3d6 into elastic:master Jan 19, 2018
@timroes timroes deleted the contrast-util branch January 19, 2018 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants