Title: [261794] trunk
Revision
261794
Author
[email protected]
Date
2020-05-17 12:28:12 -0700 (Sun, 17 May 2020)

Log Message

Fix operator== and hash() for ExtendedColor
https://bugs.webkit.org/show_bug.cgi?id=211993

Reviewed by Sam Weinig.

Source/WebCore:

Color::operator== and hash() were wrong for extended color. Fix operator==
to compare extended colors. Extended and non-extended colors have to always
conpare as non-equal to preserve computed style behavior, currently.

Fix hash() to hash the color components and colorspace for ExtendedColor.

Add some API tests for these code paths.

* platform/graphics/Color.cpp:
(WebCore::extendedColorsEqual):
* platform/graphics/Color.h:
(WebCore::operator==):
(WebCore::Color::hash const):
* platform/graphics/ColorUtilities.cpp:
(WebCore::areEssentiallyEqual):
* platform/graphics/ColorUtilities.h:
(WebCore::operator==):
(WebCore::operator!=):
* platform/graphics/ExtendedColor.cpp:
(WebCore::ExtendedColor::hash const):
* platform/graphics/ExtendedColor.h:
(WebCore::operator==):
(WebCore::operator!=):

Tools:

* TestWebKitAPI/Tests/WebCore/ExtendedColorTests.cpp:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (261793 => 261794)


--- trunk/Source/WebCore/ChangeLog	2020-05-17 16:17:45 UTC (rev 261793)
+++ trunk/Source/WebCore/ChangeLog	2020-05-17 19:28:12 UTC (rev 261794)
@@ -1,3 +1,34 @@
+2020-05-17  Simon Fraser  <[email protected]>
+
+        Fix operator== and hash() for ExtendedColor
+        https://bugs.webkit.org/show_bug.cgi?id=211993
+
+        Reviewed by Sam Weinig.
+
+        Color::operator== and hash() were wrong for extended color. Fix operator==
+        to compare extended colors. Extended and non-extended colors have to always
+        conpare as non-equal to preserve computed style behavior, currently.
+
+        Fix hash() to hash the color components and colorspace for ExtendedColor.
+
+        Add some API tests for these code paths.
+
+        * platform/graphics/Color.cpp:
+        (WebCore::extendedColorsEqual):
+        * platform/graphics/Color.h:
+        (WebCore::operator==):
+        (WebCore::Color::hash const):
+        * platform/graphics/ColorUtilities.cpp:
+        (WebCore::areEssentiallyEqual):
+        * platform/graphics/ColorUtilities.h:
+        (WebCore::operator==):
+        (WebCore::operator!=):
+        * platform/graphics/ExtendedColor.cpp:
+        (WebCore::ExtendedColor::hash const):
+        * platform/graphics/ExtendedColor.h:
+        (WebCore::operator==):
+        (WebCore::operator!=):
+
 2020-05-17  Zalan Bujtas  <[email protected]>
 
         [LFC][BFC] Introduce TableWrapperBlockFormattingContext

Modified: trunk/Source/WebCore/platform/graphics/Color.cpp (261793 => 261794)


--- trunk/Source/WebCore/platform/graphics/Color.cpp	2020-05-17 16:17:45 UTC (rev 261793)
+++ trunk/Source/WebCore/platform/graphics/Color.cpp	2020-05-17 19:28:12 UTC (rev 261794)
@@ -593,6 +593,15 @@
     return makePremultipliedRGBA(color.red(), color.green(), color.blue(), color.alpha());
 }
 
+bool extendedColorsEqual(const Color& a, const Color& b)
+{
+    if (a.isExtended() && b.isExtended())
+        return a.asExtended() == b.asExtended();
+
+    ASSERT(a.isExtended() || b.isExtended());
+    return false;
+}
+
 Color blend(const Color& from, const Color& to, double progress, bool blendPremultiplied)
 {
     // FIXME: ExtendedColor - needs to handle color spaces.

Modified: trunk/Source/WebCore/platform/graphics/Color.h (261793 => 261794)


--- trunk/Source/WebCore/platform/graphics/Color.h	2020-05-17 16:17:45 UTC (rev 261793)
+++ trunk/Source/WebCore/platform/graphics/Color.h	2020-05-17 19:28:12 UTC (rev 261794)
@@ -205,9 +205,7 @@
 
     RGBA32 rgb() const;
 
-    // FIXME: Like operator==, this will give different values for ExtendedColors that
-    // should be identical, since the respective pointer will be different.
-    unsigned hash() const { return WTF::intHash(m_colorData.rgbaAndFlags); }
+    unsigned hash() const;
 
     // FIXME: ExtendedColor - these should be renamed (to be clear about their parameter types, or
     // replaced with alternative accessors.
@@ -282,6 +280,7 @@
     WEBCORE_EXPORT Color& operator=(const Color&);
     WEBCORE_EXPORT Color& operator=(Color&&);
 
+    // Extended and non-extended colors will always be non-equal.
     friend bool operator==(const Color& a, const Color& b);
     friend bool equalIgnoringSemanticColor(const Color& a, const Color& b);
 
@@ -313,13 +312,13 @@
     } m_colorData;
 };
 
