Extended Color: Unify rounding / clamping conversions between 0-1 float components...
authorweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 8 Jun 2020 19:37:20 +0000 (19:37 +0000)
committerweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 8 Jun 2020 19:37:20 +0000 (19:37 +0000)
commitcbe4d25057bf438f10a7f1494300e9bcccf45002
tree921c08a2001cf0b26070d26a64962f26f203eff4
parentb61fa3e1caf0e65591840f5e1c168c679e300fa0
Extended Color: Unify rounding / clamping conversions between 0-1 float components and 0-255 byte components
https://bugs.webkit.org/show_bug.cgi?id=212871

Reviewed by Simon Fraser.
Source/WebCore:

Unify all conversions of 0.0f - 1.0f float based color components to 0-255 int based color components
to use the new convertToComponentByte() function which scales, rounds (using lroundf) and clamps the
value. For consistency, a convertToComponentFloat() function, which just scales down an int based value
to a float based value, is also added.

- Removes *UsingAlternativeRounding variants (actually, we now only have this variant) for color functions
  which allowed callers to pick from truncation vs. rounding when overriding alpha.
- Replaces all uses of scaleRoundAndClampColorChannel() with convertToComponentByte() (really just a rename).
- Replaces uses of nextafter(256, 0) based conversions with convertToComponentByte().

Also:
- Moves roundAndClampColorChannel() functions to SVGAnimationAdditiveValueFunctionImpl.h, which was
  the only places they were used.
- Removes areEssentiallyEqual() overload taking ColorComponents<float>. It was ununsed.
- Removes makeSimpleColorFromHSLA(...) and just inlines makeSimpleColor(hslToSRGB(...)) which now
  does the same thing.

* css/parser/CSSPropertyParserHelpers.cpp:
(WebCore::CSSPropertyParserHelpers::clampRGBComponent):
Use convertPrescaledToComponentByte() to round and clamp the components.

(WebCore::CSSPropertyParserHelpers::parseRGBParameters):
Use uint8_t more consistently now that helpers ensure that is the return type
of conversion functions.

(WebCore::CSSPropertyParserHelpers::parseHSLParameters):
Switch to using makeSimpleColor(hslToSRGB(...)) directly.

* editing/cocoa/DataDetection.mm:
(WebCore::DataDetection::detectContentInRange):
Switch to using makeSimpleColor(hslToSRGB(...)) directly.

* html/HTMLElement.cpp:
(WebCore::parseLegacyColorValue):
Use uint8_t since that is what toASCIIHexValue() returns.

* html/canvas/CanvasRenderingContext2DBase.cpp:
(WebCore::CanvasRenderingContext2DBase::setStrokeStyle):
(WebCore::CanvasRenderingContext2DBase::setFillStyle):
(WebCore::CanvasRenderingContext2DBase::setShadow):
* html/canvas/CanvasStyle.cpp:
(WebCore::CanvasStyle::createFromStringWithOverrideAlpha):
Replaces uses of colorWithAlphaUsingAlternativeRounding() with colorWithAlpha().

* inspector/agents/InspectorDOMAgent.cpp:
(WebCore::parseColor):
Use convertToComponentByte() rather than a simple truncating cast.

* platform/graphics/Color.cpp:
(WebCore::Color::light const):
(WebCore::Color::dark const):
Use makeSimpleColorFromFloats() rather than nextafterf(256.0f, 0.0f) conversion.

(WebCore::Color::colorWithAlpha const):
(WebCore::Color::invertedColorWithAlpha const):
Use convertToComponentByte() for alpha conversion.

(WebCore::Color::toSRGBASimpleColorLossy const):
Call asExtended().toSRGBAComponentsLossy() directly to avoid unnecessary branch.

(WebCore::Color::colorWithAlphaMultipliedBy const): Deleted.
(WebCore::Color::colorWithAlphaMultipliedByUsingAlternativeRounding const): Deleted.
(WebCore::Color::colorWithAlphaUsingAlternativeRounding const): Deleted.
Remove UsingAlternativeRounding variants.

* platform/graphics/Color.h:
(WebCore::Color::alpha const):
Use convertToComponentByte() for alpha conversion.

(WebCore::Color::invertedColorWithAlpha const):
Add an overload taking an Optional<float> for consistency.

(WebCore::Color::colorWithAlphaMultipliedBy const):
(WebCore::Color::colorWithAlpha const):
Simplify by inlining all variants except the main colorWithAlpha().

* platform/graphics/ColorUtilities.cpp:
(WebCore::areEssentiallyEqual): Deleted.
Remove unused function.

* platform/graphics/ColorUtilities.h:
(WebCore::convertPrescaledToComponentByte):
Added. Useful for callers who already have prescaled values but need rounding/clamping.

