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

feat: add 4.5 contrast for text in partition slices #608

Merged
merged 78 commits into from
Jun 8, 2020

Conversation

rshen91
Copy link
Contributor

@rshen91 rshen91 commented Mar 26, 2020

Summary

Closes #606
Iterated off some logic from elastic/kibana#60261 into the elastic-charts repo to calculate light or dark text for the partition and tree map charts.

  • Text color changes accounting for default 4.5 contrast within the pie or tree map slices or the link labels outside of the pie charts. If black or white text on whatever slice background can achieve the 4.5 contrast ratio, or user specified contrast ratio, then the text will be either black or white. Otherwise, the text will adjust grayscale to try and achieve the desired contrast ratio.
  • If textInvertible is set true, then the contrast of the text on the slices and the container background color is taken into account.
  • This PR also allows the user to provide the background color of the container so that contrast can be calculated effectively for any opacity/transparency of a pie slice.

New stories added:

  • Stylings - Partition Background: which has a pie chart with varying opacity values. Knobs are included to set textInvertible and textContrast to true or false. Additionally you can specify a number for textContrast to try and achieve.
  • Stylings - Partition Labels: showing the link labels adjusting in color to different background colors. There is a knob provided for the background to be set to show textColor changing in the labels and the text within the pie slices.

Main files with changes for reviewers:

  • src/chart_types/partition_chart/layout/utils/calcs.ts
  • src/chart_types/partition_chart/layout/viewmodel/fill_text_layout.ts
  • src/chart_types/partition_chart/layout/viewmodel/link_text_layout.ts
  • There is also a Google sheet with color and contrast with black text vs. white text worth checking out to justify why some of the stories changed.

Checklist

Delete any items that are not applicable to this PR.

  • This was checked for cross-browser compatibility, including a check against IE11
  • Proper documentation or storybook story was added for features that require explanation or tutorials
  • Unit tests were updated or added to match the most common scenarios

@codecov-io
Copy link

codecov-io commented Mar 26, 2020

Codecov Report

Merging #608 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #608   +/-   ##
=======================================
  Coverage   72.77%   72.77%           
=======================================
  Files         266      266           
  Lines        8716     8716           
  Branches     1645     1645           
=======================================
  Hits         6343     6343           
  Misses       2330     2330           
  Partials       43       43           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0a24a5e...0a24a5e. Read the comment docs.

@rshen91 rshen91 changed the title feat: add luminance value calculation for text in partition slices feat: add luminosity value calculation for text in partition slices Mar 26, 2020
@rshen91 rshen91 changed the title feat: add luminosity value calculation for text in partition slices feat: add 4.5 contrast for text in partition slices Apr 1, 2020
@rshen91
Copy link
Contributor Author

rshen91 commented Apr 1, 2020

@cchaos when you have the time, I'd love to hear your thoughts and make sure I'm interpreting this correctly:
This PR is meant to address the earlier conversation here about taking into account relative luminosity: https://elastic.slack.com/archives/CFK9BJ248/p1584979319002100

Currently, this PR will take into account contrast (which I believe is closely tied to luminosity) but in the sunburst heterogenous story (screenshot below) it looks like the lighter red partitions should be black text instead of white? Or am I misinterpreting and the white text on the rgba(228, 26, 28, 0.3) background is actually ok?
Screen Shot 2020-04-01 at 4 23 14 PM

Is there a different term (like transparency?) that I should be considering in calculating the text color with a certain background color? Thanks for you help in advance!

@cchaos
Copy link
Contributor

cchaos commented Apr 3, 2020

@rshen91 Yes you are correct. That white text on the transparent sections does not have high contrast which means that your calculations don't take transparency into account. Though the only way it'd be able to do so is if you also knew the background color of the container.

Opacity is a killer for trying to calculate contrast ratios. In EUI, we don't use transparency but "tint" instead which is when a color is blended with white. Or "shade" for blending with black.

But as user and consumer created object, you'll never truly know the background color unless you ask for it.

Also, it still doesn't look like the calculations are correct as even the inner-most slice, which I'd assume is not already transparent, fails at only 3.06

