Diff
Modified: trunk/Source/WebCore/ChangeLog (264229 => 264230)
--- trunk/Source/WebCore/ChangeLog 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/ChangeLog 2020-07-10 17:36:36 UTC (rev 264230)
@@ -1,3 +1,110 @@
+2020-07-10 Sam Weinig <[email protected]>
+
+ Part 4 of SimpleColor and SRGBA<uint8_t> are essentially the same - let's converge them
+ https://bugs.webkit.org/show_bug.cgi?id=214158
+
+ Reviewed by Darin Adler.
+
+ Removes SimpleColor!
+
+ Alas, makeSimpleColor() remains for now, seeking a new name in a follow up.
+
+ * platform/graphics/SimpleColor.h:
+ (WebCore::SimpleColor::SimpleColor): 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::invertedColorWithAlpha const): Deleted.
+ (WebCore::SimpleColor::asSRGBA const): Deleted.
+ (WebCore::SimpleColor::get const): Deleted.
+ (): Deleted.
+ (WebCore::operator==): Deleted.
+ (WebCore::operator!=): Deleted.
+ Remove the SimpleColor class.
+
+ * platform/graphics/Color.cpp:
+ (WebCore::Color::operator=):
+ (WebCore::Color::lightened const):
+ (WebCore::Color::darkened const):
+ (WebCore::Color::colorWithAlpha const):
+ (WebCore::Color::invertedColorWithAlpha const):
+ (WebCore::Color::colorSpaceAndComponents const):
+ * platform/graphics/Color.h:
+ (WebCore::Color::isHashTableDeletedValue const):
+ (WebCore::Color::isValid const):
+ (WebCore::Color::isSemantic const):
+ (WebCore::Color::isOpaque const):
+ (WebCore::Color::isVisible const):
+ (WebCore::Color::alpha const):
+ (WebCore::Color::alphaAsFloat const):
+ (WebCore::Color::isExtended const):
+ (WebCore::Color::isInline const):
+ (WebCore::Color::tagAsSemantic):
+ (WebCore::Color::tagAsValid):
+ (WebCore::operator==):
+ (WebCore::equalIgnoringSemanticColor):
+ (WebCore::Color::Color):
+ (WebCore::Color::~Color):
+ (WebCore::Color::hash const):
+ (WebCore::Color::toSRGBALossy const):
+ (WebCore::Color::asInline const):
+ (WebCore::Color::setInlineColor):
+ (WebCore::Color::setExtendedColor):
+ (WebCore::Color::isBlackColor):
+ (WebCore::Color::isWhiteColor):
+ (WebCore::Color::encode const):
+ (WebCore::Color::isSimple const): Deleted.
+ (WebCore::Color::asSimple const): Deleted.
+ (WebCore::Color::setSimpleColor): Deleted.
+ - Rename existing uses of Simple with Inline, denoting the inline SRGBA<uint8_t>.
+ - Move multiline functions out of line in the header.
+ - Adds new constructor functions taking Optional<SRGBA<uint8_t>> to make Color(CGColorRef)
+ constructors more straightforward.
+ - Replaces uses of SimpleColor's interface with use of ColorUtilities.h helpers. e.g.
+ Color::invertedColorWithAlpha() now uses the new invertedColorWithOverridenAlpha()
+ helper function. Use of these utilities will be expanded to ExtendedColor in a follow
+ up, allowing us to have just one implementation of these algorithms.
+
+ * platform/graphics/ColorSerialization.cpp:
+ (WebCore::serializationForCSS):
+ (WebCore::serializationForHTML):
+ (WebCore::serializationForRenderTreeAsText):
+ * platform/graphics/ColorSerialization.h:
+ Replace serialization of SimpleColor with serialization of SRGBA<uint8_t>.
+
+ * platform/graphics/ColorTypes.h:
+ (WebCore::forEachNonAlphaComponent): Deleted.
+ Removed forEachNonAlphaComponent. Uses replaced by new colorByModifingEachNonAlphaComponent
+ function in ColorUtilities.h
+
+ * platform/graphics/ColorUtilities.h:
+ (WebCore::colorByModifingEachNonAlphaComponent):
+ (WebCore::convertComponentByteTo<uint8_t>):
+ (WebCore::convertComponentByteTo<float>):
+ (WebCore::convertComponentFloatTo<uint8_t>):
+ (WebCore::convertComponentFloatTo<float>):
+ (WebCore::colorWithOverridenAlpha):
+ (WebCore::invertComponent):
+ (WebCore::invertedColorWithOverridenAlpha):
+ Added heplers to implement color mutation behaviors.
+
+ * platform/graphics/cg/ColorCG.cpp:
+ (WebCore::makeSimpleColorFromCGColor):
+ (WebCore::Color::Color):
+ (WebCore::cachedCGColor):
+ Switch makeSimpleColorFromCGColor to return an Optional<SRGBA<uint8_t>> to streamline
+ the constructors. Update to use is/asInline functions.
+
+ * platform/graphics/mac/ColorMac.mm:
+ (WebCore::nsColor):
+ Update to use is/asInline functions.
+
+ * platform/graphics/filters/FilterOperation.cpp:
+ (WebCore::BasicComponentTransferFilterOperation::transformColor const):
+ Switch to using colorByModifingEachNonAlphaComponent().
+
2020-07-10 Philippe Normand <[email protected]>
[GStreamer] Unreviewed, prospective API test bot fix...
Modified: trunk/Source/WebCore/platform/graphics/Color.cpp (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/Color.cpp 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/Color.cpp 2020-07-10 17:36:36 UTC (rev 264230)
@@ -75,7 +75,7 @@
m_colorData.extendedColor->deref();
m_colorData = other.m_colorData;
- other.m_colorData.simpleColorAndFlags = invalidSimpleColor;
+ other.m_colorData.inlineColorAndFlags = invalidInlineColor;
return *this;
}
@@ -83,7 +83,7 @@
Color Color::lightened() const
{
// Hardcode this common case for speed.
- if (isSimple() && asSimple() == SimpleColor { black })
+ if (isInline() && asInline() == black)
return lightenedBlack;
auto [r, g, b, a] = toSRGBALossy<float>();
@@ -100,7 +100,7 @@
Color Color::darkened() const
{
// Hardcode this common case for speed.
- if (isSimple() && asSimple() == SimpleColor { white })
+ if (isInline() && asInline() == white)
return darkenedWhite;
auto [r, g, b, a] = toSRGBALossy<float>();
@@ -129,7 +129,7 @@
if (isExtended())
return asExtended().colorWithAlpha(alpha);
- Color result = asSimple().colorWithAlpha(convertToComponentByte(alpha));
+ Color result = colorWithOverridenAlpha(asInline(), alpha);
// FIXME: Why is preserving the semantic bit desired and/or correct here?
if (isSemantic())
@@ -141,7 +141,7 @@
{
if (isExtended())
return asExtended().invertedColorWithAlpha(alpha);
- return asSimple().invertedColorWithAlpha(convertToComponentByte(alpha));
+ return invertedColorWithOverridenAlpha(asInline(), alpha);
}
Color Color::semanticColor() const
@@ -156,7 +156,7 @@
{
if (isExtended())
return { asExtended().colorSpace(), asExtended().components() };
- return { ColorSpace::SRGB, asColorComponents(asSimple().asSRGBA<float>()) };
+ return { ColorSpace::SRGB, asColorComponents(convertToComponentFloats(asInline())) };
}
TextStream& operator<<(TextStream& ts, const Color& color)
Modified: trunk/Source/WebCore/platform/graphics/Color.h (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/Color.h 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/Color.h 2020-07-10 17:36:36 UTC (rev 264230)
@@ -56,66 +56,35 @@
public:
Color() = default;
- Color(SRGBA<uint8_t> color)
- {
- setSimpleColor(color);
- }
+ Color(SRGBA<uint8_t>);
+ Color(Optional<SRGBA<uint8_t>>);
enum SemanticTag { Semantic };
- Color(SRGBA<uint8_t> color, SemanticTag)
- : Color(color)
- {
- tagAsSemantic();
- }
+ Color(SRGBA<uint8_t>, SemanticTag);
+ Color(Optional<SRGBA<uint8_t>>, SemanticTag);
- Color(Ref<ExtendedColor>&& extendedColor)
- {
- // Zero the union, just in case a 32-bit system only assigns the
- // top 32 bits when copying the ExtendedColor pointer below.
- m_colorData.simpleColorAndFlags = 0;
- m_colorData.extendedColor = &extendedColor.leakRef();
- ASSERT(isExtended());
- }
+ Color(Ref<ExtendedColor>&&);
- explicit Color(WTF::HashTableDeletedValueType)
- {
- static_assert(deletedHashValue & invalidSimpleColor, "Color's deleted hash value must not look like an ExtendedColor");
- static_assert(!(deletedHashValue & validSimpleColorBit), "Color's deleted hash value must not look like a valid SimpleColor");
- static_assert(deletedHashValue & (1 << 4), "Color's deleted hash value must have some bits set that an SimpleColor wouldn't have");
- m_colorData.simpleColorAndFlags = deletedHashValue;
- ASSERT(!isExtended());
- }
+ explicit Color(WTF::HashTableEmptyValueType);
+ explicit Color(WTF::HashTableDeletedValueType);
+ bool isHashTableDeletedValue() const { return m_colorData.inlineColorAndFlags == deletedHashValue; }
- bool isHashTableDeletedValue() const
- {
- return m_colorData.simpleColorAndFlags == deletedHashValue;
- }
-
- explicit Color(WTF::HashTableEmptyValueType)
- {
- static_assert(emptyHashValue & invalidSimpleColor, "Color's empty hash value must not look like an ExtendedColor");
- static_assert(emptyHashValue & (1 << 4), "Color's deleted hash value must have some bits set that an SimpleColor wouldn't have");
- m_colorData.simpleColorAndFlags = emptyHashValue;
- ASSERT(!isExtended());
- }
-
WEBCORE_EXPORT Color(const Color&);
WEBCORE_EXPORT Color(Color&&);
- ~Color()
- {
- if (isExtended())
- m_colorData.extendedColor->deref();
- }
+ ~Color();
- bool isValid() const { return isExtended() || (m_colorData.simpleColorAndFlags & validSimpleColorBit); }
+ bool isValid() const { return isExtended() || (m_colorData.inlineColorAndFlags & validInlineColorBit); }
- bool isOpaque() const { return isExtended() ? asExtended().alpha() == 1.0 : asSimple().isOpaque(); }
- bool isVisible() const { return isExtended() ? asExtended().alpha() > 0.0 : asSimple().isVisible(); }
+ // True if the color originated from a CSS semantic color name.
+ bool isSemantic() const { return isInline() && (m_colorData.inlineColorAndFlags & isSemanticInlineColorBit); }
- uint8_t alpha() const { return isExtended() ? convertToComponentByte(asExtended().alpha()) : asSimple().alphaComponent(); }
- float alphaAsFloat() const { return isExtended() ? asExtended().alpha() : asSimple().alphaComponentAsFloat(); }
+ bool isOpaque() const { return isExtended() ? asExtended().alpha() == 1.0 : asInline().alpha == 255; }
+ bool isVisible() const { return isExtended() ? asExtended().alpha() > 0.0 : asInline().alpha > 0; }
+ uint8_t alpha() const { return isExtended() ? convertToComponentByte(asExtended().alpha()) : asInline().alpha; }
+ float alphaAsFloat() const { return isExtended() ? asExtended().alpha() : convertToComponentFloat(asInline().alpha); }
+
unsigned hash() const;
WEBCORE_EXPORT std::pair<ColorSpace, ColorComponents<float>> colorSpaceAndComponents() const;
@@ -144,9 +113,6 @@
Color semanticColor() const;
- // True if the color originated from a CSS semantic color name.
- bool isSemantic() const { return isSimple() && (m_colorData.simpleColorAndFlags & isSemanticSimpleColorBit); }
-
#if PLATFORM(GTK)
Color(const GdkRGBA&);
operator GdkRGBA() const;
@@ -172,11 +138,11 @@
static constexpr auto cyan = makeSimpleColor(0, 255, 255);
static constexpr auto yellow = makeSimpleColor(255, 255, 0);
- bool isExtended() const { return !(m_colorData.simpleColorAndFlags & invalidSimpleColor); }
- bool isSimple() const { return !isExtended(); }
+ bool isExtended() const { return !(m_colorData.inlineColorAndFlags & invalidInlineColor); }
+ bool isInline() const { return !isExtended(); }
const ExtendedColor& asExtended() const;
- const SimpleColor asSimple() const;
+ SRGBA<uint8_t> asInline() const;
WEBCORE_EXPORT Color& operator=(const Color&);
WEBCORE_EXPORT Color& operator=(Color&&);
@@ -193,30 +159,26 @@
template<class Decoder> static Optional<Color> decode(Decoder&);
private:
- Color(SimpleColor color)
- : Color(color.asSRGBA<uint8_t>())
- {
- }
+ void setInlineColor(SRGBA<uint8_t>);
+ void setExtendedColor(Ref<ExtendedColor>&&);
- void setSimpleColor(SRGBA<uint8_t>);
+ void tagAsSemantic() { m_colorData.inlineColorAndFlags |= isSemanticInlineColorBit; }
+ void tagAsValid() { m_colorData.inlineColorAndFlags |= validInlineColor; }
- void tagAsSemantic() { m_colorData.simpleColorAndFlags |= isSemanticSimpleColorBit; }
- void tagAsValid() { m_colorData.simpleColorAndFlags |= validSimpleColor; }
-
// 0x_______00 is an ExtendedColor pointer.
- // 0x_______01 is an invalid SimpleColor.
- // 0x_______11 is a valid SimpleColor.
+ // 0x_______01 is an invalid inline color.
+ // 0x_______11 is a valid inline color.
static const uint64_t extendedColor = 0x0;
- static const uint64_t invalidSimpleColor = 0x1;
- static const uint64_t validSimpleColorBit = 0x2;
- static const uint64_t validSimpleColor = 0x3;
- static const uint64_t isSemanticSimpleColorBit = 0x4;
+ static const uint64_t invalidInlineColor = 0x1;
+ static const uint64_t validInlineColorBit = 0x2;
+ static const uint64_t validInlineColor = 0x3;
+ static const uint64_t isSemanticInlineColorBit = 0x4;
static const uint64_t deletedHashValue = 0xFFFFFFFFFFFFFFFD;
static const uint64_t emptyHashValue = 0xFFFFFFFFFFFFFFFB;
union {
- uint64_t simpleColorAndFlags { invalidSimpleColor };
+ uint64_t inlineColorAndFlags { invalidInlineColor };
ExtendedColor* extendedColor;
} m_colorData;
};
@@ -239,7 +201,7 @@
if (a.isExtended() || b.isExtended())
return extendedColorsEqual(a, b);
- return a.m_colorData.simpleColorAndFlags == b.m_colorData.simpleColorAndFlags;
+ return a.m_colorData.inlineColorAndFlags == b.m_colorData.inlineColorAndFlags;
}
inline bool operator!=(const Color& a, const Color& b)
@@ -260,14 +222,67 @@
{
if (a.isExtended() || b.isExtended())
return extendedColorsEqual(a, b);
- return (a.m_colorData.simpleColorAndFlags & ~Color::isSemanticSimpleColorBit) == (b.m_colorData.simpleColorAndFlags & ~Color::isSemanticSimpleColorBit);
+ return (a.m_colorData.inlineColorAndFlags & ~Color::isSemanticInlineColorBit) == (b.m_colorData.inlineColorAndFlags & ~Color::isSemanticInlineColorBit);
}
+inline Color::Color(SRGBA<uint8_t> color)
+{
+ setInlineColor(color);
+}
+
+inline Color::Color(Optional<SRGBA<uint8_t>> color)
+{
+ if (color)
+ setInlineColor(*color);
+}
+
+inline Color::Color(SRGBA<uint8_t> color, SemanticTag)
+{
+ setInlineColor(color);
+ tagAsSemantic();
+}
+
+inline Color::Color(Optional<SRGBA<uint8_t>> color, SemanticTag)
+{
+ if (color) {
+ setInlineColor(*color);
+ tagAsSemantic();
+ }
+}
+
+inline Color::Color(Ref<ExtendedColor>&& extendedColor)
+{
+ setExtendedColor(WTFMove(extendedColor));
+}
+
+inline Color::Color(WTF::HashTableDeletedValueType)
+{
+ static_assert(deletedHashValue & invalidInlineColor, "Color's deleted hash value must not look like an ExtendedColor");
+ static_assert(!(deletedHashValue & validInlineColorBit), "Color's deleted hash value must not look like a valid InlineColor");
+ static_assert(deletedHashValue & (1 << 4), "Color's deleted hash value must have some bits set that an InlineColor wouldn't have");
+ m_colorData.inlineColorAndFlags = deletedHashValue;
+ ASSERT(!isExtended());
+}
+
+inline Color::Color(WTF::HashTableEmptyValueType)
+{
+ static_assert(emptyHashValue & invalidInlineColor, "Color's empty hash value must not look like an ExtendedColor");
+ static_assert(emptyHashValue & (1 << 4), "Color's deleted hash value must have some bits set that an InlineColor wouldn't have");
+ m_colorData.inlineColorAndFlags = emptyHashValue;
+ ASSERT(!isExtended());
+}
+
+inline Color::~Color()
+{
+ if (isExtended())
+ m_colorData.extendedColor->deref();
+}
+
inline unsigned Color::hash() const
{
if (isExtended())
return asExtended().hash();
- return WTF::intHash(m_colorData.simpleColorAndFlags);
+ return WTF::intHash(m_colorData.inlineColorAndFlags);
}
template<typename T> SRGBA<T> Color::toSRGBALossy() const
@@ -274,7 +289,10 @@
{
if (isExtended())
return asExtended().toSRGBALossy<T>();
- return asSimple().asSRGBA<T>();
+ if constexpr (std::is_same_v<T, uint8_t>)
+ return asInline();
+ if constexpr (std::is_same_v<T, float>)
+ return convertToComponentFloats(asInline());
}
inline Color Color::invertedColorWithAlpha(Optional<float> alpha) const
@@ -303,23 +321,32 @@
return *m_colorData.extendedColor;
}
-inline const SimpleColor Color::asSimple() const
+inline SRGBA<uint8_t> Color::asInline() const
{
- ASSERT(isSimple());
- return SimpleColor { asSRGBA(Packed::RGBA { static_cast<uint32_t>(m_colorData.simpleColorAndFlags >> 32) }) };
+ ASSERT(isInline());
+ return asSRGBA(Packed::RGBA { static_cast<uint32_t>(m_colorData.inlineColorAndFlags >> 32) });
}
-inline void Color::setSimpleColor(SRGBA<uint8_t> color)
+inline void Color::setInlineColor(SRGBA<uint8_t> color)
{
- m_colorData.simpleColorAndFlags = static_cast<uint64_t>(Packed::RGBA { color }.value) << 32;
+ m_colorData.inlineColorAndFlags = static_cast<uint64_t>(Packed::RGBA { color }.value) << 32;
tagAsValid();
}
+inline void Color::setExtendedColor(Ref<ExtendedColor>&& extendedColor)
+{
+ // Zero the union, just in case a 32-bit system only assigns the
+ // top 32 bits when copying the ExtendedColor pointer below.
+ m_colorData.inlineColorAndFlags = 0;
+ m_colorData.extendedColor = &extendedColor.leakRef();
+ ASSERT(isExtended());
+}
+
inline bool Color::isBlackColor(const Color& color)
{
if (color.isExtended())
return color.asExtended().isBlack();
- return color.asSimple() == SimpleColor { Color::black };
+ return color.asInline() == Color::black;
}
inline bool Color::isWhiteColor(const Color& color)
@@ -326,7 +353,7 @@
{
if (color.isExtended())
return color.asExtended().isWhite();
- return color.asSimple() == SimpleColor { Color::white };
+ return color.asInline() == Color::white;
}
template<class Encoder>
@@ -355,7 +382,7 @@
// FIXME: This should encode whether the color is semantic.
encoder << true;
- encoder << Packed::RGBA { asSimple().asSRGBA<uint8_t>() }.value;
+ encoder << Packed::RGBA { asInline() }.value;
}
template<class Decoder>
Modified: trunk/Source/WebCore/platform/graphics/ColorSerialization.cpp (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/ColorSerialization.cpp 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/ColorSerialization.cpp 2020-07-10 17:36:36 UTC (rev 264230)
@@ -34,7 +34,7 @@
namespace WebCore {
-// SimpleColor overloads
+// SRGBA<uint8_t> overloads
static char decimalDigit(unsigned number)
{
@@ -53,7 +53,7 @@
return { { decimalDigit((alpha * 10 + 0x7F) / 0xFF), '\0', '\0', '\0' } };
}
-String serializationForCSS(SimpleColor color)
+String serializationForCSS(SRGBA<uint8_t> color)
{
auto [red, green, blue, alpha] = color;
switch (alpha) {
@@ -66,15 +66,15 @@
}
}
-String serializationForHTML(SimpleColor color)
+String serializationForHTML(SRGBA<uint8_t> color)
{
auto [red, green, blue, alpha] = color;
- if (color.isOpaque())
+ if (alpha == 0xFF)
return makeString('#', hex(red, 2, Lowercase), hex(green, 2, Lowercase), hex(blue, 2, Lowercase));
return serializationForCSS(color);
}
-String serializationForRenderTreeAsText(SimpleColor color)
+String serializationForRenderTreeAsText(SRGBA<uint8_t> color)
{
auto [red, green, blue, alpha] = color;
if (alpha < 0xFF)
@@ -124,7 +124,7 @@
{
if (color.isExtended())
return serializationForCSS(color.asExtended());
- return serializationForCSS(color.asSimple());
+ return serializationForCSS(color.asInline());
}
String serializationForHTML(const Color& color)
@@ -131,7 +131,7 @@
{
if (color.isExtended())
return serializationForHTML(color.asExtended());
- return serializationForHTML(color.asSimple());
+ return serializationForHTML(color.asInline());
}
String serializationForRenderTreeAsText(const Color& color)
@@ -138,7 +138,7 @@
{
if (color.isExtended())
return serializationForRenderTreeAsText(color.asExtended());
- return serializationForRenderTreeAsText(color.asSimple());
+ return serializationForRenderTreeAsText(color.asInline());
}
}
Modified: trunk/Source/WebCore/platform/graphics/ColorSerialization.h (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/ColorSerialization.h 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/ColorSerialization.h 2020-07-10 17:36:36 UTC (rev 264230)
@@ -29,15 +29,16 @@
class Color;
class ExtendedColor;
-class SimpleColor;
+template<typename> struct SRGBA;
+
// serializationForHTML returns the color serialized according to HTML5 <https://html.spec.whatwg.org/multipage/scripting.html#fill-and-stroke-styles> (10 September 2015)
// serializationForCSS returns the color serialized according to CSS
// serializationForRenderTreeAsText returns the color serialized for DumpRenderTree, #RRGGBB, #RRGGBBAA or the CSS serialization
-WEBCORE_EXPORT String serializationForCSS(SimpleColor);
-WEBCORE_EXPORT String serializationForHTML(SimpleColor);
-WEBCORE_EXPORT String serializationForRenderTreeAsText(SimpleColor);
+WEBCORE_EXPORT String serializationForCSS(SRGBA<uint8_t>);
+WEBCORE_EXPORT String serializationForHTML(SRGBA<uint8_t>);
+WEBCORE_EXPORT String serializationForRenderTreeAsText(SRGBA<uint8_t>);
WEBCORE_EXPORT String serializationForCSS(const ExtendedColor&);
WEBCORE_EXPORT String serializationForHTML(const ExtendedColor&);
Modified: trunk/Source/WebCore/platform/graphics/ColorTypes.h (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/ColorTypes.h 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/ColorTypes.h 2020-07-10 17:36:36 UTC (rev 264230)
@@ -58,14 +58,7 @@
return !(a == b);
}
-template<typename T, typename Functor> void forEachNonAlphaComponent(SRGBA<T>& color, Functor&& f)
-{
- color.red = f(color.red);
- color.green = f(color.green);
- color.blue = f(color.blue);
-}
-
template<typename T> struct LinearSRGBA {
T red;
T green;
Modified: trunk/Source/WebCore/platform/graphics/ColorUtilities.h (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/ColorUtilities.h 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/ColorUtilities.h 2020-07-10 17:36:36 UTC (rev 264230)
@@ -44,6 +44,35 @@
SRGBA<uint8_t> premultipliedCeiling(SRGBA<uint8_t>);
SRGBA<uint8_t> unpremultiplied(SRGBA<uint8_t>);
+uint8_t convertPrescaledToComponentByte(float);
+
+uint8_t convertToComponentByte(float);
+constexpr float convertToComponentFloat(uint8_t);
+
+constexpr uint8_t clampToComponentByte(int);
+constexpr uint8_t clampToComponentFloat(float);
+
+template<typename T> T convertComponentByteTo(uint8_t);
+template<typename T> T convertComponentFloatTo(float);
+
+template<template<typename> typename ColorType> ColorType<uint8_t> convertToComponentBytes(const ColorType<float>&);
+template<template<typename> typename ColorType> constexpr ColorType<float> convertToComponentFloats(const ColorType<uint8_t>&);
+
+template<template<typename> typename ColorType> constexpr ColorType<uint8_t> clampToComponentBytes(int r, int g, int b, int a);
+template<template<typename> typename ColorType> constexpr ColorType<float> clampToComponentFloats(float r, float g, float b, float a);
+
+template<typename ColorType, typename Functor> ColorType colorByModifingEachNonAlphaComponent(const ColorType&, Functor&&);
+
+template<typename ColorType> constexpr ColorType colorWithOverridenAlpha(const ColorType&, uint8_t overrideAlpha);
+template<typename ColorType> ColorType colorWithOverridenAlpha(const ColorType&, float overrideAlpha);
+
+template<typename ColorType> constexpr ColorType invertedColorWithOverridenAlpha(const ColorType&, uint8_t overrideAlpha);
+template<typename ColorType> ColorType invertedColorWithOverridenAlpha(const ColorType&, float overrideAlpha);
+
+constexpr uint16_t fastMultiplyBy255(uint16_t);
+constexpr uint16_t fastDivideBy255(uint16_t);
+
+
inline uint8_t convertPrescaledToComponentByte(float f)
{
return std::clamp(std::lround(f), 0l, 255l);
@@ -54,6 +83,11 @@
return std::clamp(std::lround(f * 255.0f), 0l, 255l);
}
+constexpr float convertToComponentFloat(uint8_t byte)
+{
+ return byte / 255.0f;
+}
+
constexpr uint8_t clampToComponentByte(int c)
{
return std::clamp(c, 0, 255);
@@ -64,11 +98,26 @@
return std::clamp(f, 0.0f, 1.0f);
}
-constexpr float convertToComponentFloat(uint8_t byte)
+template<> constexpr uint8_t convertComponentByteTo<uint8_t>(uint8_t value)
{
- return byte / 255.0f;
+ return value;
}
+template<> constexpr float convertComponentByteTo<float>(uint8_t value)
+{
+ return convertToComponentFloat(value);
+}
+
+template<> inline uint8_t convertComponentFloatTo<uint8_t>(float value)
+{
+ return convertToComponentByte(value);
+}
+
+template<> inline float convertComponentFloatTo<float>(float value)
+{
+ return clampToComponentFloat(value);
+}
+
template<template<typename> typename ColorType> inline ColorType<uint8_t> convertToComponentBytes(const ColorType<float>& color)
{
auto components = asColorComponents(color);
@@ -91,6 +140,58 @@
return { clampToComponentFloat(r), clampToComponentFloat(g), clampToComponentFloat(b), clampToComponentFloat(a) };
}
+template<typename ColorType, typename Functor> ColorType colorByModifingEachNonAlphaComponent(const ColorType& color, Functor&& functor)
+{
+ // FIXME: This should be made to work with colors that don't use the names red, green, and blue for their channels.
+ auto copy = color;
+ copy.red = functor(color.red);
+ copy.green = functor(color.green);
+ copy.blue = functor(color.blue);
+ return copy;
+}
+
+template<typename ColorType> constexpr ColorType colorWithOverridenAlpha(const ColorType& color, uint8_t overrideAlpha)
+{
+ ColorType copy = color;
+ copy.alpha = convertComponentByteTo<decltype(copy.alpha)>(overrideAlpha);
+ return copy;
+}
+
+template<typename ColorType> ColorType colorWithOverridenAlpha(const ColorType& color, float overrideAlpha)
+{
+ ColorType copy = color;
+ copy.alpha = convertComponentFloatTo<decltype(copy.alpha)>(overrideAlpha);
+ return copy;
+}
+
+constexpr uint8_t invertComponent(uint8_t component)
+{
+ return 255 - component;
+}
+
+constexpr float invertComponent(float component)
+{
+ return 1.0f - component;
+}
+
+template<typename ColorType> constexpr ColorType invertedColorWithOverridenAlpha(const ColorType& color, uint8_t overrideAlpha)
+{
+ auto copy = colorByModifingEachNonAlphaComponent(color, [] (auto component) {
+ return invertComponent(component);
+ });
+ copy.alpha = convertComponentByteTo<decltype(copy.alpha)>(overrideAlpha);
+ return copy;
+}
+
+template<typename ColorType> ColorType invertedColorWithOverridenAlpha(const ColorType& color, float overrideAlpha)
+{
+ auto copy = colorByModifingEachNonAlphaComponent(color, [] (auto component) {
+ return invertComponent(component);
+ });
+ copy.alpha = convertComponentFloatTo<decltype(copy.alpha)>(overrideAlpha);
+ return copy;
+}
+
constexpr uint16_t fastMultiplyBy255(uint16_t value)
{
return (value << 8) - value;
Modified: trunk/Source/WebCore/platform/graphics/SimpleColor.h (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/SimpleColor.h 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/SimpleColor.h 2020-07-10 17:36:36 UTC (rev 264230)
@@ -26,78 +26,11 @@
#pragma once
#include "ColorBuilder.h"
-#include "ColorComponents.h"
#include "ColorTypes.h"
#include "ColorUtilities.h"
namespace WebCore {
-class SimpleColor {
-public:
- constexpr explicit SimpleColor(SRGBA<uint8_t> value) : m_value { value } { }
-
- constexpr uint8_t alphaComponent() const { return m_value.alpha; }
- constexpr float alphaComponentAsFloat() const { return convertToComponentFloat(m_value.alpha); }
-
- constexpr bool isOpaque() const { return m_value.alpha == 0xFF; }
- constexpr bool isVisible() const { return m_value.alpha; }
-
- constexpr SimpleColor colorWithAlpha(uint8_t alpha) const
- {
- return { m_value.red, m_value.green, m_value.blue, alpha };
- }
-
- constexpr SimpleColor invertedColorWithAlpha(uint8_t alpha) const
- {
- return { static_cast<uint8_t>(0xFF - m_value.red), static_cast<uint8_t>(0xFF - m_value.green), static_cast<uint8_t>(0xFF - m_value.blue), alpha };
- }
-
- template<typename T> constexpr SRGBA<T> asSRGBA() const
- {
- if constexpr (std::is_same_v<T, float>)
- return convertToComponentFloats(m_value);
- else if constexpr (std::is_same_v<T, uint8_t>)
- return m_value;
- }
-
- template<std::size_t N>
- constexpr uint8_t get() const
- {
- static_assert(N < 4);
- if constexpr (!N)
- return m_value.red;
- else if constexpr (N == 1)
- return m_value.green;
- else if constexpr (N == 2)
- return m_value.blue;
- else if constexpr (N == 3)
- return m_value.alpha;
- }
-
-private:
- constexpr SimpleColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : m_value { r, g, b, a } { }
-
- SRGBA<uint8_t> m_value { 0, 0, 0, 0 };
-};
-
-bool operator==(SimpleColor, SimpleColor);
-bool operator!=(SimpleColor, SimpleColor);
-
-inline bool operator==(SimpleColor a, SimpleColor b)
-{
- return a.asSRGBA<uint8_t>() == b.asSRGBA<uint8_t>();
-}
-
-inline bool operator!=(SimpleColor a, SimpleColor b)
-{
- return !(a == b);
-}
-
-constexpr ColorBuilder<SRGBA<uint8_t>> makeSimpleColor(int r, int g, int b);
-constexpr ColorBuilder<SRGBA<uint8_t>> makeSimpleColor(int r, int g, int b, int a);
-constexpr ColorBuilder<SRGBA<uint8_t>> makeSimpleColor(SRGBA<uint8_t>);
-ColorBuilder<SRGBA<uint8_t>> makeSimpleColor(const SRGBA<float>&);
-
constexpr ColorBuilder<SRGBA<uint8_t>> makeSimpleColor(int r, int g, int b)
{
return clampToComponentBytes<SRGBA>(r, g, b, 0xFF);
@@ -119,15 +52,3 @@
}
} // namespace WebCore
-
-namespace std {
-
-template<> class tuple_size<WebCore::SimpleColor> : public std::integral_constant<std::size_t, 4> {
-};
-
-template<std::size_t N> class tuple_element<N, WebCore::SimpleColor> {
-public:
- using type = uint8_t;
-};
-
-}
Modified: trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp 2020-07-10 17:36:36 UTC (rev 264230)
@@ -51,10 +51,13 @@
namespace WebCore {
-static SRGBA<uint8_t> makeSimpleColorFromCGColor(CGColorRef color)
+static Optional<SRGBA<uint8_t>> roundAndClampToSRGBALossy(CGColorRef color)
{
// FIXME: ExtendedColor - needs to handle color spaces.
+ if (!color)
+ return WTF::nullopt;
+
size_t numComponents = CGColorGetNumberOfComponents(color);
const CGFloat* components = CGColorGetComponents(color);
@@ -78,32 +81,17 @@
ASSERT_NOT_REACHED();
}
- return makeSimpleColor(SRGBA { r, g, b, a });
+ return convertToComponentBytes(SRGBA { r, g, b, a });
}
Color::Color(CGColorRef color)
+ : Color(roundAndClampToSRGBALossy(color))
{
- // FIXME: ExtendedColor - needs to handle color spaces.
-
- if (!color) {
- m_colorData.simpleColorAndFlags = invalidSimpleColor;
- return;
- }
-
- setSimpleColor(makeSimpleColorFromCGColor(color));
}
-Color::Color(CGColorRef color, SemanticTag)
+Color::Color(CGColorRef color, SemanticTag tag)
+ : Color(roundAndClampToSRGBALossy(color), tag)
{
- // FIXME: ExtendedColor - needs to handle color spaces.
-
- if (!color) {
- m_colorData.simpleColorAndFlags = invalidSimpleColor;
- return;
- }
-
- setSimpleColor(makeSimpleColorFromCGColor(color));
- tagAsSemantic();
}
static CGColorRef leakCGColor(const Color& color)
@@ -128,8 +116,8 @@
CGColorRef cachedCGColor(const Color& color)
{
- if (color.isSimple()) {
- switch (Packed::RGBA { color.asSimple().asSRGBA<uint8_t>() }.value) {
+ if (color.isInline()) {
+ switch (Packed::RGBA { color.asInline() }.value) {
case Packed::RGBA { Color::transparent }.value: {
static CGColorRef transparentCGColor = leakCGColor(color);
return transparentCGColor;
Modified: trunk/Source/WebCore/platform/graphics/filters/FilterOperation.cpp (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/filters/FilterOperation.cpp 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/filters/FilterOperation.cpp 2020-07-10 17:36:36 UTC (rev 264230)
@@ -158,7 +158,7 @@
return true;
case INVERT: {
float _oneMinusAmount_ = 1.0f - m_amount;
- forEachNonAlphaComponent(color, [&](float component) {
+ color = colorByModifingEachNonAlphaComponent(color, [&](float component) {
return 1.0f - (oneMinusAmount + component * (m_amount - oneMinusAmount));
});
return true;
@@ -165,13 +165,13 @@
}
case CONTRAST: {
float intercept = -(0.5f * m_amount) + 0.5f;
- forEachNonAlphaComponent(color, [&](float component) {
+ color = colorByModifingEachNonAlphaComponent(color, [&](float component) {
return std::clamp<float>(intercept + m_amount * component, 0.0f, 1.0f);
});
return true;
}
case BRIGHTNESS:
- forEachNonAlphaComponent(color, [&](float component) {
+ color = colorByModifingEachNonAlphaComponent(color, [&](float component) {
return std::max<float>(m_amount * component, 0.0f);
});
return true;
Modified: trunk/Source/WebCore/platform/graphics/gtk/ColorGtk.cpp (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/gtk/ColorGtk.cpp 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/gtk/ColorGtk.cpp 2020-07-10 17:36:36 UTC (rev 264230)
@@ -27,7 +27,7 @@
Color::Color(const GdkRGBA& color)
{
- setSimpleColor(makeSimpleColor(SRGBA { static_cast<float>(color.red), static_cast<float>(color.green), static_cast<float>(color.blue), static_cast<float>(color.alpha) }));
+ setInlineColor(convertToComponentBytes(SRGBA { static_cast<float>(color.red), static_cast<float>(color.green), static_cast<float>(color.blue), static_cast<float>(color.alpha) }));
}
Color::operator GdkRGBA() const
Modified: trunk/Source/WebCore/platform/graphics/mac/ColorMac.mm (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/mac/ColorMac.mm 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/mac/ColorMac.mm 2020-07-10 17:36:36 UTC (rev 264230)
@@ -122,8 +122,8 @@
NSColor *nsColor(const Color& color)
{
- if (color.isSimple()) {
- switch (Packed::RGBA { color.asSimple().asSRGBA<uint8_t>() }.value) {
+ if (color.isInline()) {
+ switch (Packed::RGBA { color.asInline() }.value) {
case Packed::RGBA { Color::transparent }.value: {
static NSColor *clearColor = [[NSColor colorWithSRGBRed:0 green:0 blue:0 alpha:0] retain];
return clearColor;
Modified: trunk/Source/WebCore/platform/graphics/win/ColorDirect2D.cpp (264229 => 264230)
--- trunk/Source/WebCore/platform/graphics/win/ColorDirect2D.cpp 2020-07-10 17:21:13 UTC (rev 264229)
+++ trunk/Source/WebCore/platform/graphics/win/ColorDirect2D.cpp 2020-07-10 17:36:36 UTC (rev 264230)
@@ -35,7 +35,7 @@
Color::Color(D2D1_COLOR_F color)
{
- setSimpleColor(makeSimpleColor(SRGBA { color.r, color.g, color.b, color.a }));
+ setInlineColor(convertToComponentBytes(SRGBA { color.r, color.g, color.b, color.a }));
}
Color::operator D2D1_COLOR_F() const