From 76dea7243010f7a3aada8d7f42ad336d6ca153ba Mon Sep 17 00:00:00 2001 From: Sam Weinig Date: Sun, 24 May 2020 19:13:02 +0000 Subject: [PATCH] Extended Color Cleanup: Move SimpleColor into its own files https://bugs.webkit.org/show_bug.cgi?id=212309 Reviewed by Simon Fraser. Move SimpleColor into its own files. It's about time. * Headers.cmake: * Sources.txt: * WebCore.xcodeproj/project.pbxproj: * platform/graphics/Color.cpp: (WebCore::premultipliedChannel): Deleted. (WebCore::unpremultipliedChannel): Deleted. (WebCore::makePremultipliedRGBA): Deleted. (WebCore::makeUnPremultipliedRGBA): Deleted. (WebCore::colorFloatToRGBAByte): Deleted. (WebCore::makeRGBA32FromFloats): Deleted. (WebCore::makeRGBAFromHSLA): Deleted. (WebCore::makeRGBAFromCMYKA): Deleted. (WebCore::SimpleColor::serializationForHTML const): Deleted. (WebCore::decimalDigit): Deleted. (WebCore::fractionDigitsForFractionalAlphaValue): Deleted. (WebCore::SimpleColor::serializationForCSS const): Deleted. (WebCore::RGBA32::serializationForRenderTreeAsText const): Deleted. * platform/graphics/Color.h: (WebCore::SimpleColor::SimpleColor): Deleted. (WebCore::SimpleColor::value const): Deleted. (WebCore::SimpleColor::redComponent const): Deleted. (WebCore::SimpleColor::greenComponent const): Deleted. (WebCore::SimpleColor::blueComponent const): Deleted. (WebCore::SimpleColor::alphaComponent const): Deleted. (WebCore::SimpleColor::alphaComponentAsFloat const): Deleted. (WebCore::SimpleColor::isOpaque const): Deleted. (WebCore::SimpleColor::isVisible const): Deleted. (WebCore::SimpleColor::colorWithAlpha const): Deleted. (WebCore::SimpleColor::get const): Deleted. (WebCore::roundAndClampColorChannel): Deleted. (WebCore::fastMultiplyBy255): Deleted. (WebCore::fastDivideBy255): Deleted. (WebCore::makeRGB): Deleted. (WebCore::makeRGBA): Deleted. * platform/graphics/SimpleColor.cpp: Copied from platform/graphics/Color.cpp. (WebCore::SimpleColor::serializationForRenderTreeAsText const): (): Deleted. (WebCore::colorFloatToRGBAByte): Deleted. (WebCore::parseHexColorInternal): Deleted. (WebCore::Color::parseHexColor): Deleted. (WebCore::differenceSquared): Deleted. (WebCore::findNamedColor): Deleted. (WebCore::Color::Color): Deleted. (WebCore::Color::operator=): Deleted. (WebCore::Color::serialized const): Deleted. (WebCore::Color::cssText const): Deleted. (WebCore::RGBA32::serializationForRenderTreeAsText const): Deleted. (WebCore::Color::nameForRenderTreeAsText const): Deleted. (WebCore::Color::light const): Deleted. (WebCore::Color::dark const): Deleted. (WebCore::Color::isDark const): Deleted. (WebCore::Color::lightness const): Deleted. (WebCore::blendComponent): Deleted. (WebCore::Color::blend const): Deleted. (WebCore::Color::blendWithWhite const): Deleted. (WebCore::Color::colorWithAlphaMultipliedBy const): Deleted. (WebCore::Color::colorWithAlphaMultipliedByUsingAlternativeRounding const): Deleted. (WebCore::Color::colorWithAlpha const): Deleted. (WebCore::Color::colorWithAlphaUsingAlternativeRounding const): Deleted. (WebCore::Color::colorSpaceAndComponents const): Deleted. (WebCore::Color::toSRGBASimpleColorLossy const): Deleted. (WebCore::Color::toSRGBAComponentsLossy const): Deleted. (WebCore::extendedColorsEqual): Deleted. (WebCore::blend): Deleted. (WebCore::blendWithoutPremultiply): Deleted. (WebCore::Color::tagAsValid): Deleted. (WebCore::Color::asExtended const): Deleted. (WebCore::operator<<): Deleted. * platform/graphics/SimpleColor.h: Copied from platform/graphics/Color.h. (WebCore::SimpleColor::alphaComponentAsFloat const): (WebCore::SimpleColor::colorWithAlpha const): (WebCore::SimpleColor::get const): (WebCore::colorFloatToRGBAByte): (WebCore::Color::Color): Deleted. (WebCore::Color::isHashTableDeletedValue const): Deleted. (WebCore::Color::~Color): Deleted. (WebCore::Color::isValid const): Deleted. (WebCore::Color::isOpaque const): Deleted. (WebCore::Color::isVisible const): Deleted. (WebCore::Color::red const): Deleted. (WebCore::Color::green const): Deleted. (WebCore::Color::blue const): Deleted. (WebCore::Color::alpha const): Deleted. (WebCore::Color::alphaAsFloat const): Deleted. (WebCore::Color::opaqueColor const): Deleted. (WebCore::Color::isSemantic const): Deleted. (WebCore::Color::isExtended const): Deleted. (WebCore::Color::setRGB): Deleted. (WebCore::Color::setIsSemantic): Deleted. (WebCore::equalIgnoringSemanticColor): Deleted. (WebCore::Color::hash const): Deleted. (WebCore::Color::colorWithAlphaMultipliedByUsingAlternativeRounding const): Deleted. (WebCore::Color::colorWithAlphaUsingAlternativeRounding const): Deleted. (WebCore::Color::rgb const): Deleted. (WebCore::Color::isBlackColor): Deleted. (WebCore::Color::isWhiteColor): Deleted. (WebCore::Color::encode const): Deleted. (WebCore::Color::decode): Deleted. Canonical link: https://commits.webkit.org/225173@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@262110 268f45cc-cd09-0410-ab3c-d52691b4dbfc --- Source/WebCore/ChangeLog | 108 +++++++++++ Source/WebCore/Headers.cmake | 1 + Source/WebCore/Sources.txt | 1 + .../WebCore/WebCore.xcodeproj/project.pbxproj | 6 + Source/WebCore/platform/graphics/Color.cpp | 108 ----------- Source/WebCore/platform/graphics/Color.h | 125 +------------ .../WebCore/platform/graphics/SimpleColor.cpp | 138 ++++++++++++++ .../WebCore/platform/graphics/SimpleColor.h | 168 ++++++++++++++++++ 8 files changed, 423 insertions(+), 232 deletions(-) create mode 100644 Source/WebCore/platform/graphics/SimpleColor.cpp create mode 100644 Source/WebCore/platform/graphics/SimpleColor.h diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog index 8d9a79e6a697..c44ecf25fa8a 100644 --- a/Source/WebCore/ChangeLog +++ b/Source/WebCore/ChangeLog @@ -1,3 +1,111 @@ +2020-05-24 Sam Weinig + + Extended Color Cleanup: Move SimpleColor into its own files + https://bugs.webkit.org/show_bug.cgi?id=212309 + + Reviewed by Simon Fraser. + + Move SimpleColor into its own files. It's about time. + + * Headers.cmake: + * Sources.txt: + * WebCore.xcodeproj/project.pbxproj: + * platform/graphics/Color.cpp: + (WebCore::premultipliedChannel): Deleted. + (WebCore::unpremultipliedChannel): Deleted. + (WebCore::makePremultipliedRGBA): Deleted. + (WebCore::makeUnPremultipliedRGBA): Deleted. + (WebCore::colorFloatToRGBAByte): Deleted. + (WebCore::makeRGBA32FromFloats): Deleted. + (WebCore::makeRGBAFromHSLA): Deleted. + (WebCore::makeRGBAFromCMYKA): Deleted. + (WebCore::SimpleColor::serializationForHTML const): Deleted. + (WebCore::decimalDigit): Deleted. + (WebCore::fractionDigitsForFractionalAlphaValue): Deleted. + (WebCore::SimpleColor::serializationForCSS const): Deleted. + (WebCore::RGBA32::serializationForRenderTreeAsText const): Deleted. + * platform/graphics/Color.h: + (WebCore::SimpleColor::SimpleColor): Deleted. + (WebCore::SimpleColor::value const): Deleted. + (WebCore::SimpleColor::redComponent const): Deleted. + (WebCore::SimpleColor::greenComponent const): Deleted. + (WebCore::SimpleColor::blueComponent const): Deleted. + (WebCore::SimpleColor::alphaComponent const): Deleted. + (WebCore::SimpleColor::alphaComponentAsFloat const): Deleted. + (WebCore::SimpleColor::isOpaque const): Deleted. + (WebCore::SimpleColor::isVisible const): Deleted. + (WebCore::SimpleColor::colorWithAlpha const): Deleted. + (WebCore::SimpleColor::get const): Deleted. + (WebCore::roundAndClampColorChannel): Deleted. + (WebCore::fastMultiplyBy255): Deleted. + (WebCore::fastDivideBy255): Deleted. + (WebCore::makeRGB): Deleted. + (WebCore::makeRGBA): Deleted. + * platform/graphics/SimpleColor.cpp: Copied from platform/graphics/Color.cpp. + (WebCore::SimpleColor::serializationForRenderTreeAsText const): + (): Deleted. + (WebCore::colorFloatToRGBAByte): Deleted. + (WebCore::parseHexColorInternal): Deleted. + (WebCore::Color::parseHexColor): Deleted. + (WebCore::differenceSquared): Deleted. + (WebCore::findNamedColor): Deleted. + (WebCore::Color::Color): Deleted. + (WebCore::Color::operator=): Deleted. + (WebCore::Color::serialized const): Deleted. + (WebCore::Color::cssText const): Deleted. + (WebCore::RGBA32::serializationForRenderTreeAsText const): Deleted. + (WebCore::Color::nameForRenderTreeAsText const): Deleted. + (WebCore::Color::light const): Deleted. + (WebCore::Color::dark const): Deleted. + (WebCore::Color::isDark const): Deleted. + (WebCore::Color::lightness const): Deleted. + (WebCore::blendComponent): Deleted. + (WebCore::Color::blend const): Deleted. + (WebCore::Color::blendWithWhite const): Deleted. + (WebCore::Color::colorWithAlphaMultipliedBy const): Deleted. + (WebCore::Color::colorWithAlphaMultipliedByUsingAlternativeRounding const): Deleted. + (WebCore::Color::colorWithAlpha const): Deleted. + (WebCore::Color::colorWithAlphaUsingAlternativeRounding const): Deleted. + (WebCore::Color::colorSpaceAndComponents const): Deleted. + (WebCore::Color::toSRGBASimpleColorLossy const): Deleted. + (WebCore::Color::toSRGBAComponentsLossy const): Deleted. + (WebCore::extendedColorsEqual): Deleted. + (WebCore::blend): Deleted. + (WebCore::blendWithoutPremultiply): Deleted. + (WebCore::Color::tagAsValid): Deleted. + (WebCore::Color::asExtended const): Deleted. + (WebCore::operator<<): Deleted. + * platform/graphics/SimpleColor.h: Copied from platform/graphics/Color.h. + (WebCore::SimpleColor::alphaComponentAsFloat const): + (WebCore::SimpleColor::colorWithAlpha const): + (WebCore::SimpleColor::get const): + (WebCore::colorFloatToRGBAByte): + (WebCore::Color::Color): Deleted. + (WebCore::Color::isHashTableDeletedValue const): Deleted. + (WebCore::Color::~Color): Deleted. + (WebCore::Color::isValid const): Deleted. + (WebCore::Color::isOpaque const): Deleted. + (WebCore::Color::isVisible const): Deleted. + (WebCore::Color::red const): Deleted. + (WebCore::Color::green const): Deleted. + (WebCore::Color::blue const): Deleted. + (WebCore::Color::alpha const): Deleted. + (WebCore::Color::alphaAsFloat const): Deleted. + (WebCore::Color::opaqueColor const): Deleted. + (WebCore::Color::isSemantic const): Deleted. + (WebCore::Color::isExtended const): Deleted. + (WebCore::Color::setRGB): Deleted. + (WebCore::Color::setIsSemantic): Deleted. + (WebCore::equalIgnoringSemanticColor): Deleted. + (WebCore::Color::hash const): Deleted. + (WebCore::Color::colorWithAlphaMultipliedByUsingAlternativeRounding const): Deleted. + (WebCore::Color::colorWithAlphaUsingAlternativeRounding const): Deleted. + (WebCore::Color::rgb const): Deleted. + (WebCore::Color::isBlackColor): Deleted. + (WebCore::Color::isWhiteColor): Deleted. + (WebCore::Color::encode const): Deleted. + (WebCore::Color::decode): Deleted. + 2020-05-24 Zalan Bujtas [LFC][TFC] Ignore section borders even when border collapse is off. diff --git a/Source/WebCore/Headers.cmake b/Source/WebCore/Headers.cmake index b589dfc95410..9badf454b056 100644 --- a/Source/WebCore/Headers.cmake +++ b/Source/WebCore/Headers.cmake @@ -1184,6 +1184,7 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS platform/graphics/RemoteVideoSample.h platform/graphics/RenderingMode.h platform/graphics/RoundedRect.h + platform/graphics/SimpleColor.h platform/graphics/StringTruncator.h platform/graphics/TabSize.h platform/graphics/TextRun.h diff --git a/Source/WebCore/Sources.txt b/Source/WebCore/Sources.txt index ccd6728b84e4..5183b9e38751 100644 --- a/Source/WebCore/Sources.txt +++ b/Source/WebCore/Sources.txt @@ -1937,6 +1937,7 @@ platform/graphics/Region.cpp platform/graphics/RemoteVideoSample.cpp platform/graphics/RoundedRect.cpp platform/graphics/ShadowBlur.cpp +platform/graphics/SimpleColor.cpp platform/graphics/StringTruncator.cpp platform/graphics/SurrogatePairAwareTextIterator.cpp platform/graphics/TextRun.cpp diff --git a/Source/WebCore/WebCore.xcodeproj/project.pbxproj b/Source/WebCore/WebCore.xcodeproj/project.pbxproj index 262dcda28c30..d86d2ad50f17 100644 --- a/Source/WebCore/WebCore.xcodeproj/project.pbxproj +++ b/Source/WebCore/WebCore.xcodeproj/project.pbxproj @@ -2241,6 +2241,7 @@ 7C2FA6121EA95A3C00A03108 /* ResourceCryptographicDigest.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C2FA6101EA95A3200A03108 /* ResourceCryptographicDigest.h */; settings = {ATTRIBUTES = (Private, ); }; }; 7C30D9841F815AEC00268356 /* JSAbortController.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C30D9801F815AC100268356 /* JSAbortController.h */; }; 7C30D9861F815AEC00268356 /* JSAbortSignal.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C30D9811F815AC100268356 /* JSAbortSignal.h */; }; + 7C31C88E247AD595005BF319 /* SimpleColor.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C31C88C247AD595005BF319 /* SimpleColor.h */; settings = {ATTRIBUTES = (Private, ); }; }; 7C330A021DF8FAC600D3395C /* GraphicsContextGLAttributes.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C330A011DF8FAC600D3395C /* GraphicsContextGLAttributes.h */; settings = {ATTRIBUTES = (Private, ); }; }; 7C330A081DF9F95100D3395C /* JSPositionOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C330A061DF9F95100D3395C /* JSPositionOptions.h */; }; 7C3A91E61C963B8800D1A7E3 /* ClipboardAccessPolicy.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C3A91E51C963B8800D1A7E3 /* ClipboardAccessPolicy.h */; settings = {ATTRIBUTES = (Private, ); }; }; @@ -9974,6 +9975,8 @@ 7C30D9801F815AC100268356 /* JSAbortController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSAbortController.h; sourceTree = ""; }; 7C30D9811F815AC100268356 /* JSAbortSignal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSAbortSignal.h; sourceTree = ""; }; 7C30D9821F815AC200268356 /* JSAbortSignal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSAbortSignal.cpp; sourceTree = ""; }; + 7C31C88A247AD594005BF319 /* SimpleColor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SimpleColor.cpp; sourceTree = ""; }; + 7C31C88C247AD595005BF319 /* SimpleColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleColor.h; sourceTree = ""; }; 7C330A011DF8FAC600D3395C /* GraphicsContextGLAttributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GraphicsContextGLAttributes.h; sourceTree = ""; }; 7C330A031DF9E95B00D3395C /* PositionOptions.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = PositionOptions.idl; sourceTree = ""; }; 7C330A051DF9F95100D3395C /* JSPositionOptions.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSPositionOptions.cpp; sourceTree = ""; }; @@ -25594,6 +25597,8 @@ A73F95FD12C97BFE0031AAF9 /* RoundedRect.h */, 0F3DD44D12F5EA1B000D9190 /* ShadowBlur.cpp */, 0F3DD44E12F5EA1B000D9190 /* ShadowBlur.h */, + 7C31C88A247AD594005BF319 /* SimpleColor.cpp */, + 7C31C88C247AD595005BF319 /* SimpleColor.h */, CD641EB21818F5ED00EE4C41 /* SourceBufferPrivate.h */, CDC8B5AC1804AE5D0016E685 /* SourceBufferPrivateClient.h */, 7C83DE851D04CBD400FEBCF3 /* SpringSolver.h */, @@ -33537,6 +33542,7 @@ CD336F6417FA0A4D00DDDCD0 /* VideoTrackPrivateAVF.h in Headers */, CD336F6817FA0AC600DDDCD0 /* VideoTrackPrivateAVFObjC.h in Headers */, CD8B5A43180D149A008B8E65 /* VideoTrackPrivateMediaSourceAVFObjC.h in Headers */, + 7C31C88E247AD595005BF319 /* SimpleColor.h in Headers */, 070E81D11BF27656001FDA48 /* VideoTrackPrivateMediaStream.h in Headers */, CEF418CF1179678C009D112C /* ViewportArguments.h in Headers */, 26F9A83918A046AC00AEB88A /* ViewportConfiguration.h in Headers */, diff --git a/Source/WebCore/platform/graphics/Color.cpp b/Source/WebCore/platform/graphics/Color.cpp index 4006064edc04..e38bfc2222ba 100644 --- a/Source/WebCore/platform/graphics/Color.cpp +++ b/Source/WebCore/platform/graphics/Color.cpp @@ -40,71 +40,6 @@ namespace WebCore { static constexpr SimpleColor lightenedBlack { 0xFF545454 }; static constexpr SimpleColor darkenedWhite { 0xFFABABAB }; -static inline unsigned premultipliedChannel(unsigned c, unsigned a, bool ceiling = true) -{ - return fastDivideBy255(ceiling ? c * a + 254 : c * a); -} - -static inline unsigned unpremultipliedChannel(unsigned c, unsigned a) -{ - return (fastMultiplyBy255(c) + a - 1) / a; -} - -RGBA32 makePremultipliedRGBA(int r, int g, int b, int a, bool ceiling) -{ - return makeRGBA(premultipliedChannel(r, a, ceiling), premultipliedChannel(g, a, ceiling), premultipliedChannel(b, a, ceiling), a); -} - -RGBA32 makePremultipliedRGBA(RGBA32 pixelColor) -{ - if (pixelColor.isOpaque()) - return pixelColor; - return makePremultipliedRGBA(pixelColor.redComponent(), pixelColor.greenComponent(), pixelColor.blueComponent(), pixelColor.alphaComponent()); -} - -RGBA32 makeUnPremultipliedRGBA(int r, int g, int b, int a) -{ - return makeRGBA(unpremultipliedChannel(r, a), unpremultipliedChannel(g, a), unpremultipliedChannel(b, a), a); -} - -RGBA32 makeUnPremultipliedRGBA(RGBA32 pixelColor) -{ - if (pixelColor.isVisible() && !pixelColor.isOpaque()) - return makeUnPremultipliedRGBA(pixelColor.redComponent(), pixelColor.greenComponent(), pixelColor.blueComponent(), pixelColor.alphaComponent()); - return pixelColor; -} - -static int colorFloatToRGBAByte(float f) -{ - // We use lroundf and 255 instead of nextafterf(256, 0) to match CG's rounding - return std::max(0, std::min(static_cast(lroundf(255.0f * f)), 255)); -} - -RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a) -{ - return makeRGBA(colorFloatToRGBAByte(r), colorFloatToRGBAByte(g), colorFloatToRGBAByte(b), colorFloatToRGBAByte(a)); -} - -RGBA32 makeRGBAFromHSLA(float hue, float saturation, float lightness, float alpha) -{ - const float scaleFactor = 255.0; - FloatComponents floatResult = hslToSRGB({ hue, saturation, lightness, alpha }); - return makeRGBA( - round(floatResult.components[0] * scaleFactor), - round(floatResult.components[1] * scaleFactor), - round(floatResult.components[2] * scaleFactor), - round(floatResult.components[3] * scaleFactor)); -} - -RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a) -{ - double colors = 1 - k; - int r = static_cast(nextafter(256, 0) * (colors * (1 - c))); - int g = static_cast(nextafter(256, 0) * (colors * (1 - m))); - int b = static_cast(nextafter(256, 0) * (colors * (1 - y))); - return makeRGBA(r, g, b, static_cast(nextafter(256, 0) * a)); -} - // originally moved here from the CSS parser template static inline bool parseHexColorInternal(const CharacterType* name, unsigned length, RGBA32& rgb) @@ -292,13 +227,6 @@ Color& Color::operator=(Color&& other) return *this; } -String SimpleColor::serializationForHTML() const -{ - if (isOpaque()) - return makeString('#', hex(redComponent(), 2, Lowercase), hex(greenComponent(), 2, Lowercase), hex(blueComponent(), 2, Lowercase)); - return serializationForCSS(); -} - String Color::serialized() const { if (isExtended()) @@ -306,35 +234,6 @@ String Color::serialized() const return rgb().serializationForHTML(); } -static char decimalDigit(unsigned number) -{ - ASSERT(number < 10); - return '0' + number; -} - -static std::array fractionDigitsForFractionalAlphaValue(uint8_t alpha) -{ - ASSERT(alpha > 0); - ASSERT(alpha < 0xFF); - if (((alpha * 100 + 0x7F) / 0xFF * 0xFF + 50) / 100 != alpha) - return { { decimalDigit(alpha * 10 / 0xFF % 10), decimalDigit(alpha * 100 / 0xFF % 10), decimalDigit((alpha * 1000 + 0x7F) / 0xFF % 10), '\0' } }; - if (int thirdDigit = (alpha * 100 + 0x7F) / 0xFF % 10) - return { { decimalDigit(alpha * 10 / 0xFF), decimalDigit(thirdDigit), '\0', '\0' } }; - return { { decimalDigit((alpha * 10 + 0x7F) / 0xFF), '\0', '\0', '\0' } }; -} - -String SimpleColor::serializationForCSS() const -{ - switch (alphaComponent()) { - case 0: - return makeString("rgba(", redComponent(), ", ", greenComponent(), ", ", blueComponent(), ", 0)"); - case 0xFF: - return makeString("rgb(", redComponent(), ", ", greenComponent(), ", ", blueComponent(), ')'); - default: - return makeString("rgba(", redComponent(), ", ", greenComponent(), ", ", blueComponent(), ", 0.", fractionDigitsForFractionalAlphaValue(alphaComponent()).data(), ')'); - } -} - String Color::cssText() const { if (isExtended()) @@ -342,13 +241,6 @@ String Color::cssText() const return rgb().serializationForCSS(); } -String RGBA32::serializationForRenderTreeAsText() const -{ - if (alphaComponent() < 0xFF) - return makeString('#', hex(redComponent(), 2), hex(greenComponent(), 2), hex(blueComponent(), 2), hex(alphaComponent(), 2)); - return makeString('#', hex(redComponent(), 2), hex(greenComponent(), 2), hex(blueComponent(), 2)); -} - String Color::nameForRenderTreeAsText() const { // FIXME: Handle extended colors. diff --git a/Source/WebCore/platform/graphics/Color.h b/Source/WebCore/platform/graphics/Color.h index 0eec8c1a6778..bc1b49c197c4 100644 --- a/Source/WebCore/platform/graphics/Color.h +++ b/Source/WebCore/platform/graphics/Color.h @@ -27,6 +27,7 @@ #include "ColorSpace.h" #include "ExtendedColor.h" +#include "SimpleColor.h" #include #include #include @@ -56,69 +57,6 @@ namespace WebCore { struct FloatComponents; -// Color value with 8-bit components for red, green, blue, and alpha. -// For historical reasons, stored as a 32-bit integer, with alpha in the high bits: ARGB. -class SimpleColor { -public: - constexpr SimpleColor(uint32_t value = 0) : m_value { value } { } - - constexpr uint32_t value() const { return m_value; } - - constexpr uint8_t redComponent() const { return m_value >> 16; } - constexpr uint8_t greenComponent() const { return m_value >> 8; } - constexpr uint8_t blueComponent() const { return m_value; } - constexpr uint8_t alphaComponent() const { return m_value >> 24; } - - constexpr float alphaComponentAsFloat() const { return static_cast(alphaComponent()) / 0xFF; } - - constexpr bool isOpaque() const { return alphaComponent() == 0xFF; } - constexpr bool isVisible() const { return alphaComponent(); } - - String serializationForHTML() const; - String serializationForCSS() const; - String serializationForRenderTreeAsText() const; - - constexpr SimpleColor colorWithAlpha(uint8_t alpha) const { return { (m_value & 0x00FFFFFF) | alpha << 24 }; } - - template - constexpr uint8_t get() const - { - static_assert(N < 4); - if constexpr (!N) - return redComponent(); - else if constexpr (N == 1) - return greenComponent(); - else if constexpr (N == 2) - return blueComponent(); - else if constexpr (N == 3) - return alphaComponent(); - } - -private: - uint32_t m_value { 0 }; -}; - -bool operator==(SimpleColor, SimpleColor); -bool operator!=(SimpleColor, SimpleColor); - -// FIXME: Remove this after migrating to the new name. -using RGBA32 = SimpleColor; - -constexpr RGBA32 makeRGB(int r, int g, int b); -constexpr RGBA32 makeRGBA(int r, int g, int b, int a); - -RGBA32 makePremultipliedRGBA(int r, int g, int b, int a, bool ceiling = true); -RGBA32 makePremultipliedRGBA(RGBA32); -RGBA32 makeUnPremultipliedRGBA(int r, int g, int b, int a); -RGBA32 makeUnPremultipliedRGBA(RGBA32); - -WEBCORE_EXPORT RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a); -WEBCORE_EXPORT RGBA32 makeRGBAFromHSLA(float h, float s, float l, float a); -RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a); - -uint8_t roundAndClampColorChannel(int); -uint8_t roundAndClampColorChannel(float); - class Color { WTF_MAKE_FAST_ALLOCATED; public: @@ -350,9 +288,6 @@ Color blendWithoutPremultiply(const Color& from, const Color& to, double progres int differenceSquared(const Color&, const Color&); -uint16_t fastMultiplyBy255(uint16_t value); -uint16_t fastDivideBy255(uint16_t); - #if USE(CG) WEBCORE_EXPORT CGColorRef cachedCGColor(const Color&); #endif @@ -360,16 +295,6 @@ WEBCORE_EXPORT CGColorRef cachedCGColor(const Color&); WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const Color&); WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, ColorSpace); -inline bool operator==(SimpleColor a, SimpleColor b) -{ - return a.value() == b.value(); -} - -inline bool operator!=(SimpleColor a, SimpleColor b) -{ - return !(a == b); -} - inline bool operator==(const Color& a, const Color& b) { if (a.isExtended() || b.isExtended()) @@ -398,30 +323,6 @@ inline unsigned Color::hash() const return WTF::intHash(m_colorData.rgbaAndFlags); } -inline uint8_t roundAndClampColorChannel(int value) -{ - return std::max(0, std::min(255, value)); -} - -inline uint8_t roundAndClampColorChannel(float value) -{ - return std::max(0.f, std::min(255.f, std::round(value))); -} - -inline uint16_t fastMultiplyBy255(uint16_t value) -{ - return (value << 8) - value; -} - -inline uint16_t fastDivideBy255(uint16_t value) -{ - // While this is an approximate algorithm for division by 255, it gives perfectly accurate results for 16-bit values. - // FIXME: Since this gives accurate results for 16-bit values, we should get this optimization into compilers like clang. - uint16_t approximation = value >> 8; - uint16_t remainder = value - (approximation * 255) + 1; - return approximation + (remainder >> 8); -} - inline Color Color::colorWithAlphaMultipliedByUsingAlternativeRounding(Optional alpha) const { return alpha ? colorWithAlphaMultipliedByUsingAlternativeRounding(alpha.value()) : *this; @@ -534,33 +435,9 @@ Optional Color::decode(Decoder& decoder) return Color { SimpleColor { value } }; } -constexpr RGBA32 makeRGB(int r, int g, int b) -{ - return makeRGBA(r, g, b, 0xFF); -} - -constexpr RGBA32 makeRGBA(int r, int g, int b, int a) -{ - return { static_cast(std::max(0, std::min(a, 0xFF)) << 24 | std::max(0, std::min(r, 0xFF)) << 16 | std::max(0, std::min(g, 0xFF)) << 8 | std::max(0, std::min(b, 0xFF))) }; -} - } // namespace WebCore namespace WTF { template<> struct DefaultHash; template<> struct HashTraits; } - -namespace std { - -template<> -class tuple_size : public std::integral_constant { -}; - -template -class tuple_element { -public: - using type = uint8_t; -}; - -} diff --git a/Source/WebCore/platform/graphics/SimpleColor.cpp b/Source/WebCore/platform/graphics/SimpleColor.cpp new file mode 100644 index 000000000000..5ded331ce1f3 --- /dev/null +++ b/Source/WebCore/platform/graphics/SimpleColor.cpp @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2003-2020 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "SimpleColor.h" + +#include +#include +#include +#include + +namespace WebCore { + +static inline unsigned premultipliedChannel(unsigned c, unsigned a, bool ceiling = true) +{ + return fastDivideBy255(ceiling ? c * a + 254 : c * a); +} + +static inline unsigned unpremultipliedChannel(unsigned c, unsigned a) +{ + return (fastMultiplyBy255(c) + a - 1) / a; +} + +RGBA32 makePremultipliedRGBA(int r, int g, int b, int a, bool ceiling) +{ + return makeRGBA(premultipliedChannel(r, a, ceiling), premultipliedChannel(g, a, ceiling), premultipliedChannel(b, a, ceiling), a); +} + +RGBA32 makePremultipliedRGBA(RGBA32 pixelColor) +{ + if (pixelColor.isOpaque()) + return pixelColor; + return makePremultipliedRGBA(pixelColor.redComponent(), pixelColor.greenComponent(), pixelColor.blueComponent(), pixelColor.alphaComponent()); +} + +RGBA32 makeUnPremultipliedRGBA(int r, int g, int b, int a) +{ + return makeRGBA(unpremultipliedChannel(r, a), unpremultipliedChannel(g, a), unpremultipliedChannel(b, a), a); +} + +RGBA32 makeUnPremultipliedRGBA(RGBA32 pixelColor) +{ + if (pixelColor.isVisible() && !pixelColor.isOpaque()) + return makeUnPremultipliedRGBA(pixelColor.redComponent(), pixelColor.greenComponent(), pixelColor.blueComponent(), pixelColor.alphaComponent()); + return pixelColor; +} + +RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a) +{ + return makeRGBA(colorFloatToRGBAByte(r), colorFloatToRGBAByte(g), colorFloatToRGBAByte(b), colorFloatToRGBAByte(a)); +} + +RGBA32 makeRGBAFromHSLA(float hue, float saturation, float lightness, float alpha) +{ + const float scaleFactor = 255.0; + FloatComponents floatResult = hslToSRGB({ hue, saturation, lightness, alpha }); + return makeRGBA( + round(floatResult.components[0] * scaleFactor), + round(floatResult.components[1] * scaleFactor), + round(floatResult.components[2] * scaleFactor), + round(floatResult.components[3] * scaleFactor)); +} + +RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a) +{ + double colors = 1 - k; + int r = static_cast(nextafter(256, 0) * (colors * (1 - c))); + int g = static_cast(nextafter(256, 0) * (colors * (1 - m))); + int b = static_cast(nextafter(256, 0) * (colors * (1 - y))); + return makeRGBA(r, g, b, static_cast(nextafter(256, 0) * a)); +} + +String SimpleColor::serializationForHTML() const +{ + if (isOpaque()) + return makeString('#', hex(redComponent(), 2, Lowercase), hex(greenComponent(), 2, Lowercase), hex(blueComponent(), 2, Lowercase)); + return serializationForCSS(); +} + +static char decimalDigit(unsigned number) +{ + ASSERT(number < 10); + return '0' + number; +} + +static std::array fractionDigitsForFractionalAlphaValue(uint8_t alpha) +{ + ASSERT(alpha > 0); + ASSERT(alpha < 0xFF); + if (((alpha * 100 + 0x7F) / 0xFF * 0xFF + 50) / 100 != alpha) + return { { decimalDigit(alpha * 10 / 0xFF % 10), decimalDigit(alpha * 100 / 0xFF % 10), decimalDigit((alpha * 1000 + 0x7F) / 0xFF % 10), '\0' } }; + if (int thirdDigit = (alpha * 100 + 0x7F) / 0xFF % 10) + return { { decimalDigit(alpha * 10 / 0xFF), decimalDigit(thirdDigit), '\0', '\0' } }; + return { { decimalDigit((alpha * 10 + 0x7F) / 0xFF), '\0', '\0', '\0' } }; +} + +String SimpleColor::serializationForCSS() const +{ + switch (alphaComponent()) { + case 0: + return makeString("rgba(", redComponent(), ", ", greenComponent(), ", ", blueComponent(), ", 0)"); + case 0xFF: + return makeString("rgb(", redComponent(), ", ", greenComponent(), ", ", blueComponent(), ')'); + default: + return makeString("rgba(", redComponent(), ", ", greenComponent(), ", ", blueComponent(), ", 0.", fractionDigitsForFractionalAlphaValue(alphaComponent()).data(), ')'); + } +} + +String SimpleColor::serializationForRenderTreeAsText() const +{ + if (alphaComponent() < 0xFF) + return makeString('#', hex(redComponent(), 2), hex(greenComponent(), 2), hex(blueComponent(), 2), hex(alphaComponent(), 2)); + return makeString('#', hex(redComponent(), 2), hex(greenComponent(), 2), hex(blueComponent(), 2)); +} + +} // namespace WebCore diff --git a/Source/WebCore/platform/graphics/SimpleColor.h b/Source/WebCore/platform/graphics/SimpleColor.h new file mode 100644 index 000000000000..6b06e63561ee --- /dev/null +++ b/Source/WebCore/platform/graphics/SimpleColor.h @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2003-2020 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma once + +#include "ColorUtilities.h" +#include +#include +#include + +namespace WebCore { + +// Color value with 8-bit components for red, green, blue, and alpha. +// For historical reasons, stored as a 32-bit integer, with alpha in the high bits: ARGB. +class SimpleColor { +public: + constexpr SimpleColor(uint32_t value = 0) : m_value { value } { } + + constexpr uint32_t value() const { return m_value; } + + constexpr uint8_t redComponent() const { return m_value >> 16; } + constexpr uint8_t greenComponent() const { return m_value >> 8; } + constexpr uint8_t blueComponent() const { return m_value; } + constexpr uint8_t alphaComponent() const { return m_value >> 24; } + + constexpr float alphaComponentAsFloat() const { return static_cast(alphaComponent()) / 0xFF; } + + constexpr bool isOpaque() const { return alphaComponent() == 0xFF; } + constexpr bool isVisible() const { return alphaComponent(); } + + String serializationForHTML() const; + String serializationForCSS() const; + String serializationForRenderTreeAsText() const; + + constexpr SimpleColor colorWithAlpha(uint8_t alpha) const { return { (m_value & 0x00FFFFFF) | alpha << 24 }; } + + template + constexpr uint8_t get() const + { + static_assert(N < 4); + if constexpr (!N) + return redComponent(); + else if constexpr (N == 1) + return greenComponent(); + else if constexpr (N == 2) + return blueComponent(); + else if constexpr (N == 3) + return alphaComponent(); + } + +private: + uint32_t m_value { 0 }; +}; + +bool operator==(SimpleColor, SimpleColor); +bool operator!=(SimpleColor, SimpleColor); + +// FIXME: Remove this after migrating to the new name. +using RGBA32 = SimpleColor; + +constexpr RGBA32 makeRGB(int r, int g, int b); +constexpr RGBA32 makeRGBA(int r, int g, int b, int a); + +RGBA32 makePremultipliedRGBA(int r, int g, int b, int a, bool ceiling = true); +RGBA32 makePremultipliedRGBA(RGBA32); +RGBA32 makeUnPremultipliedRGBA(int r, int g, int b, int a); +RGBA32 makeUnPremultipliedRGBA(RGBA32); + +WEBCORE_EXPORT RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a); +WEBCORE_EXPORT RGBA32 makeRGBAFromHSLA(float h, float s, float l, float a); +RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a); + +uint8_t roundAndClampColorChannel(int); +uint8_t roundAndClampColorChannel(float); + +uint16_t fastMultiplyBy255(uint16_t value); +uint16_t fastDivideBy255(uint16_t); + +uint8_t colorFloatToRGBAByte(float); + +inline bool operator==(SimpleColor a, SimpleColor b) +{ + return a.value() == b.value(); +} + +inline bool operator!=(SimpleColor a, SimpleColor b) +{ + return !(a == b); +} + +inline uint8_t roundAndClampColorChannel(int value) +{ + return std::max(0, std::min(255, value)); +} + +inline uint8_t roundAndClampColorChannel(float value) +{ + return std::max(0.f, std::min(255.f, std::round(value))); +} + +inline uint16_t fastMultiplyBy255(uint16_t value) +{ + return (value << 8) - value; +} + +inline uint16_t fastDivideBy255(uint16_t value) +{ + // While this is an approximate algorithm for division by 255, it gives perfectly accurate results for 16-bit values. + // FIXME: Since this gives accurate results for 16-bit values, we should get this optimization into compilers like clang. + uint16_t approximation = value >> 8; + uint16_t remainder = value - (approximation * 255) + 1; + return approximation + (remainder >> 8); +} + +inline uint8_t colorFloatToRGBAByte(float f) +{ + // FIXME: Consolidate with clampedColorComponent(). + // We use lroundf and 255 instead of nextafterf(256, 0) to match CG's rounding + return std::max(0, std::min(static_cast(lroundf(255.0f * f)), 255)); +} + +constexpr RGBA32 makeRGB(int r, int g, int b) +{ + return makeRGBA(r, g, b, 0xFF); +} + +constexpr RGBA32 makeRGBA(int r, int g, int b, int a) +{ + return { static_cast(std::max(0, std::min(a, 0xFF)) << 24 | std::max(0, std::min(r, 0xFF)) << 16 | std::max(0, std::min(g, 0xFF)) << 8 | std::max(0, std::min(b, 0xFF))) }; +} + +} // namespace WebCore + +namespace std { + +template<> +class tuple_size : public std::integral_constant { +}; + +template +class tuple_element { +public: + using type = uint8_t; +}; + +}