Screen Shot 2020-04-03 at 16 59 49 PM

@rshen91
Copy link
Contributor Author

rshen91 commented Apr 7, 2020

Issue #238 is tied to this issue in that if we get the background color in order to determine tint or shade for this PR, that same prop can be used to help solve issue #238 detect the background color and apply an inverse color or one color from a dark/light palette

@rshen91
Copy link
Contributor Author

rshen91 commented Apr 14, 2020

This PR will be relevant for PR #629 where the background color can be used for the tooltip color icon

Copy link
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

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

One suggested change but otherwise looks great 🎉

Nice work, this makes the pie charts look that much better!

src/components/chart_background.tsx Outdated Show resolved Hide resolved
@@ -34,18 +33,19 @@ export class ChartBackgroundComponent extends React.Component<ChartBackgroundPro
}

render() {
return <div className="echChartBackground" style={{ background: this.props?.backgroundStyles?.color }} />;
const { background } = this.props;
return <div className="echChartBackground" style={{ background }} />;
Copy link
Collaborator

@nickofthyme nickofthyme Jun 8, 2020

Choose a reason for hiding this comment

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

This could allow non-colors to be passed to the background. Is this a good idea?

body {
  background: lightblue url("img_tree.gif") no-repeat fixed center;
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

changed here 9c91e74

}
}

const mapStateToProps = (state: GlobalChartState): ChartBackgroundProps => {
if (!getInternalIsInitializedSelector(state)) {
return {
backgroundStyles: getChartThemeSelector(state).background,
background: 'transparent',
Copy link
Collaborator

Choose a reason for hiding this comment

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

good catch

@nickofthyme nickofthyme merged commit eded2ac into elastic:master Jun 8, 2020
@rshen91 rshen91 deleted the luminance-pie branch June 10, 2020 15:13
markov00 pushed a commit that referenced this pull request Jun 15, 2020
# [19.5.0](v19.4.1...v19.5.0) (2020-06-15)

### Bug Fixes

* **tooltip:** show true opaque colors in tooltips ([#629](#629)) ([23290be](23290be)), closes [#628](#628)
* path of stacked area series with missing values ([#703](#703)) ([2541180](2541180))
* remove double rendering ([#693](#693)) ([ebf2748](ebf2748)), closes [#690](#690)

### Features

* **partition:** add 4.5 contrast for text in partition slices ([#608](#608)) ([eded2ac](eded2ac)), closes [#606](#606)
* add screenshot functions to partition/goal ([#697](#697)) ([5581c3c](5581c3c))
@markov00
Copy link
Member

🎉 This PR is included in version 19.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@markov00 markov00 added the released Issue released publicly label Jun 15, 2020
AMoo-Miki pushed a commit to AMoo-Miki/OpenSearch-Dashboards that referenced this pull request Feb 10, 2022
# [19.5.0](elastic/elastic-charts@v19.4.1...v19.5.0) (2020-06-15)

### Bug Fixes

* **tooltip:** show true opaque colors in tooltips ([opensearch-project#629](elastic/elastic-charts#629)) ([323b325](elastic/elastic-charts@323b325)), closes [opensearch-project#628](elastic/elastic-charts#628)
* path of stacked area series with missing values ([opensearch-project#703](elastic/elastic-charts#703)) ([93f063f](elastic/elastic-charts@93f063f))
* remove double rendering ([#693](elastic/elastic-charts#693)) ([1a9bbb9](elastic/elastic-charts@1a9bbb9)), closes [#690](elastic/elastic-charts#690)

### Features

* **partition:** add 4.5 contrast for text in partition slices ([opensearch-project#608](elastic/elastic-charts#608)) ([59d6d49](elastic/elastic-charts@59d6d49)), closes [opensearch-project#606](elastic/elastic-charts#606)
* add screenshot functions to partition/goal ([opensearch-project#697](elastic/elastic-charts#697)) ([6d2ff64](elastic/elastic-charts@6d2ff64))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Issue released publicly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Partition: calculate relative luminance for text
7 participants