-
Notifications
You must be signed in to change notification settings - Fork 122
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
refactor: simplify color manipulation code #1372
refactor: simplify color manipulation code #1372
Conversation
7148acf
to
604b153
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code wise, and in spirit, looks great 🎉
See some non-blocker comments inline.
The darkening of some (but for reason, not all; see most Cartesians and bullet/gauge/goal) axis tick labels is a welcome change, going from mid gray to slightly darker gray. Not necessarily in this PR, it'd be great to reach black or near-black.
The level of text whitening seems excessive, at least to me resulting in clearly less readable text on lighter or mid luma backgrounds even when compared to the former dark gray, not to mention compared to using the new, real black which many partition charts now do for the dark. See individual comments for image examples
A blocker is that some hover highlights (well, fading the other bits) got lost, for example:
@monfera Thanks for catching the missing hover highlights: it was caused by my changes in the
The problem happens when the |
@monfera and actually there is no darkening of the axis, but just the fact that the background didn't get cleared correctly caused the axis being rendered twice, one on top of the other, when moving the mouse over the legend (as in the linked screenshot) |
@markov00 I had a vague recall that I did the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving it with the note that I'm not the authority on the contrast criteria, so I can't speak for or against the extent the new algo leans toward white text over mid colored areas. Thanks for linking the external PR where you enquired about some of the apparent contrast regressions we saw
Thanks for the note @markov00. I love that you all continue to think through accessibility improvements. I will throw my quick concern out there for jumping into the APCA calculation (without much research of my own). Mainly, that I have 2 concerns.
A ratio of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @cchaos for your comments, I'd like to add few more comments to clarify the choices:
You are right, we should prefer a consistent methodology across Elastic. I reverted the changes to use the WCAG2/relative luminance algorithm, for now, but I will keep the APCA method around for testing. If I'm not wrong Axe works testing accessibility of DOM elements, but it can't read/interpret the content of an image, providing valuable hints if a text has more/less contrast than its background. Charts are rendered in
The method used in your example to calculate the contrast is the one available in the WCAG 2 specification (based on relative luminance, with a set of AA, AAA levels specific to the calculated contrast ratio). The APCA method instead shows that white on orange has higher contrast than black on orange (see this interesting article about the orange button problem in a11y https://www.bounteous.com/insights/2019/03/22/orange-you-accessible-mini-case-study-color-ratio/). There are definitely differences in the calculated contrast but, as described here, these differences between old and new ways are the output of multiple research for improving text readability and contast. The WCAG 3.0 is still a draft, but it's announced that the APCA method will replace the existing one: Chrome also has added the APCA contrast in the dev tools (now only behind an experimental flag and with some bugs 😬 ) https://developer.chrome.com/blog/new-in-devtools-89/#apca Anyway, I've already switched back to the previous WCAG 2.0 calculation for consistency. APCA is still in development/research so there can be improvement in the near feature that can push us to adopt it earlier, in the meantime, we can live with the relative luminance method. |
You have probably picked up an outdated commit, this was solved in 70f1774 |
Thanks for the explanation @markov00 . I definitely agree that the current luminance method is not ideal in certain situations, but I just couldn't believe how different the results between that and the new method were. It worries me a bit and since it's still in draft, I'd worry about jumping to use it right away. I appreciate that it's back to the original calculation at least for now. It's definitely something we'll want to continue to monitor and most likely an effort we'll want to take across multiple packages, not just charts. As an FYI, I've also been monitoring this issue for a while, just now realizing that it was started/continues to be updated by the creator of that APCA method. w3c/wcag#695. But the fact that it remains open makes me think there's not been a final decision/method yet. |
🎉 This PR is included in version 37.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Hello everyone, @monfera @cchaos @markov00 this post is replying specifically to Marco, Caroline, and Robert: First, just FYI, there is an APCA repo hereAnd I've opened the discussion tab to field any questions or concerns. Some general responses to comments in this thread:
SpecificsLuminance vs Lightness for Readability Contrast
Hi Marco, yes the WCAG 2 method does not follow human perception of suprathreshold contrast, which is critical for readability. APCA does use luminance, but it goes much farther, weighting the luminances per a perception model to achieve uniformity that follows our perception of text on a screen.
I was surprised by the interest and the large number of early adopters. I seriously was not expecting the turhout if you will, so I'm a tad embarrassed that some of the early code was a little less stable. The current version has been going since February, and seems stable, further adjustments are expected to be more like tweaks.
There are some developers that are doing both WCAG 2 and 3 at one with success (you lose the APCA flexibility though). Feel free to ask questions at the APCA GitHub repo in the discussions area.... I'm going to respond to some of your other comments with Caroline's below... Mid Contrast, and the W/B Switch Point — Responding to @monferaHello Robert, it appears to me that the fonts may be too small for the given contrast? One of the changes in APCA is the recognition of spatial frequency as a primary force driving contrast perception. For our purposes, spatial frequency means font size and weight. Another change is the definition of the "middle" of the contrast range on a display ... where to flip from black to white text? The short answer is that if we are going to "auto flip" between black and white, we want to do so when relative luminance (Y) is between 0.32Y and 0.40Y, with 0.35Y - 0.36Y being about ideal for most colors. This translates to a BG color of equivalent luminance to In the example below, just the top two lines qualify, bottom row is a fail for fluent readability: This is using a color from your icicle chart, and now let's look there: The font is fairly thin, and perhaps font-smoothing is set to the faux antialias? While the black font is indeed near black, the white font is no where near white, it's only at 50% the luminance of white, so perceptually is about 65% lightness when we'd want it to be ~95% lightness. This has nothing to do with the APCA algorithm, it's a function of the font weight and size, and the way the rasterizer is antialiasing, reducing contrast. One of the stipulations in WCAG 3 is to not use faux antialiasing, and leave "webkit font smoothing" at auto, which allows the device to do subpixel anti-aliasing. Counterintuitively, the anti-alias setting in Webkit-font-smoothing is actually a blending that reduces resolution and contrast. This same issue is true here: Again, with that BG color, only the top two rows in this test sample are recommended. Second from the bottom is "poor but passing" and the bottom row is a fail for fluent readability. In response to @cchaosHello Caroline, regarding your concerns with "consistency" & "less contrast", in reverse order: 2) To your comment "Actually produces less contrast" — this is not true, though I see how you might come to that conclusion. APCA does not produce contrast. APCA does nothing to the colors, it only predicts how a human will perceive them, and their effect on readability. And yes, if you try to compare the math it might appear that APCA allows lower contrast, but the reality is perhaps a bit hidden. For instance, your example: Let me ask you if that seems like less than 3:1 contrast? Perhaps it's not fair without a comparison. Take a look at this one below to compare, which one has better contrast? The one above or this one below? After thinking about this for a moment, click the spoiler for the answer: **Spoiler: Which is better contrast?**Okay, so the first example that you provided was the Orange Example I am running on my sites. This problem brought WCAG 2.x to my attention and is part of why the design community loathes WCAG 2 contrast. In the first example, APCA correctly reports the contrast as readable at Lc61 for the appropriate font sizes. WCAG reports a fail at 2.97:1. Okay but then on the second one, APCA reports a total fail and prohibits any text at this level. WCAG reports the second one as a PASS at 4.5:1 (!!)Should this second one be a pass? At 4.5:1? The colors are WCAG 2 says this is fine and passes AA. Do you agree? FACT: When the brightest color is darker than And here is a central problem: the numbers generated by WCAG 2's contrast math is not relative to human perception of supra-threshold contrast, which is the contrast needed for best readability. APCA very specifically is perceptually accurate, and predicts constant contrast across the entire range. WCAG values are artificially inflated over part of the range to try and make up for these shortcomings, but APCA does not need any such "brute force". Since APCA is perceptually uniform, one correct Lc value works across the entire available range.At the SAPC development site, we have a "Research Mode" you can click that reveals some interactive experiments that demonstrate some of the concepts. And here are links to some brief articles (shorter than this post LOL) with loads of examples that more fully demonstrate this: The Lighter Side of Dark BackgroundsOrange You Wondering About Contrast?Answer to Caroline's first question: 1) Consistency: it is not possible to create something that works correctly and also be consistent with something that is incorrect. And that is the problem we face with APCA. There is no way to compare the two. WCAG 2's problems are well understood. APCA is superior in accuracy and useful predictions. Fortunately, there is enough overlap that a "transition" can be had, some of the tools developed by other developers have a "pass all" setting, to pass both WCAG 2 and WCAG 3 at once, though the trade off is a loss of the flexibility that APCA normally adds. Also, APCA has an "escape level" called Score 1, it is the weakest passing score, and considered "poor" but placed there so as to allow sites to pass that are currently passing WCAG 2, while providing guidance to improve to the newer standard. Caroline:
Yes, I started thread 695. In that thread I explain how and why I ended up taking this on as a full time research project. I originally thought I'd be done in three months. LOL. I mean ROTFLMAO. Going into year three, I can say that we will have a new readable world! The internet killed the print industry. Magazines are all but gone. I miss reading real paper, and I find it very difficult to read on a screen. In fact, I believe the reason that so few people actually read today is due to this issue. Consider the chilling effects of a populace that declines reading, as a civil rights author, I can find no better motivation that making the world a more readable place. 695 is OPEN in part because it is tagged as normative, and in part because this is a major paradigm shift, and comments and discussion will continue, as it has here in THIS thread. I am trying to be open to the degree possible, which is why I opened the discussion tab at the APCA repo. A last question, perhaps for Marco?If I can ask, what are you using to generate colors? Are you using CIELUV or a variant? Or CAM02? Or?? Regardless of the method, are you having any difficult problems with color or contrast generation? TL;DR (LOL)APCA is demonstrably superior to the old method, but is also more involved than the distance between two colors, as spatial frequency is a key part of the prediction. I hope I addressed everyone's thoughts and concerns. Feel free to start a chat at the APCA repo discussion area as well. Thank you, Andy Andrew Somers |
Summary
This PR simplifies and refactor the current color manipulation code. This is required to complete #1320
General changes
RgbaTuple: [r,g,b,a]
d3-color
textContrast
,textInvertible
andtextOpacity
props are removed from the API due to favor using always high contrast text colormakeHighContrastColor
method now just returnswhite
orblack
text color depending on their contrast with the provided background. The contrast is computed with APCA algorithm that is an improved version of the current WCAG 2 math (part of the new WCAG 3.0 guidelines)BREAKING CHANGES:
the
textContrast
textInvertible
andtextOpacity
in theFont
type has been removed. This affect theHeatmapConfig
type in thecell.label
,xAxisLabel
, andyAxisLabel
properties. This also affect thePartitionConfig
properties:fillLabel
andlinkLabel
. ThetextInvertible
andtextContrast
properties are also removed from theDisplayValueStyle
used to display bar values labels. The mentioned changes are replaced with the following behavior: always invert the text color depending on the color contrast with its background using pureblack
orwhite
colors.Note for EUI team review
@elastic/eui-design there is a minor change that affects the
theme
of the chart, in particular, thebarSeriesStyle.displayValue
property that introduces the following change: https://github.com/elastic/elastic-charts/pull/1372/files#diff-ded06b4515f820c90ab4007ab80a647215fcc9cb4b53998851ce302aee64d5a6L690-L691Your current theme actually uses a flat color for the changed
fill
type, so the change doesn't affect your theme.I'd like to just highlight a change we did to the text color when used above a colored background (aka value labels or labels on partition charts): instead of computing a gradual more dark/light color with the sufficient WCAG AA level, we apply a flat
black
orwhite
color based on theirs contrast with the background. The color contrast calculation is done using a new algorithm calledAPCA
used in the WCAG 3 draft instead of the previous algorithm that used the perceived luminance. As linked above, this method seems to fix a lot of issues that the luminance method didn't consider.Checklist
:xy
,:partition
):interactions
,:axis
):theme
label has been added and the@elastic/eui-design
team has been pinged when there areTheme
API changespackages/charts/src/index.ts
dark
,light
,eui-dark
&eui-light