Changeset 262110 in webkit
- Timestamp:
- May 24, 2020 12:13:02 PM (4 years ago)
- Location:
- trunk/Source/WebCore
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebCore/ChangeLog
r262109 r262110 1 2020-05-24 Sam Weinig <weinig@apple.com> 2 3 Extended Color Cleanup: Move SimpleColor into its own files 4 https://bugs.webkit.org/show_bug.cgi?id=212309 5 6 Reviewed by Simon Fraser. 7 8 Move SimpleColor into its own files. It's about time. 9 10 * Headers.cmake: 11 * Sources.txt: 12 * WebCore.xcodeproj/project.pbxproj: 13 * platform/graphics/Color.cpp: 14 (WebCore::premultipliedChannel): Deleted. 15 (WebCore::unpremultipliedChannel): Deleted. 16 (WebCore::makePremultipliedRGBA): Deleted. 17 (WebCore::makeUnPremultipliedRGBA): Deleted. 18 (WebCore::colorFloatToRGBAByte): Deleted. 19 (WebCore::makeRGBA32FromFloats): Deleted. 20 (WebCore::makeRGBAFromHSLA): Deleted. 21 (WebCore::makeRGBAFromCMYKA): Deleted. 22 (WebCore::SimpleColor::serializationForHTML const): Deleted. 23 (WebCore::decimalDigit): Deleted. 24 (WebCore::fractionDigitsForFractionalAlphaValue): Deleted. 25 (WebCore::SimpleColor::serializationForCSS const): Deleted. 26 (WebCore::RGBA32::serializationForRenderTreeAsText const): Deleted. 27 * platform/graphics/Color.h: 28 (WebCore::SimpleColor::SimpleColor): Deleted. 29 (WebCore::SimpleColor::value const): Deleted. 30 (WebCore::SimpleColor::redComponent const): Deleted. 31 (WebCore::SimpleColor::greenComponent const): Deleted. 32 (WebCore::SimpleColor::blueComponent const): Deleted. 33 (WebCore::SimpleColor::alphaComponent const): Deleted. 34 (WebCore::SimpleColor::alphaComponentAsFloat const): Deleted. 35 (WebCore::SimpleColor::isOpaque const): Deleted. 36 (WebCore::SimpleColor::isVisible const): Deleted. 37 (WebCore::SimpleColor::colorWithAlpha const): Deleted. 38 (WebCore::SimpleColor::get const): Deleted. 39 (WebCore::roundAndClampColorChannel): Deleted. 40 (WebCore::fastMultiplyBy255): Deleted. 41 (WebCore::fastDivideBy255): Deleted. 42 (WebCore::makeRGB): Deleted. 43 (WebCore::makeRGBA): Deleted. 44 * platform/graphics/SimpleColor.cpp: Copied from platform/graphics/Color.cpp. 45 (WebCore::SimpleColor::serializationForRenderTreeAsText const): 46 (): Deleted. 47 (WebCore::colorFloatToRGBAByte): Deleted. 48 (WebCore::parseHexColorInternal): Deleted. 49 (WebCore::Color::parseHexColor): Deleted. 50 (WebCore::differenceSquared): Deleted. 51 (WebCore::findNamedColor): Deleted. 52 (WebCore::Color::Color): Deleted. 53 (WebCore::Color::operator=): Deleted. 54 (WebCore::Color::serialized const): Deleted. 55 (WebCore::Color::cssText const): Deleted. 56 (WebCore::RGBA32::serializationForRenderTreeAsText const): Deleted. 57 (WebCore::Color::nameForRenderTreeAsText const): Deleted. 58 (WebCore::Color::light const): Deleted. 59 (WebCore::Color::dark const): Deleted. 60 (WebCore::Color::isDark const): Deleted. 61 (WebCore::Color::lightness const): Deleted. 62 (WebCore::blendComponent): Deleted. 63 (WebCore::Color::blend const): Deleted. 64 (WebCore::Color::blendWithWhite const): Deleted. 65 (WebCore::Color::colorWithAlphaMultipliedBy const): Deleted. 66 (WebCore::Color::colorWithAlphaMultipliedByUsingAlternativeRounding const): Deleted. 67 (WebCore::Color::colorWithAlpha const): Deleted. 68 (WebCore::Color::colorWithAlphaUsingAlternativeRounding const): Deleted. 69 (WebCore::Color::colorSpaceAndComponents const): Deleted. 70 (WebCore::Color::toSRGBASimpleColorLossy const): Deleted. 71 (WebCore::Color::toSRGBAComponentsLossy const): Deleted. 72 (WebCore::extendedColorsEqual): Deleted. 73 (WebCore::blend): Deleted. 74 (WebCore::blendWithoutPremultiply): Deleted. 75 (WebCore::Color::tagAsValid): Deleted. 76 (WebCore::Color::asExtended const): Deleted. 77 (WebCore::operator<<): Deleted. 78 * platform/graphics/SimpleColor.h: Copied from platform/graphics/Color.h. 79 (WebCore::SimpleColor::alphaComponentAsFloat const): 80 (WebCore::SimpleColor::colorWithAlpha const): 81 (WebCore::SimpleColor::get const): 82 (WebCore::colorFloatToRGBAByte): 83 (WebCore::Color::Color): Deleted. 84 (WebCore::Color::isHashTableDeletedValue const): Deleted. 85 (WebCore::Color::~Color): Deleted. 86 (WebCore::Color::isValid const): Deleted. 87 (WebCore::Color::isOpaque const): Deleted. 88 (WebCore::Color::isVisible const): Deleted. 89 (WebCore::Color::red const): Deleted. 90 (WebCore::Color::green const): Deleted. 91 (WebCore::Color::blue const): Deleted. 92 (WebCore::Color::alpha const): Deleted. 93 (WebCore::Color::alphaAsFloat const): Deleted. 94 (WebCore::Color::opaqueColor const): Deleted. 95 (WebCore::Color::isSemantic const): Deleted. 96 (WebCore::Color::isExtended const): Deleted. 97 (WebCore::Color::setRGB): Deleted. 98 (WebCore::Color::setIsSemantic): Deleted. 99 (WebCore::equalIgnoringSemanticColor): Deleted. 100 (WebCore::Color::hash const): Deleted. 101 (WebCore::Color::colorWithAlphaMultipliedByUsingAlternativeRounding const): Deleted. 102 (WebCore::Color::colorWithAlphaUsingAlternativeRounding const): Deleted. 103 (WebCore::Color::rgb const): Deleted. 104 (WebCore::Color::isBlackColor): Deleted. 105 (WebCore::Color::isWhiteColor): Deleted. 106 (WebCore::Color::encode const): Deleted. 107 (WebCore::Color::decode): Deleted. 108 1 109 2020-05-24 Zalan Bujtas <zalan@apple.com> 2 110 -
trunk/Source/WebCore/Headers.cmake
r262095 r262110 1185 1185 platform/graphics/RenderingMode.h 1186 1186 platform/graphics/RoundedRect.h 1187 platform/graphics/SimpleColor.h 1187 1188 platform/graphics/StringTruncator.h 1188 1189 platform/graphics/TabSize.h -
trunk/Source/WebCore/Sources.txt
r261981 r262110 1938 1938 platform/graphics/RoundedRect.cpp 1939 1939 platform/graphics/ShadowBlur.cpp 1940 platform/graphics/SimpleColor.cpp 1940 1941 platform/graphics/StringTruncator.cpp 1941 1942 platform/graphics/SurrogatePairAwareTextIterator.cpp -
trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj
r262095 r262110 2242 2242 7C30D9841F815AEC00268356 /* JSAbortController.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C30D9801F815AC100268356 /* JSAbortController.h */; }; 2243 2243 7C30D9861F815AEC00268356 /* JSAbortSignal.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C30D9811F815AC100268356 /* JSAbortSignal.h */; }; 2244 7C31C88E247AD595005BF319 /* SimpleColor.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C31C88C247AD595005BF319 /* SimpleColor.h */; settings = {ATTRIBUTES = (Private, ); }; }; 2244 2245 7C330A021DF8FAC600D3395C /* GraphicsContextGLAttributes.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C330A011DF8FAC600D3395C /* GraphicsContextGLAttributes.h */; settings = {ATTRIBUTES = (Private, ); }; }; 2245 2246 7C330A081DF9F95100D3395C /* JSPositionOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C330A061DF9F95100D3395C /* JSPositionOptions.h */; }; … … 9975 9976 7C30D9811F815AC100268356 /* JSAbortSignal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSAbortSignal.h; sourceTree = "<group>"; }; 9976 9977 7C30D9821F815AC200268356 /* JSAbortSignal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSAbortSignal.cpp; sourceTree = "<group>"; }; 9978 7C31C88A247AD594005BF319 /* SimpleColor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SimpleColor.cpp; sourceTree = "<group>"; }; 9979 7C31C88C247AD595005BF319 /* SimpleColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleColor.h; sourceTree = "<group>"; }; 9977 9980 7C330A011DF8FAC600D3395C /* GraphicsContextGLAttributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GraphicsContextGLAttributes.h; sourceTree = "<group>"; }; 9978 9981 7C330A031DF9E95B00D3395C /* PositionOptions.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = PositionOptions.idl; sourceTree = "<group>"; }; … … 25595 25598 0F3DD44D12F5EA1B000D9190 /* ShadowBlur.cpp */, 25596 25599 0F3DD44E12F5EA1B000D9190 /* ShadowBlur.h */, 25600 7C31C88A247AD594005BF319 /* SimpleColor.cpp */, 25601 7C31C88C247AD595005BF319 /* SimpleColor.h */, 25597 25602 CD641EB21818F5ED00EE4C41 /* SourceBufferPrivate.h */, 25598 25603 CDC8B5AC1804AE5D0016E685 /* SourceBufferPrivateClient.h */, … … 33538 33543 CD336F6817FA0AC600DDDCD0 /* VideoTrackPrivateAVFObjC.h in Headers */, 33539 33544 CD8B5A43180D149A008B8E65 /* VideoTrackPrivateMediaSourceAVFObjC.h in Headers */, 33545 7C31C88E247AD595005BF319 /* SimpleColor.h in Headers */, 33540 33546 070E81D11BF27656001FDA48 /* VideoTrackPrivateMediaStream.h in Headers */, 33541 33547 CEF418CF1179678C009D112C /* ViewportArguments.h in Headers */, -
trunk/Source/WebCore/platform/graphics/Color.cpp
r262108 r262110 41 41 static constexpr SimpleColor darkenedWhite { 0xFFABABAB }; 42 42 43 static inline unsigned premultipliedChannel(unsigned c, unsigned a, bool ceiling = true)44 {45 return fastDivideBy255(ceiling ? c * a + 254 : c * a);46 }47 48 static inline unsigned unpremultipliedChannel(unsigned c, unsigned a)49 {50 return (fastMultiplyBy255(c) + a - 1) / a;51 }52 53 RGBA32 makePremultipliedRGBA(int r, int g, int b, int a, bool ceiling)54 {55 return makeRGBA(premultipliedChannel(r, a, ceiling), premultipliedChannel(g, a, ceiling), premultipliedChannel(b, a, ceiling), a);56 }57 58 RGBA32 makePremultipliedRGBA(RGBA32 pixelColor)59 {60 if (pixelColor.isOpaque())61 return pixelColor;62 return makePremultipliedRGBA(pixelColor.redComponent(), pixelColor.greenComponent(), pixelColor.blueComponent(), pixelColor.alphaComponent());63 }64 65 RGBA32 makeUnPremultipliedRGBA(int r, int g, int b, int a)66 {67 return makeRGBA(unpremultipliedChannel(r, a), unpremultipliedChannel(g, a), unpremultipliedChannel(b, a), a);68 }69 70 RGBA32 makeUnPremultipliedRGBA(RGBA32 pixelColor)71 {72 if (pixelColor.isVisible() && !pixelColor.isOpaque())73 return makeUnPremultipliedRGBA(pixelColor.redComponent(), pixelColor.greenComponent(), pixelColor.blueComponent(), pixelColor.alphaComponent());74 return pixelColor;75 }76 77 static int colorFloatToRGBAByte(float f)78 {79 // We use lroundf and 255 instead of nextafterf(256, 0) to match CG's rounding80 return std::max(0, std::min(static_cast<int>(lroundf(255.0f * f)), 255));81 }82 83 RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a)84 {85 return makeRGBA(colorFloatToRGBAByte(r), colorFloatToRGBAByte(g), colorFloatToRGBAByte(b), colorFloatToRGBAByte(a));86 }87 88 RGBA32 makeRGBAFromHSLA(float hue, float saturation, float lightness, float alpha)89 {90 const float scaleFactor = 255.0;91 FloatComponents floatResult = hslToSRGB({ hue, saturation, lightness, alpha });92 return makeRGBA(93 round(floatResult.components[0] * scaleFactor),94 round(floatResult.components[1] * scaleFactor),95 round(floatResult.components[2] * scaleFactor),96 round(floatResult.components[3] * scaleFactor));97 }98 99 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a)100 {101 double colors = 1 - k;102 int r = static_cast<int>(nextafter(256, 0) * (colors * (1 - c)));103 int g = static_cast<int>(nextafter(256, 0) * (colors * (1 - m)));104 int b = static_cast<int>(nextafter(256, 0) * (colors * (1 - y)));105 return makeRGBA(r, g, b, static_cast<float>(nextafter(256, 0) * a));106 }107 108 43 // originally moved here from the CSS parser 109 44 template <typename CharacterType> … … 293 228 } 294 229 295 String SimpleColor::serializationForHTML() const296 {297 if (isOpaque())298 return makeString('#', hex(redComponent(), 2, Lowercase), hex(greenComponent(), 2, Lowercase), hex(blueComponent(), 2, Lowercase));299 return serializationForCSS();300 }301 302 230 String Color::serialized() const 303 231 { … … 307 235 } 308 236 309 static char decimalDigit(unsigned number)310 {311 ASSERT(number < 10);312 return '0' + number;313 }314 315 static std::array<char, 4> fractionDigitsForFractionalAlphaValue(uint8_t alpha)316 {317 ASSERT(alpha > 0);318 ASSERT(alpha < 0xFF);319 if (((alpha * 100 + 0x7F) / 0xFF * 0xFF + 50) / 100 != alpha)320 return { { decimalDigit(alpha * 10 / 0xFF % 10), decimalDigit(alpha * 100 / 0xFF % 10), decimalDigit((alpha * 1000 + 0x7F) / 0xFF % 10), '\0' } };321 if (int thirdDigit = (alpha * 100 + 0x7F) / 0xFF % 10)322 return { { decimalDigit(alpha * 10 / 0xFF), decimalDigit(thirdDigit), '\0', '\0' } };323 return { { decimalDigit((alpha * 10 + 0x7F) / 0xFF), '\0', '\0', '\0' } };324 }325 326 String SimpleColor::serializationForCSS() const327 {328 switch (alphaComponent()) {329 case 0:330 return makeString("rgba(", redComponent(), ", ", greenComponent(), ", ", blueComponent(), ", 0)");331 case 0xFF:332 return makeString("rgb(", redComponent(), ", ", greenComponent(), ", ", blueComponent(), ')');333 default:334 return makeString("rgba(", redComponent(), ", ", greenComponent(), ", ", blueComponent(), ", 0.", fractionDigitsForFractionalAlphaValue(alphaComponent()).data(), ')');335 }336 }337 338 237 String Color::cssText() const 339 238 { … … 341 240 return asExtended().cssText(); 342 241 return rgb().serializationForCSS(); 343 }344 345 String RGBA32::serializationForRenderTreeAsText() const346 {347 if (alphaComponent() < 0xFF)348 return makeString('#', hex(redComponent(), 2), hex(greenComponent(), 2), hex(blueComponent(), 2), hex(alphaComponent(), 2));349 return makeString('#', hex(redComponent(), 2), hex(greenComponent(), 2), hex(blueComponent(), 2));350 242 } 351 243 -
trunk/Source/WebCore/platform/graphics/Color.h
r262108 r262110 28 28 #include "ColorSpace.h" 29 29 #include "ExtendedColor.h" 30 #include "SimpleColor.h" 30 31 #include <algorithm> 31 32 #include <cmath> … … 56 57 57 58 struct FloatComponents; 58 59 // Color value with 8-bit components for red, green, blue, and alpha.60 // For historical reasons, stored as a 32-bit integer, with alpha in the high bits: ARGB.61 class SimpleColor {62 public:63 constexpr SimpleColor(uint32_t value = 0) : m_value { value } { }64 65 constexpr uint32_t value() const { return m_value; }66 67 constexpr uint8_t redComponent() const { return m_value >> 16; }68 constexpr uint8_t greenComponent() const { return m_value >> 8; }69 constexpr uint8_t blueComponent() const { return m_value; }70 constexpr uint8_t alphaComponent() const { return m_value >> 24; }71 72 constexpr float alphaComponentAsFloat() const { return static_cast<float>(alphaComponent()) / 0xFF; }73 74 constexpr bool isOpaque() const { return alphaComponent() == 0xFF; }75 constexpr bool isVisible() const { return alphaComponent(); }76 77 String serializationForHTML() const;78 String serializationForCSS() const;79 String serializationForRenderTreeAsText() const;80 81 constexpr SimpleColor colorWithAlpha(uint8_t alpha) const { return { (m_value & 0x00FFFFFF) | alpha << 24 }; }82 83 template<std::size_t N>84 constexpr uint8_t get() const85 {86 static_assert(N < 4);87 if constexpr (!N)88 return redComponent();89 else if constexpr (N == 1)90 return greenComponent();91 else if constexpr (N == 2)92 return blueComponent();93 else if constexpr (N == 3)94 return alphaComponent();95 }96 97 private:98 uint32_t m_value { 0 };99 };100 101 bool operator==(SimpleColor, SimpleColor);102 bool operator!=(SimpleColor, SimpleColor);103 104 // FIXME: Remove this after migrating to the new name.105 using RGBA32 = SimpleColor;106 107 constexpr RGBA32 makeRGB(int r, int g, int b);108 constexpr RGBA32 makeRGBA(int r, int g, int b, int a);109 110 RGBA32 makePremultipliedRGBA(int r, int g, int b, int a, bool ceiling = true);111 RGBA32 makePremultipliedRGBA(RGBA32);112 RGBA32 makeUnPremultipliedRGBA(int r, int g, int b, int a);113 RGBA32 makeUnPremultipliedRGBA(RGBA32);114 115 WEBCORE_EXPORT RGBA32 makeRGBA32FromFloats(float r, float g, float b, float a);116 WEBCORE_EXPORT RGBA32 makeRGBAFromHSLA(float h, float s, float l, float a);117 RGBA32 makeRGBAFromCMYKA(float c, float m, float y, float k, float a);118 119 uint8_t roundAndClampColorChannel(int);120 uint8_t roundAndClampColorChannel(float);121 59 122 60 class Color { … … 351 289 int differenceSquared(const Color&, const Color&); 352 290 353 uint16_t fastMultiplyBy255(uint16_t value);354 uint16_t fastDivideBy255(uint16_t);355 356 291 #if USE(CG) 357 292 WEBCORE_EXPORT CGColorRef cachedCGColor(const Color&); … … 360 295 WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const Color&); 361 296 WEBCORE_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, ColorSpace); 362 363 inline bool operator==(SimpleColor a, SimpleColor b)364 {365 return a.value() == b.value();366 }367 368 inline bool operator!=(SimpleColor a, SimpleColor b)369 {370 return !(a == b);371 }372 297 373 298 inline bool operator==(const Color& a, const Color& b) … … 397 322 398 323 return WTF::intHash(m_colorData.rgbaAndFlags); 399 }400 401 inline uint8_t roundAndClampColorChannel(int value)402 {403 return std::max(0, std::min(255, value));404 }405 406 inline uint8_t roundAndClampColorChannel(float value)407 {408 return std::max(0.f, std::min(255.f, std::round(value)));409 }410 411 inline uint16_t fastMultiplyBy255(uint16_t value)412 {413 return (value << 8) - value;414 }415 416 inline uint16_t fastDivideBy255(uint16_t value)417 {418 // While this is an approximate algorithm for division by 255, it gives perfectly accurate results for 16-bit values.419 // FIXME: Since this gives accurate results for 16-bit values, we should get this optimization into compilers like clang.420 uint16_t approximation = value >> 8;421 uint16_t remainder = value - (approximation * 255) + 1;422 return approximation + (remainder >> 8);423 324 } 424 325 … … 535 436 } 536 437 537 constexpr RGBA32 makeRGB(int r, int g, int b)538 {539 return makeRGBA(r, g, b, 0xFF);540 }541 542 constexpr RGBA32 makeRGBA(int r, int g, int b, int a)543 {544 return { static_cast<unsigned>(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))) };545 }546 547 438 } // namespace WebCore 548 439 … … 551 442 template<> struct HashTraits<WebCore::Color>; 552 443 } 553 554 namespace std {555 556 template<>557 class tuple_size<WebCore::SimpleColor> : public std::integral_constant<std::size_t, 4> {558 };559 560 template<std::size_t N>561 class tuple_element<N, WebCore::SimpleColor> {562 public:563 using type = uint8_t;564 };565 566 }
Note: See TracChangeset
for help on using the changeset viewer.