feat(heatmap): add text color contrast to heatmap cells#1342
feat(heatmap): add text color contrast to heatmap cells#1342rshen91 merged 19 commits intoelastic:masterfrom
Conversation
|
@rshen91 I've few doubts on your todo list:
we should fix the bullet chart in a different PR not this one. I'm also not sure what you mean by fix stories without having contrast defined.
Can you please explain to me why? in heatmap we are rendering the text on top of a cell, that is colored. Why we need to consider the background color of the chart? for semi-transparent colors? |
It looks now they are rendering fine right? merging master probably was the fix
I think we can fix that in a future PR, just open a different issue with that ok? |
packages/charts/src/chart_types/heatmap/layout/types/config_types.ts
Outdated
Show resolved
Hide resolved
| align: TextAlign; | ||
| baseline: TextBaseline; | ||
| visible: boolean; | ||
| textContrast: boolean | number; |
There was a problem hiding this comment.
let's reduce the possibilities there. My personal opinion is that we should always invert the text color if there is not enough contrast.
If you are ok we can remove this configuration option and have the code that always invert by default the color.
If requested, we can then expose a configuration option to turn this off or change the contrast ratio
packages/charts/src/chart_types/xy_chart/renderer/canvas/primitives/text.ts
Outdated
Show resolved
Hide resolved
| cellColor?: RgbObject, | ||
| ) { | ||
| const { textContrast } = font; | ||
| const convertPageBackgroundColor = getHexValue(pageBackgroundColor); |
There was a problem hiding this comment.
you should not need an hex value for the fillTextColor method
There was a problem hiding this comment.
For sure! So the issue I was facing was using rgba(0,0,0,0) instead of #00000 both pointing to a transparent color. The fillTextColor() has a warning for rgba values with an alpha value of 0. But yes, overall I do think it makes the most sense to have the fillTextColor() just using rgba values.
There was a problem hiding this comment.
rgba(0,0,0,0) is transparent
If you want to use black is rgba(0,0,0,1) with full opacity
#0 is black
| const contrastColor = | ||
| originalhighContrastTextColor === 'rgba(255, 255, 255, 1)' || originalhighContrastTextColor === '#fff' | ||
| ? '#000' | ||
| : '#fff'; | ||
| // make sure the new text color hits the ratio, if not, then return the scaledContrast since we tried earlier | ||
| return getContrast(contrastColor, background) > ratio ? contrastColor : scaledContrast.toString(); |
There was a problem hiding this comment.
two comments here:
1- can you explain why you have made these changes?
2- instead of use both hex and rgba values, can you please always use rgba strings instead where possible?
There was a problem hiding this comment.
-
I was noticing one of the cell background colors (rgba(44, 127, 184, 1), #2c7fb8) was going to white text with worse contrast than if it was black text. I hunted the issue down to this edge case and added this to catch this issue in the future.
-
I agree, I will make sure to refactor to use rgba values where possible instead of hex. I got hung up on the transparent hex value being passed by default but I agree that the rgba value makes the most sense. Will include this in my refactoring, thanks!
| export function getHexValue(color: Color) { | ||
| return RGBAToHex(RGBtoString(getColor(color))); | ||
| } | ||
|
|
There was a problem hiding this comment.
isn't that function the same as RGBAToHex?
There was a problem hiding this comment.
it looks like the function still there
|
jenkins, test this |
| align: TextAlign; | ||
| baseline: TextBaseline; | ||
| visible: boolean; | ||
| textContrast: boolean; |
There was a problem hiding this comment.
let's remove this for now from the configuration. If requested we can add that configuration, but I prefer to remove it
There was a problem hiding this comment.
Ok cool sounds good to me 0a9a914 for changes thanks!
| textColor: fillTextColor( | ||
| config.cell.label.textColor, | ||
| true, | ||
| config.cell.label.textContrast ? 4.5 : false, |
There was a problem hiding this comment.
As written on my previous comment, the color inversion should always happen, so you can replace this with always the 4.5 value
| true, | ||
| config.cell.label.textContrast ? 4.5 : false, | ||
| color, | ||
| theme.background.color === 'transparent' ? '#000000' : theme.background.color, |
There was a problem hiding this comment.
if with this code you mean: if the theme background color is transparent keyword use black as the fallback, I think we need to change that to use white instead, as the default theme is the LIGHT one
Use white rgba if possible rgba(255,255,255,1)
| ? '#000' | ||
| : '#fff'; |
There was a problem hiding this comment.
| ? '#000' | |
| : '#fff'; | |
| ? 'rgba(0,0,0,1)' | |
| : 'rgba(255,255,255,1)'; |
| export function getHexValue(color: Color) { | ||
| return RGBAToHex(RGBtoString(getColor(color))); | ||
| } | ||
|
|
There was a problem hiding this comment.
it looks like the function still there
| const containerBackground = combineColors(shapeFillColor, backgroundColor); | ||
| const containerBackground = combineColors( | ||
| shapeFillColor, | ||
| backgroundColor === 'transparent' ? '#000000' : backgroundColor, |
There was a problem hiding this comment.
similar to a previous comment:
the background fallback for transparent color should be white (because the default theme is the light one)
So if you need to replace the transparent with a valid fallback please use rgba(255,255,255,1)
| export { Example as basic } from './1_basic.story'; | ||
| export { Example as time } from './3_time.story'; | ||
| export { Example as categorical } from './2_categorical.story'; | ||
| export { Example as textColorContrast } from './4_color_text.story'; |
There was a problem hiding this comment.
instead of creating a new vrt for that, just show by default the value labels on the categorical example,we can save two new tests (please also remove the added test as this replace the need of the vrt added)
markov00
left a comment
There was a problem hiding this comment.
I'm ok merging this PR for now.
I will take a look and clean up the color contrast functions in a next PR because it doesn't look very clean and readable.
# [35.0.0](v34.2.1...v35.0.0) (2021-09-13) ### Bug Fixes * **a11y:** restore focus after popover close with color picker ([#1272](#1272)) ([0c6f945](0c6f945)), closes [#1266](#1266) [#935](#935) * **build:** fix license in package.json ([#1362](#1362)) ([d524fdf](d524fdf)) * **deps:** update dependency @elastic/eui to ^37.5.0 ([#1341](#1341)) ([fb05c98](fb05c98)) * **deps:** update dependency @elastic/eui to ^37.6.1 ([#1359](#1359)) ([2ae90ce](2ae90ce)) * **deps:** update dependency @elastic/eui to ^37.7.0 ([#1373](#1373)) ([553b6b0](553b6b0)) * **heatmap:** filter out tooltip picked shapes in x-axis area ([#1351](#1351)) ([174047d](174047d)), closes [#1215](#1215) * **heatmap:** remove values when brushing only over axes ([#1364](#1364)) ([77ff8d3](77ff8d3)) ### Features * **annotations:** add onClickHandler for annotations ([#1293](#1293)) ([48198be](48198be)), closes [#1211](#1211) * **heatmap:** add text color contrast to heatmap cells ([#1342](#1342)) ([f9a26ef](f9a26ef)), closes [#1296](#1296) * **heatmap:** reduce font size to fit label within cells ([#1352](#1352)) ([16b5546](16b5546)) * **xy:** mutilayer time axis step 1 ([#1326](#1326)) ([867b1f5](867b1f5)) ### BREAKING CHANGES * **xy:** - feat: removes the axis deduplication feature - fix: `showDuplicatedTicks` causes a duplication check on the actual axis tick label (possibly yielded by `Axis.tickLabel` rather than the more general `tickFormat`) * **heatmap:** the `config.label.fontSize` prop is replaced by `config.label.minFontSize` and `config.label.maxFontSize`. You can specify the same value for both properties to have a fixed font size. The `config.label.align` and `config.label.baseline` props are removed from the `HeatmapConfig` object.
|
🎉 This PR is included in version 35.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Summary
Text will now take into account the background color of the cell and adjust its contrast to 4.5.
Details
Still to do:
Issues
Closes #1296
Checklist
:xy,:partition):interactions,:axis)closes #123,fixes #123)packages/charts/src/index.ts