RESOLVED FIXED 224512
Use range-for loop in target contrast selection to simplify/improve code readability
https://bugs.webkit.org/show_bug.cgi?id=224512
Summary Use range-for loop in target contrast selection to simplify/improve code read...
Sam Weinig
Reported 2021-04-13 14:58:09 PDT
Use range-for loop in target contrast selection to simplify/improve code readability
Attachments
Patch (4.75 KB, patch)
2021-04-13 15:00 PDT, Sam Weinig
no flags
Patch (34.57 KB, patch)
2021-04-13 17:55 PDT, Sam Weinig
no flags
Patch (17.11 KB, patch)
2021-04-13 17:56 PDT, Sam Weinig
darin: review+
ews-feeder: commit-queue-
Patch (17.11 KB, patch)
2021-04-14 08:58 PDT, Sam Weinig
no flags
Sam Weinig
Comment 1 2021-04-13 15:00:13 PDT
Sam Weinig
Comment 2 2021-04-13 15:00:53 PDT
Couldn't work out a clear way to use std::max_element without duplicating luminance calculations or some ugly gymnastics.
Darin Adler
Comment 3 2021-04-13 16:19:20 PDT
Comment on attachment 425920 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=425920&action=review > Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp:1512 > +static Color selectFirstColorThatMeetsOrExceedsTargetContrast(const Color& originBackgroundColor, Vector<Color>&& colorsToCompareAgainst, double targetContrast) Annoying that this is double and the return value of WebCore::contrastRatio is float. > Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp:1519 > return color; Could use WTFMove(color) here since the vector is an rvalue reference. I presume that is why we wanted to pass ownership? > Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp:1533 > + auto* colorWithHigestContrastRatio = &colorsToCompareAgainst[0]; Just noticed "higest". > Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp:1543 > + return *colorWithHigestContrastRatio; Could use WTFMove(*colorWithHighestContrastRatio) here since the vector is an rvalue reference. I presume that is why we wanted to pass ownership?
Darin Adler
Comment 4 2021-04-13 16:27:12 PDT
Separately looked at callers of contrastRatio. Noticed this: // Uses WCAG 2.0 definition of legibility: contrast ratio of 4.5:1 or greater. // https://www.w3.org/TR/WCAG20/#visual-audio-contrast-contrast return Color::contrastRatio(textColor, backgroundColor) > 4.5; Comment says >=, code says >. And contrastRatio returns float, but we compare with a double. Also not sure that contrastRatio should be a static member of the Color class. Just overload it for Color arguments but keep it at the WebCore level since callOnUnderlyingType is a public member function template. Also don’t need to write WebCore::contrastRatio in most places. Just contrastRatio will do. Also don’t need a local variable named contrastRatio in selectFirstColorThatMeetsOrExceedsTargetContrast; local is only used once. Also could use the phrase "highest contrast" or "greatest contrast" instead of "highest contrast ratio" to keep variable names smaller. Also the implementation of contrast ratio converts from float to double and back because of the use of "0.05" instead of "0.05f".
Sam Weinig
Comment 5 2021-04-13 17:55:14 PDT
Sam Weinig
Comment 6 2021-04-13 17:55:34 PDT
Address more feedback in this last one.
Sam Weinig
Comment 7 2021-04-13 17:56:32 PDT
Darin Adler
Comment 8 2021-04-13 18:16:09 PDT
Comment on attachment 425939 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=425939&action=review > Source/WebCore/rendering/TextPaintStyle.cpp:66 > // Uses the WCAG 2.0 definition of legibility: a contrast ratio of 4.5:1 or greater. > // https://www.w3.org/TR/WCAG20/#visual-audio-contrast-contrast > - return Color::contrastRatio(textColor, backgroundColor) > 4.5; > + return contrastRatio(textColor, backgroundColor) > 4.5; Comment says >=, code still says just >. I know this is floating point, so the difference is infinitesimal, but still ...
Sam Weinig
Comment 9 2021-04-14 08:58:08 PDT
Darin Adler
Comment 10 2021-04-14 09:17:19 PDT
Comment on attachment 425990 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=425990&action=review Really like these refinements. And I do have a parting shot. > Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp:1514 > -static Color selectFirstColorThatMeetsOrExceedsTargetContrast(const Color& originBackgroundColor, const Vector<Color>& colorsToCompareAgainst, double targetContrast) > +static Color selectFirstColorThatMeetsOrExceedsTargetContrast(const Color& originBackgroundColor, Vector<Color>&& colorsToCompareAgainst, double targetContrast) > { > auto originBackgroundColorLuminance = originBackgroundColor.luminance(); Too late for this patch, and eventually we hit diminishing returns, but please indulge me with one more thought from the "Darin coding manual of style": The names here are too long. The function uses "origin background color" as an argument name; that is unmotivated. This function is not about "origins" so the word origin is intruding from the context where the function is used, not so appropriate within the function itself, unless something about the function was truly related to the notion of an origin. And this local variable hardly needs so many words in its name given the context of this short function. I would probably call it "backgroundLuminance", or if no one was watching me and I left to my own devices I might even call it "luminance" (and be objectively wrong). I feel better about "colorsToCompareAgainst"; additional context in that name seems welcome even for this short function. But note that we not only *compare* against those colors but rather we *select* from among them, which explains why this function takes ownership. So even that name might be refined. I might name that foregroundColors or foregroundColorCandidates. > Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp:1528 > +static Color selectFirstColorWithHighestContrast(const Color& originBackgroundColor, Vector<Color>&& colorsToCompareAgainst) Same naming issues.
EWS
Comment 11 2021-04-14 10:44:51 PDT
Found 1 new test failure: webrtc/peerconnection-new-candidate-page-cache.html
EWS
Comment 12 2021-04-14 13:00:26 PDT
Found 1 new test failure: http/tests/webgl/1.0.2/origin-clean-conformance.html
EWS
Comment 13 2021-04-14 17:15:12 PDT
Committed r275981 (236533@main): <https://commits.webkit.org/236533@main> All reviewed patches have been landed. Closing bug and clearing flags on attachment 425990 [details].
Radar WebKit Bug Importer
Comment 14 2021-04-14 17:17:33 PDT
Note You need to log in before you can comment on or make changes to this bug.