-// FIXME: These do not work for ExtendedColor because
-// they become just pointer comparison.
 bool operator==(const Color&, const Color&);
 bool operator!=(const Color&, const Color&);
 
 Color colorFromPremultipliedARGB(RGBA32);
 RGBA32 premultipliedARGBFromColor(const Color&);
+// One or both must be extended colors.
+WEBCORE_EXPORT bool extendedColorsEqual(const Color&, const Color&);
 
 Color blend(const Color& from, const Color& to, double progress, bool blendPremultiplied = true);
 
@@ -347,6 +346,9 @@
 
 inline bool operator==(const Color& a, const Color& b)
 {
+    if (a.isExtended() || b.isExtended())
+        return extendedColorsEqual(a, b);
+
     return a.m_colorData.rgbaAndFlags == b.m_colorData.rgbaAndFlags;
 }
 
@@ -357,9 +359,19 @@
 
 inline bool equalIgnoringSemanticColor(const Color& a, const Color& b)
 {
+    if (a.isExtended() || b.isExtended())
+        return extendedColorsEqual(a, b);
     return (a.m_colorData.rgbaAndFlags & ~Color::isSemanticRBGAColorBit) == (b.m_colorData.rgbaAndFlags & ~Color::isSemanticRBGAColorBit);
 }
 
+inline unsigned Color::hash() const
+{
+    if (isExtended())
+        return asExtended().hash();
+
+    return WTF::intHash(m_colorData.rgbaAndFlags);
+}
+
 inline uint8_t roundAndClampColorChannel(int value)
 {
     return std::max(0, std::min(255, value));

Modified: trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp (261793 => 261794)


--- trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp	2020-05-17 16:17:45 UTC (rev 261793)
+++ trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp	2020-05-17 19:28:12 UTC (rev 261794)
@@ -44,6 +44,14 @@
     components[3] = clampedColorComponent(floatComponents.components[3]);
 }
 
+bool areEssentiallyEqual(const FloatComponents& a, const FloatComponents& b)
+{
+    return WTF::areEssentiallyEqual(a.components[0], b.components[0])
+        && WTF::areEssentiallyEqual(a.components[1], b.components[1])
+        && WTF::areEssentiallyEqual(a.components[2], b.components[2])
+        && WTF::areEssentiallyEqual(a.components[3], b.components[3]);
+}
+
 // These are the standard sRGB <-> linearRGB conversion functions (https://en.wikipedia.org/wiki/SRGB).
 float linearToSRGBColorComponent(float c)
 {

Modified: trunk/Source/WebCore/platform/graphics/ColorUtilities.h (261793 => 261794)


--- trunk/Source/WebCore/platform/graphics/ColorUtilities.h	2020-05-17 16:17:45 UTC (rev 261793)
+++ trunk/Source/WebCore/platform/graphics/ColorUtilities.h	2020-05-17 19:28:12 UTC (rev 261794)
@@ -104,6 +104,8 @@
     std::array<float, 4> components;
 };
 
+bool areEssentiallyEqual(const FloatComponents&, const FloatComponents&);
+
 struct ColorComponents {
     ColorComponents(const FloatComponents&);
     
@@ -148,6 +150,16 @@
     };
 }
 
+inline bool operator==(const ColorComponents& a, const ColorComponents& b)
+{
+    return a.components == b.components;
+}
+
+inline bool operator!=(const ColorComponents& a, const ColorComponents& b)
+{
+    return !(a == b);
+}
+
 inline uint8_t clampedColorComponent(float f)
 {
     // See also colorFloatToRGBAByte().

Modified: trunk/Source/WebCore/platform/graphics/ExtendedColor.cpp (261793 => 261794)


--- trunk/Source/WebCore/platform/graphics/ExtendedColor.cpp	2020-05-17 16:17:45 UTC (rev 261793)
+++ trunk/Source/WebCore/platform/graphics/ExtendedColor.cpp	2020-05-17 19:28:12 UTC (rev 261794)
@@ -26,6 +26,7 @@
 #include "config.h"
 #include "ExtendedColor.h"
 
+#include <wtf/Hasher.h>
 #include <wtf/MathExtras.h>
 #include <wtf/text/StringConcatenateNumbers.h>
 
@@ -36,6 +37,11 @@
     return adoptRef(*new ExtendedColor(c1, c2, c3, alpha, colorSpace));
 }
 
