Changeset 273683 in webkit


Ignore:
Timestamp:
Mar 1, 2021 12:56:56 PM (17 months ago)
Author:
weinig@apple.com
Message:

Add experimental support for CSS Color 5 color-contrast()
https://bugs.webkit.org/show_bug.cgi?id=222530

Reviewed by Simon Fraser.

Source/WebCore:

This feature is off by default and can be enabled via the CSSColorContrastEnabled
experimental preference flag.

This implementation has the same restriction on it that the recently landed
Relative Color Syntax and color-mix() do, in that it does support system colors
or currentColor as input, since those can't be resolved at parse time. Ultimately,
we will need to add a late binding version of this for those cases.

Test: fast/css/parsing-color-contrast.html

  • Headers.cmake:
  • WebCore.xcodeproj/project.pbxproj:

Add new ColorLuminance.h header where the generic relative luminance and contrast ratio
functions live.

  • css/CSSValueKeywords.in:

Add new keywords, color-contrast and vs, that are needed for the color-contrast() function.

  • css/parser/CSSParserContext.cpp:
  • css/parser/CSSParserContext.h:

Add a setting for color-contrast.

  • css/parser/CSSPropertyParserHelpers.cpp:

(WebCore::CSSPropertyParserHelpers::parseColorContrastFunctionParameters):
(WebCore::CSSPropertyParserHelpers::parseColorFunction):
Add parsing and computation of color-contrast().

  • platform/graphics/ColorUtilities.cpp:

(WebCore::lightness): Deleted.
(WebCore::luminance): Deleted.
(WebCore::contrastRatio): Deleted.
Moved luminance related functions to ColorLuminance.h and inlined lightness
to its one caller, Color and noted it should be removed.

  • platform/graphics/ColorUtilities.h:

(WebCore::invertedColorWithOverriddenAlpha):
(WebCore::invertedcolorWithOverriddenAlpha): Deleted.
Fix capitalization issue seen. invertedcolorWithOverriddenAlpha -> invertedColorWithOverriddenAlpha.

  • platform/graphics/Color.cpp:

(WebCore::Color::lightness const):
Inline implementation and add comment explaining it should be removed.

(WebCore::Color::luminance const):
Re-write to use the new WebCore::relativeLuminance that works for any color type
without conversion to sRGB.

(WebCore::Color::contrastRatio):
Add helper to call generic WebCore::contrastRatio that works on any color types
to avoid callers needing to do the unfolding themselves.

(WebCore::Color::isBlackColor):
(WebCore::Color::isWhiteColor):

  • platform/graphics/Color.h:

(WebCore::Color::isBlackColor): Deleted.
(WebCore::Color::isWhiteColor): Deleted.
Move these rare functions out of line to reduce the number of places in the header we are
calling callOnUnderlyingType(), which produces code linerally with the number of color spaces
supported. Calling it in the cpp files means we only expand it once for each function.

  • platform/graphics/ColorLuminance.h: Added.

(WebCore::relativeLuminance):
This is a generic version of the old luminance function that works for any color type
by converting to XYZ and taking the Y component. The old function required always
converting to sRGB which could be lossy.

(WebCore::contrastRatio):
Split out computation of contrastRatio based on relative luminace floats into its own
function so that if we have the relative luminance computed already, we don't have to
recompute it. Add version contrastRatio that works for any color type utilizing the
generic relativeLuminance function above.

  • rendering/RenderTheme.cpp:

(WebCore::RenderTheme::disabledTextColor const):

  • rendering/TextPaintStyle.cpp:

(WebCore::textColorIsLegibleAgainstBackgroundColor):
Update to use new Color::contrastRatio helper that handles all color types.

Source/WTF:

  • Scripts/Preferences/WebPreferencesExperimental.yaml:

Add new experimental preference for CSS Color 5 color-contrast()
which is off by default.

Tools:

  • TestWebKitAPI/Tests/WebCore/ColorTests.cpp:

(TestWebKitAPI::TEST):
Update luminance values to account for more accurate conversion to
XYZ now that we are usuing the actual matrix values from SRGBADescriptor
and not a truncated copy.