(WebCore::convertToComponentByte):
Added. Bottleneck for float to byte based color component conversions.
(WebCore::convertToComponentFloat):
Added. Bottleneck for byte to float based color component conversions.

* platform/graphics/SimpleColor.cpp:
(WebCore::makeSimpleColorFromCMYKA):
Use makeSimpleColorFromFloats() rather than nextafterf(256.0f, 0.0f) conversion. Eventually,
when we probably want this to go away and store CMYKA colors as ExtendedColors with their own
color space.

(WebCore::makeSimpleColorFromFloats):
Moved to header.

* platform/graphics/SimpleColor.h:
(WebCore::SimpleColor::alphaComponentAsFloat const):
Use convertToComponentFloat().

(WebCore::SimpleColor::asSRGBFloatComponents const):
Use convertToComponentFloat().

(WebCore::makeSimpleColor):
Avoid unncessary clamping of the alpha component by calling constructor directly.

(WebCore::makeSimpleColorFromFloats):
Inlined. Calls convertToComponentByte now rather than the older (identical) scaleRoundAndClampColorChannel().

* platform/graphics/cairo/CairoOperations.cpp:
(WebCore::Cairo::prepareCairoContextSource):
Replaces use of colorWithAlphaMultipliedByUsingAlternativeRounding with colorWithAlphaMultipliedBy().

* platform/graphics/cg/ColorCG.cpp:
(WebCore::makeSimpleColorFromCGColor):
Use makeSimpleColorFromFloats() rather than nextafter(256.0, 0.0) conversion.

* platform/graphics/filters/FEFlood.cpp:
(WebCore::FEFlood::platformApplySoftware):
Replaces use of colorWithAlphaMultipliedByUsingAlternativeRounding with colorWithAlphaMultipliedBy().

* platform/graphics/mac/ColorMac.mm:
(WebCore::makeSimpleColorFromNSColor):
Use makeSimpleColorFromFloats() rather than nextafter(256.0, 0.0) conversion.

* platform/ios/ColorIOS.mm:
(WebCore::colorFromUIColor):
Use makeSimpleColorFromFloats() rather than nextafter(256.0, 0.0) conversion.

* svg/SVGStopElement.cpp:
(WebCore::SVGStopElement::stopColorIncludingOpacity const):
Replaces use of colorWithAlphaMultipliedByUsingAlternativeRounding with colorWithAlphaMultipliedBy().

* svg/properties/SVGAnimationAdditiveValueFunctionImpl.h:
(WebCore::SVGAnimationColorFunction::roundAndClampColorChannel):
Moved from ColorUtilities.h as this was the only use.

Source/WebKit:

* UIProcess/API/ios/WKWebViewIOS.mm:
(scrollViewBackgroundColor):
Replaces uses of colorWithAlphaUsingAlternativeRounding() with colorWithAlpha().

Tools:

* TestWebKitAPI/Tests/WebCore/ColorTests.cpp:
(TestWebKitAPI::TEST):
Switch to using makeSimpleColor(hslToSRGB(...)) directly.

LayoutTests:

* fast/css/ios/system-color-for-css-value-expected.txt:
* fast/css/ios/system-color-for-css-value.html:
Update test to account for slightly differnt rounding (alpha goes from 0.298 to 0.3).

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@262733 268f45cc-cd09-0410-ab3c-d52691b4dbfc
27 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/css/ios/system-color-for-css-value-expected.txt
LayoutTests/fast/css/ios/system-color-for-css-value.html
Source/WebCore/ChangeLog
Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp
Source/WebCore/editing/cocoa/DataDetection.mm
Source/WebCore/html/HTMLElement.cpp
Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp
Source/WebCore/html/canvas/CanvasStyle.cpp
Source/WebCore/inspector/agents/InspectorDOMAgent.cpp
Source/WebCore/platform/graphics/Color.cpp
Source/WebCore/platform/graphics/Color.h
Source/WebCore/platform/graphics/ColorUtilities.cpp
Source/WebCore/platform/graphics/ColorUtilities.h
Source/WebCore/platform/graphics/SimpleColor.cpp
Source/WebCore/platform/graphics/SimpleColor.h
Source/WebCore/platform/graphics/cairo/CairoOperations.cpp
Source/WebCore/platform/graphics/cg/ColorCG.cpp
Source/WebCore/platform/graphics/filters/FEFlood.cpp
Source/WebCore/platform/graphics/mac/ColorMac.mm
Source/WebCore/platform/ios/ColorIOS.mm
Source/WebCore/svg/SVGStopElement.cpp
Source/WebCore/svg/properties/SVGAnimationAdditiveValueFunctionImpl.h
Source/WebKit/ChangeLog
Source/WebKit/UIProcess/API/ios/WKWebViewIOS.mm
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebCore/ColorTests.cpp