+unsigned ExtendedColor::hash() const
+{
+    return computeHash(m_channels.components[0], m_channels.components[1], m_channels.components[2], m_channels.components[3], m_colorSpace);
+}
+
 String ExtendedColor::cssText() const
 {
     const char* colorSpace;

Modified: trunk/Source/WebCore/platform/graphics/ExtendedColor.h (261793 => 261794)


--- trunk/Source/WebCore/platform/graphics/ExtendedColor.h	2020-05-17 16:17:45 UTC (rev 261793)
+++ trunk/Source/WebCore/platform/graphics/ExtendedColor.h	2020-05-17 19:28:12 UTC (rev 261794)
@@ -59,6 +59,8 @@
 
     ColorSpace colorSpace() const { return m_colorSpace; }
 
+    WEBCORE_EXPORT unsigned hash() const;
+
     WEBCORE_EXPORT String cssText() const;
 
 private:
@@ -71,4 +73,14 @@
     ColorSpace m_colorSpace { ColorSpace::SRGB };
 };
 
+inline bool operator==(const ExtendedColor& a, const ExtendedColor& b)
+{
+    return a.colorSpace() == b.colorSpace() && areEssentiallyEqual(a.channels(), b.channels());
 }
+
+inline bool operator!=(const ExtendedColor& a, const ExtendedColor& b)
+{
+    return !(a == b);
+}
+
+}

Modified: trunk/Tools/ChangeLog (261793 => 261794)


--- trunk/Tools/ChangeLog	2020-05-17 16:17:45 UTC (rev 261793)
+++ trunk/Tools/ChangeLog	2020-05-17 19:28:12 UTC (rev 261794)
@@ -1,3 +1,13 @@
+2020-05-17  Simon Fraser  <[email protected]>
+
+        Fix operator== and hash() for ExtendedColor
+        https://bugs.webkit.org/show_bug.cgi?id=211993
+
+        Reviewed by Sam Weinig.
+
+        * TestWebKitAPI/Tests/WebCore/ExtendedColorTests.cpp:
+        (TestWebKitAPI::TEST):
+
 2020-05-16  Simon Fraser  <[email protected]>
 
         Rename some API test files

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/ExtendedColorTests.cpp (261793 => 261794)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/ExtendedColorTests.cpp	2020-05-17 16:17:45 UTC (rev 261793)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/ExtendedColorTests.cpp	2020-05-17 19:28:12 UTC (rev 261794)
@@ -77,6 +77,69 @@
     EXPECT_EQ(c2.cssText(), "color(display-p3 1 0.5 0.25)");
 }
 
+TEST(ExtendedColor, Equality)
+{
+    {
+        Color c1(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3);
+        EXPECT_TRUE(c1.isExtended());
+
+        Color c2(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3);
+        EXPECT_TRUE(c1.isExtended());
+
+        EXPECT_EQ(c1, c2);
+    }
+
+    {
+        Color c1(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3);
+        EXPECT_TRUE(c1.isExtended());
+
+        Color c2(1.0, 0.5, 0.25, 1.0, ColorSpace::SRGB);
+        EXPECT_TRUE(c1.isExtended());
+
+        EXPECT_NE(c1, c2);
+    }
+
+    int r = 255;
+    int g = 128;
+    int b = 63;
+    int a = 127;
+    Color rgb1(r / 255.0, g / 255.0, b / 255.0, a / 255.0, ColorSpace::SRGB);
+    Color rgb2(r, g, b, a);
+    EXPECT_NE(rgb1, rgb2);
+    EXPECT_NE(rgb2, rgb1);
+}
+
+TEST(ExtendedColor, Hash)
+{
+    {
+        Color c1(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3);
+        EXPECT_TRUE(c1.isExtended());
+
+        Color c2(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3);
+        EXPECT_TRUE(c1.isExtended());
+
+        EXPECT_EQ(c1.hash(), c2.hash());
+    }
+
+    {
+        Color c1(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3);
+        EXPECT_TRUE(c1.isExtended());
+
+        Color c2(1.0, 0.5, 0.25, 1.0, ColorSpace::SRGB);
+        EXPECT_TRUE(c1.isExtended());
+
+        EXPECT_NE(c1.hash(), c2.hash());
+    }
+
+    int r = 255;
+    int g = 128;
+    int b = 63;
+    int a = 127;
+    Color rgb1(r / 255.0, g / 255.0, b / 255.0, a / 255.0, ColorSpace::SRGB);
+    Color rgb2(r, g, b, a);
+    EXPECT_NE(rgb1.hash(), rgb2.hash());
+}
+
 TEST(ExtendedColor, MoveConstructor)
 {
     Color c1(1.0, 0.5, 0.25, 1.0, ColorSpace::DisplayP3);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to