LayoutTests:

  • fast/css/parsing-color-contrast-expected.txt: Added.
  • fast/css/parsing-color-contrast.html: Added.

Add parsing and computed style computation tests for color-contast().

Location:
trunk
Files:
3 added
18 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r273672 r273683  
     12021-03-01  Sam Weinig  <weinig@apple.com>
     2
     3        Add experimental support for CSS Color 5 color-contrast()
     4        https://bugs.webkit.org/show_bug.cgi?id=222530
     5
     6        Reviewed by Simon Fraser.
     7
     8        * fast/css/parsing-color-contrast-expected.txt: Added.
     9        * fast/css/parsing-color-contrast.html: Added.
     10        Add parsing and computed style computation tests for color-contast().
     11
    1122021-03-01  Said Abou-Hallawa  <said@apple.com>
    213
  • trunk/Source/WTF/ChangeLog

    r273657 r273683  
     12021-03-01  Sam Weinig  <weinig@apple.com>
     2
     3        Add experimental support for CSS Color 5 color-contrast()
     4        https://bugs.webkit.org/show_bug.cgi?id=222530
     5
     6        Reviewed by Simon Fraser.
     7
     8        * Scripts/Preferences/WebPreferencesExperimental.yaml:
     9        Add new experimental preference for CSS Color 5 color-contrast()
     10        which is off by default.
     11
    1122021-02-25  Simon Fraser  <simon.fraser@apple.com>
    213
  • trunk/Source/WTF/Scripts/Preferences/WebPreferencesExperimental.yaml

    r273244 r273683  
    114114      default: false
    115115
     116CSSColorContrastEnabled:
     117  type: bool
     118  humanReadableName: "CSS color-contrast()"
     119  humanReadableDescription: "Enable support for CSS color-contrast() defined in CSS Color 5"
     120  defaultValue:
     121    WebKitLegacy:
     122      default: false
     123    WebKit:
     124      default: false
     125    WebCore:
     126      default: false
     127
    116128CSSColorMixEnabled:
    117129  type: bool
  • trunk/Source/WebCore/ChangeLog

    r273681 r273683  
     12021-03-01  Sam Weinig  <weinig@apple.com>
     2
     3        Add experimental support for CSS Color 5 color-contrast()
     4        https://bugs.webkit.org/show_bug.cgi?id=222530
     5
     6        Reviewed by Simon Fraser.
     7
     8        This feature is off by default and can be enabled via the CSSColorContrastEnabled
     9        experimental preference flag.
     10
     11        This implementation has the same restriction on it that the recently landed
     12        Relative Color Syntax and color-mix() do, in that it does support system colors
     13        or currentColor as input, since those can't be resolved at parse time. Ultimately,
     14        we will need to add a late binding version of this for those cases.
     15
     16        Test: fast/css/parsing-color-contrast.html
     17
     18        * Headers.cmake:
     19        * WebCore.xcodeproj/project.pbxproj:
     20        Add new ColorLuminance.h header where the generic relative luminance and contrast ratio
     21        functions live.
     22
     23        * css/CSSValueKeywords.in:
     24        Add new keywords, color-contrast and vs, that are needed for the color-contrast() function.
     25
     26        * css/parser/CSSParserContext.cpp:
     27        * css/parser/CSSParserContext.h:
     28        Add a setting for color-contrast.
     29
     30        * css/parser/CSSPropertyParserHelpers.cpp:
     31        (WebCore::CSSPropertyParserHelpers::parseColorContrastFunctionParameters):
     32        (WebCore::CSSPropertyParserHelpers::parseColorFunction):
     33        Add parsing and computation of color-contrast().
     34
     35        * platform/graphics/ColorUtilities.cpp:
     36        (WebCore::lightness): Deleted.
     37        (WebCore::luminance): Deleted.
     38        (WebCore::contrastRatio): Deleted.
     39        Moved luminance related functions to ColorLuminance.h and inlined lightness
     40        to its one caller, Color and noted it should be removed.
     41       
     42        * platform/graphics/ColorUtilities.h:
     43        (WebCore::invertedColorWithOverriddenAlpha):
     44        (WebCore::invertedcolorWithOverriddenAlpha): Deleted.
     45        Fix capitalization issue seen. invertedcolorWithOverriddenAlpha -> invertedColorWithOverriddenAlpha.
     46
     47        * platform/graphics/Color.cpp:
     48        (WebCore::Color::lightness const):
     49        Inline implementation and add comment explaining it should be removed.
     50
     51        (WebCore::Color::luminance const):
     52        Re-write to use the new WebCore::relativeLuminance that works for any color type
     53        without conversion to sRGB.
     54
     55        (WebCore::Color::contrastRatio):
     56        Add helper to call generic WebCore::contrastRatio that works on any color types
     57        to avoid callers needing to do the unfolding themselves.
     58 
     59        (WebCore::Color::isBlackColor):
     60        (WebCore::Color::isWhiteColor):
     61        * platform/graphics/Color.h:
     62        (WebCore::Color::isBlackColor): Deleted.
     63        (WebCore::Color::isWhiteColor): Deleted.
     64        Move these rare functions out of line to reduce the number of places in the header we are
     65        calling callOnUnderlyingType(), which produces code linerally with the number of color spaces
     66        supported. Calling it in the cpp files means we only expand it once for each function.
     67
     68        * platform/graphics/ColorLuminance.h: Added.
     69        (WebCore::relativeLuminance):
     70        This is a generic version of the old luminance function that works for any color type
     71        by converting to XYZ and taking the Y component. The old function required always
     72        converting to sRGB which could be lossy.
     73
     74        (WebCore::contrastRatio):
     75        Split out computation of contrastRatio based on relative luminace floats into its own
     76        function so that if we have the relative luminance computed already, we don't have to
     77        recompute it. Add version contrastRatio that works for any color type utilizing the
     78        generic relativeLuminance function above.
     79
     80        * rendering/RenderTheme.cpp:
     81        (WebCore::RenderTheme::disabledTextColor const):
     82        * rendering/TextPaintStyle.cpp:
     83        (WebCore::textColorIsLegibleAgainstBackgroundColor):
     84        Update to use new Color::contrastRatio helper that handles all color types.
     85
    1862021-03-01  Zalan Bujtas  <zalan@apple.com>
    287
  • trunk/Source/WebCore/Headers.cmake

    r273504 r273683  
    11671167    platform/graphics/ColorConversion.h
    11681168    platform/graphics/ColorHash.h
     1169    platform/graphics/ColorLuminance.h
    11691170    platform/graphics/ColorMatrix.h
    11701171    platform/graphics/ColorModels.h
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r273550 r273683  
    39733973                BC4918C90BFEA050009D6316 /* JSHTMLIFrameElement.h in Headers */ = {isa = PBXBuildFile; fileRef = BC4918C30BFEA050009D6316 /* JSHTMLIFrameElement.h */; };
    39743974                BC491B790C023EFD009D6316 /* JSHTMLMarqueeElement.h in Headers */ = {isa = PBXBuildFile; fileRef = BC491B770C023EFD009D6316 /* JSHTMLMarqueeElement.h */; };
     3975                BC4A23EC25EC160200AAC630 /* ColorLuminance.h in Headers */ = {isa = PBXBuildFile; fileRef = BC4A23EB25EC160200AAC630 /* ColorLuminance.h */; settings = {ATTRIBUTES = (Private, ); }; };
    39753976                BC4A5323256052760028C592 /* EditableLinkBehavior.h in Headers */ = {isa = PBXBuildFile; fileRef = BC4A5321256052700028C592 /* EditableLinkBehavior.h */; settings = {ATTRIBUTES = (Private, ); }; };
    39763977                BC4A53252560555F0028C592 /* TextDirectionSubmenuInclusionBehavior.h in Headers */ = {isa = PBXBuildFile; fileRef = BC4A5324256055590028C592 /* TextDirectionSubmenuInclusionBehavior.h */; settings = {ATTRIBUTES = (Private, ); }; };
     
    1403514036                BC491B760C023EFD009D6316 /* JSHTMLMarqueeElement.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = JSHTMLMarqueeElement.cpp; sourceTree = "<group>"; };
    1403614037                BC491B770C023EFD009D6316 /* JSHTMLMarqueeElement.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = JSHTMLMarqueeElement.h; sourceTree = "<group>"; };
     14038                BC4A23EB25EC160200AAC630 /* ColorLuminance.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ColorLuminance.h; sourceTree = "<group>"; };
    1403714039                BC4A5321256052700028C592 /* EditableLinkBehavior.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EditableLinkBehavior.h; sourceTree = "<group>"; };
    1403814040                BC4A5324256055590028C592 /* TextDirectionSubmenuInclusionBehavior.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TextDirectionSubmenuInclusionBehavior.h; sourceTree = "<group>"; };
     
    2666026662                                7CAC6AE8247F082000E61D59 /* ColorMatrix.h */,
    2666126663                                BC10137B25C3624B00DC773C /* ColorModels.h */,
     26664                                BC4A23EB25EC160200AAC630 /* ColorLuminance.h */,
    2666226665                                7CD1E69224ABF6240089C419 /* ColorSerialization.cpp */,
    2666326666                                7CD1E69124ABF6240089C419 /* ColorSerialization.h */,
     
    3114631149                                A1BF6B831AA96C7D00AF4A8A /* MockContentFilter.h in Headers */,
    3114731150                                A1B5B29F1AAA846F008B6042 /* MockContentFilterSettings.h in Headers */,
    31148                                 51058ADC1D6792C1009A538C /* MockGamepad.h in Headers */,
    31149                                 51058ADE1D6792C1009A538C /* MockGamepadProvider.h in Headers */,
    3115031151                                4157EBFB1E3AB67F00AC9FE9 /* MockLibWebRTCPeerConnection.h in Headers */,
    3115131152                                2D6F3E911C1ECB2F0061DBD4 /* MockPageOverlay.h in Headers */,
     
    3187331874                                715AD7212050513F00D592DC /* CSSTransition.h in Headers */,
    3187431875                                371F53E90D2704F900ECE0D5 /* CSSUnicodeRangeValue.h in Headers */,
     31876                                BC4A23EC25EC160200AAC630 /* ColorLuminance.h in Headers */,
    3187531877                                0F6B707A237BC36D0052CA47 /* CSSUnits.h in Headers */,
    3187631878                                DD7CDF250A23CF9800069928 /* CSSUnknownRule.h in Headers */,
  • trunk/Source/WebCore/css/CSSValueKeywords.in

    r273244 r273683  
    14351435xyz
    14361436
     1437// color-contast()
     1438color-contrast
     1439vs
     1440
    14371441// color-mix()
    14381442color-mix
  • trunk/Source/WebCore/css/parser/CSSParserContext.cpp

    r273244 r273683  
    6969    , useSystemAppearance { document.page() ? document.page()->useSystemAppearance() : false }
    7070    , aspectRatioEnabled { document.settings().aspectRatioEnabled() }
     71    , colorContrastEnabled { document.settings().cssColorContrastEnabled() }
    7172    , colorFilterEnabled { document.settings().colorFilterEnabled() }
    7273    , colorMixEnabled { document.settings().cssColorMixEnabled() }
     
    107108        && a.useSystemAppearance == b.useSystemAppearance
    108109        && a.aspectRatioEnabled == b.aspectRatioEnabled
     110        && a.colorContrastEnabled == b.colorContrastEnabled
    109111        && a.colorFilterEnabled == b.colorFilterEnabled
    110112        && a.colorMixEnabled == b.colorMixEnabled
  • trunk/Source/WebCore/css/parser/CSSParserContext.h

    r273244 r273683  
    5959    // Settings.
    6060    bool aspectRatioEnabled { false };
     61    bool colorContrastEnabled { false };
    6162    bool colorFilterEnabled { false };
    6263    bool colorMixEnabled { false };
     
    110111            & key.useSystemAppearance                       << 3
    111112            & key.aspectRatioEnabled                        << 4
    112             & key.colorFilterEnabled                        << 5
    113             & key.colorMixEnabled                           << 6
    114             & key.constantPropertiesEnabled                 << 7
    115             & key.deferredCSSParserEnabled                  << 8
    116             & key.enforcesCSSMIMETypeInNoQuirksMode         << 9
    117             & key.individualTransformPropertiesEnabled      << 10
     113            & key.colorContrastEnabled                      << 5
     114            & key.colorFilterEnabled                        << 6
     115            & key.colorMixEnabled                           << 7
     116            & key.constantPropertiesEnabled                 << 8
     117            & key.deferredCSSParserEnabled                  << 9
     118            & key.enforcesCSSMIMETypeInNoQuirksMode         << 10
     119            & key.individualTransformPropertiesEnabled      << 11
    118120#if ENABLE(OVERFLOW_SCROLLING_TOUCH)
    119             & key.legacyOverflowScrollingTouchEnabled       << 11
     121            & key.legacyOverflowScrollingTouchEnabled       << 12
    120122#endif
    121             & key.overscrollBehaviorEnabled                 << 12
    122             & key.relativeColorSyntaxEnabled                << 13
    123             & key.scrollBehaviorEnabled                     << 14
    124             & key.springTimingFunctionEnabled               << 15
     123            & key.overscrollBehaviorEnabled                 << 13
     124            & key.relativeColorSyntaxEnabled                << 14
     125            & key.scrollBehaviorEnabled                     << 15
     126            & key.springTimingFunctionEnabled               << 16
    125127#if ENABLE(TEXT_AUTOSIZING)
    126             & key.textAutosizingEnabled                     << 16
     128            & key.textAutosizingEnabled                     << 17
    127129#endif
    128130#if ENABLE(CSS_TRANSFORM_STYLE_OPTIMIZED_3D)
    129             & key.transformStyleOptimized3DEnabled          << 17
     131            & key.transformStyleOptimized3DEnabled          << 18
    130132#endif
    131             & key.useLegacyBackgroundSizeShorthandBehavior  << 18
    132             & key.focusVisibleEnabled                       << 19
     133            & key.useLegacyBackgroundSizeShorthandBehavior  << 19
     134            & key.focusVisibleEnabled                       << 20
    133135#if ENABLE(ATTACHMENT_ELEMENT)
    134             & key.attachmentEnabled                         << 20
     136            & key.attachmentEnabled                         << 21
    135137#endif
    136             & key.mode                                      << 21; // Keep this last.
     138            & key.mode                                      << 22; // Keep this last.
    137139        hash ^= WTF::intHash(bits);
    138140        return hash;
  • trunk/Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp

    r273428 r273683  
    4343#include "CSSValuePool.h"
    4444#include "ColorConversion.h"
     45#include "ColorLuminance.h"
    4546#include "Pair.h"
    4647#include "RuntimeEnabledFeatures.h"
     
    14831484}
    14841485
     1486static Color parseColorContrastFunctionParameters(CSSParserTokenRange& range, const CSSParserContext& context)
     1487{
     1488    ASSERT(range.peek().functionId() == CSSValueColorContrast);
     1489
     1490    if (!context.colorContrastEnabled)
     1491        return { };
     1492
     1493    auto args = consumeFunction(range);
     1494
     1495    auto originBackgroundColor = consumeOriginColor(args, context);
     1496    if (!originBackgroundColor.isValid())
     1497        return { };
     1498
     1499    if (!consumeIdentRaw<CSSValueVs>(args))
     1500        return { };
     1501
     1502    Vector<Color> colorsToCompareAgainst;
     1503    do {
     1504        auto colorToCompareAgainst = consumeOriginColor(args, context);
     1505        if (!colorToCompareAgainst.isValid())
     1506            return { };
     1507
     1508        colorsToCompareAgainst.append(WTFMove(colorToCompareAgainst));
     1509    } while (consumeCommaIncludingWhitespace(args));
     1510
     1511    if (colorsToCompareAgainst.size() == 1)
     1512        return { };
     1513
     1514    auto originBackgroundColorLuminance = originBackgroundColor.luminance();
     1515
     1516    size_t indexOfColorWithHigestContrastRatio = 0;
     1517    float highestContrastRatioSoFar = 0;
     1518    for (size_t i = 0; i < colorsToCompareAgainst.size(); ++i) {
     1519        auto contrastRatio = WebCore::contrastRatio(originBackgroundColorLuminance, colorsToCompareAgainst[i].luminance());
     1520        if (contrastRatio > highestContrastRatioSoFar) {
     1521            highestContrastRatioSoFar = contrastRatio;
     1522            indexOfColorWithHigestContrastRatio = i;
     1523        }
     1524    }
     1525
     1526    return colorsToCompareAgainst[indexOfColorWithHigestContrastRatio];
     1527}
     1528
    14851529struct HueColorAdjuster {
    14861530    enum class Type {
     
    19251969    case CSSValueColor:
    19261970        color = parseColorFunctionParameters(colorRange);
     1971        break;
     1972    case CSSValueColorContrast:
     1973        color = parseColorContrastFunctionParameters(colorRange, context);
    19271974        break;
    19281975    case CSSValueColorMix:
  • trunk/Source/WebCore/platform/graphics/Color.cpp

    r273211 r273683  
    2727#include "Color.h"
    2828
     29#include "ColorLuminance.h"
    2930#include "ColorSerialization.h"
    30 #include "ColorUtilities.h"
    3131#include <wtf/Assertions.h>
    3232#include <wtf/text/TextStream.h>
     
    112112float Color::lightness() const
    113113{
    114     // FIXME: This can probably avoid conversion to sRGB by having per-colorspace algorithms for HSL.
    115     return WebCore::lightness(toSRGBALossy<float>());
     114    // FIXME: Replace remaining uses with luminance.
     115    auto [r, g, b, a] = toSRGBALossy<float>();
     116    auto [min, max] = std::minmax({ r, g, b });
     117    return 0.5f * (max + min);
    116118}
    117119
    118120float Color::luminance() const
    119121{
    120     // FIXME: This can probably avoid conversion to sRGB by having per-colorspace algorithms
    121     // for luminance (e.g. convertToXYZ(c).yComponent()).
    122     return WebCore::luminance(toSRGBALossy<float>());
     122    return callOnUnderlyingType([&] (const auto& underlyingColor) {
     123        return WebCore::relativeLuminance(underlyingColor);
     124    });
     125}
     126
     127float Color::contrastRatio(const Color& colorA, const Color& colorB)
     128{
     129    return colorA.callOnUnderlyingType([&] (const auto& underlyingColorA) {
     130        return colorB.callOnUnderlyingType([&] (const auto& underlyingColorB) {
     131            return WebCore::contrastRatio(underlyingColorA, underlyingColorB);
     132        });
     133    });
    123134}
    124135
     
    145156        // of alternatives.
    146157        if constexpr (ColorType::Model::isInvertible)
    147             return invertedcolorWithOverriddenAlpha(underlyingColor, alpha);
     158            return invertedColorWithOverriddenAlpha(underlyingColor, alpha);
    148159        else
    149             return invertedcolorWithOverriddenAlpha(convertColor<SRGBA<float>>(underlyingColor), alpha);
     160            return invertedColorWithOverriddenAlpha(convertColor<SRGBA<float>>(underlyingColor), alpha);
    150161    });
    151162}
     
    168179}
    169180
     181bool Color::isBlackColor(const Color& color)
     182{
     183    return color.callOnUnderlyingType([] (const auto& underlyingColor) {
     184        return WebCore::isBlack(underlyingColor);
     185    });
     186}
     187
     188bool Color::isWhiteColor(const Color& color)
     189{
     190    return color.callOnUnderlyingType([] (const auto& underlyingColor) {
     191        return WebCore::isWhite(underlyingColor);
     192    });
     193}
     194
    170195TextStream& operator<<(TextStream& ts, const Color& color)
    171196{
  • trunk/Source/WebCore/platform/graphics/Color.h

    r273244 r273683  
    106106    WEBCORE_EXPORT float luminance() const;
    107107    WEBCORE_EXPORT float lightness() const; // FIXME: Replace remaining uses with luminance.
     108    WEBCORE_EXPORT static float contrastRatio(const Color&, const Color&);
    108109
    109110    template<typename Functor> decltype(auto) callOnUnderlyingType(Functor&&) const;
     
    493494    m_colorAndFlags = encodedExtendedColor(WTFMove(color)) | encodedFlags(flags);
    494495    ASSERT(isExtended());
    495 }
    496 
    497 inline bool Color::isBlackColor(const Color& color)
    498 {
    499     return color.callOnUnderlyingType([] (const auto& underlyingColor) {
    500         return WebCore::isBlack(underlyingColor);
    501     });
    502 }
    503 
    504 inline bool Color::isWhiteColor(const Color& color)
    505 {
    506     return color.callOnUnderlyingType([] (const auto& underlyingColor) {
    507         return WebCore::isWhite(underlyingColor);
    508     });
    509496}
    510497
  • trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp

    r272436 r273683  
    2727#include "ColorUtilities.h"
    2828
    29 #include "ColorConversion.h"
    30 
    3129namespace WebCore {
    32 
    33 float lightness(const SRGBA<float>& color)
    34 {
    35     auto [r, g, b, a] = color;
    36     auto [min, max] = std::minmax({ r, g, b });
    37     return 0.5f * (max + min);
    38 }
    39 
    40 float luminance(const SRGBA<float>& color)
    41 {
    42     // NOTE: This is the equivalent of convertColor<XYZA<float, WhitePoint::D65>>(color).y
    43     // FIMXE: If we can generalize ColorMatrix a bit more, it might be nice to write this as:
    44     //      return convertColor<LinearSRGBA<float>>(color) * linearSRGBToXYZMatrix.row(1);
    45     auto [r, g, b, a] = convertColor<LinearSRGBA<float>>(color);
    46     return 0.2126f * r + 0.7152f * g + 0.0722f * b;
    47 }
    48 
    49 float contrastRatio(const SRGBA<float>& colorA, const SRGBA<float>& colorB)
    50 {
    51     // Uses the WCAG 2.0 definition of contrast ratio.
    52     // https://www.w3.org/TR/WCAG20/#contrast-ratiodef
    53     float lighterLuminance = luminance(colorA);
    54     float darkerLuminance = luminance(colorB);
    55 
    56     if (lighterLuminance < darkerLuminance)
    57         std::swap(lighterLuminance, darkerLuminance);
    58 
    59     return (lighterLuminance + 0.05) / (darkerLuminance + 0.05);
    60 }
    6130
    6231SRGBA<float> premultiplied(const SRGBA<float>& color)
  • trunk/Source/WebCore/platform/graphics/ColorUtilities.h

    r273211 r273683  
    3535namespace WebCore {
    3636
    37 float lightness(const SRGBA<float>&);
    38 float luminance(const SRGBA<float>&);
    39 float contrastRatio(const SRGBA<float>&, const SRGBA<float>&);
    40 
    4137SRGBA<float> premultiplied(const SRGBA<float>&);
    4238SRGBA<float> unpremultiplied(const SRGBA<float>&);
     
    5652template<typename ColorType> ColorType colorWithOverriddenAlpha(const ColorType&, float overrideAlpha);
    5753
    58 template<typename ColorType> constexpr ColorType invertedcolorWithOverriddenAlpha(const ColorType&, uint8_t overrideAlpha);
    59 template<typename ColorType> ColorType invertedcolorWithOverriddenAlpha(const ColorType&, float overrideAlpha);
     54template<typename ColorType> constexpr ColorType invertedColorWithOverriddenAlpha(const ColorType&, uint8_t overrideAlpha);
     55template<typename ColorType> ColorType invertedColorWithOverriddenAlpha(const ColorType&, float overrideAlpha);
    6056
    6157template<typename ColorType, typename std::enable_if_t<std::is_same_v<typename ColorType::Model, RGBModel<typename ColorType::ComponentType>>>* = nullptr> constexpr bool isBlack(const ColorType&);
     
    122118}
    123119
    124 template<typename ColorType> constexpr ColorType invertedcolorWithOverriddenAlpha(const ColorType& color, uint8_t overrideAlpha)
     120template<typename ColorType> constexpr ColorType invertedColorWithOverriddenAlpha(const ColorType& color, uint8_t overrideAlpha)
    125121{
    126122    static_assert(ColorType::Model::isInvertible);
     
    136132}
    137133
    138 template<typename ColorType> ColorType invertedcolorWithOverriddenAlpha(const ColorType& color, float overrideAlpha)
     134template<typename ColorType> ColorType invertedColorWithOverriddenAlpha(const ColorType& color, float overrideAlpha)
    139135{
    140136    static_assert(ColorType::Model::isInvertible);
  • trunk/Source/WebCore/rendering/RenderTheme.cpp

    r272881 r273683  
    14081408    // just leave the text color alone. We don't want to change a good contrast color scheme so that it has really bad contrast.
    14091409    // If the contrast was already poor, then it doesn't do any good to change it to a different poor contrast color scheme.
    1410     if (contrastRatio(disabledColor.toSRGBALossy<float>(), backgroundColor.toSRGBALossy<float>()) < minColorContrastValue)
     1410    if (Color::contrastRatio(disabledColor, backgroundColor) < minColorContrastValue)
    14111411        return textColor;
    14121412
  • trunk/Source/WebCore/rendering/TextPaintStyle.cpp

    r263941 r273683  
    6464    // Uses the WCAG 2.0 definition of legibility: a contrast ratio of 4.5:1 or greater.
    6565    // https://www.w3.org/TR/WCAG20/#visual-audio-contrast-contrast
    66     return contrastRatio(textColor.toSRGBALossy<float>(), backgroundColor.toSRGBALossy<float>()) > 4.5;
     66    return Color::contrastRatio(textColor, backgroundColor) > 4.5;
    6767}
    6868
  • trunk/Tools/ChangeLog

    r273654 r273683  
     12021-03-01  Sam Weinig  <weinig@apple.com>
     2
     3        Add experimental support for CSS Color 5 color-contrast()
     4        https://bugs.webkit.org/show_bug.cgi?id=222530
     5
     6        Reviewed by Simon Fraser.
     7
     8        * TestWebKitAPI/Tests/WebCore/ColorTests.cpp:
     9        (TestWebKitAPI::TEST):
     10        Update luminance values to account for more accurate conversion to
     11        XYZ now that we are usuing the actual matrix values from SRGBADescriptor
     12        and not a truncated copy.
     13
    1142021-03-01  Megan Gardner  <megan_gardner@apple.com>
    215
  • trunk/Tools/TestWebKitAPI/Tests/WebCore/ColorTests.cpp

    r272870 r273683  
    215215
    216216    auto cComponents = SRGBA<uint8_t> { 85, 90, 160 };
    217     EXPECT_FLOAT_EQ(Color(cComponents).luminance(), 0.11781692);
     217    EXPECT_FLOAT_EQ(Color(cComponents).luminance(), 0.11781455);
    218218
    219219    EXPECT_EQ(cComponents.red, 85);
     
    222222
    223223    auto cLigtened = Color(cComponents).lightened().toSRGBALossy<uint8_t>();
    224     EXPECT_FLOAT_EQ(Color(cLigtened).luminance(), 0.29168808);
     224    EXPECT_FLOAT_EQ(Color(cLigtened).luminance(), 0.291682);
    225225    EXPECT_EQ(cLigtened.red, 130);
    226226    EXPECT_EQ(cLigtened.green, 137);
     
    228228
    229229    auto cDarkened = Color(cComponents).darkened().toSRGBALossy<uint8_t>();
    230     EXPECT_FLOAT_EQ(Color(cDarkened).luminance(), 0.027006727);
     230    EXPECT_FLOAT_EQ(Color(cDarkened).luminance(), 0.027006242);
    231231    EXPECT_EQ(cDarkened.red, 40);
    232232    EXPECT_EQ(cDarkened.green, 43);
Note: See TracChangeset for help on using the changeset viewer.