Title: [264577] trunk/Source/WebCore
Revision
264577
Author
[email protected]
Date
2020-07-19 10:59:04 -0700 (Sun, 19 Jul 2020)

Log Message

Additional Color related cleanups
https://bugs.webkit.org/show_bug.cgi?id=214514

Reviewed by Darin Adler.

* platform/graphics/ColorBuilder.h:
Remove redundant "public" and use decltype() rather than more wordy typename for static_assert.

* platform/graphics/ColorUtilities.h:
(WebCore::clampToComponentByte):
(WebCore::clampToComponentFloat):
Templatize to support any input.

(WebCore::clampToComponentBytes):
(WebCore::clampToComponentFloats):
Templatize using variadic templates to support any input and prepare for color types with more
than four components.

(WebCore::colorByModifingEachNonAlphaComponent):
Use std::invoke to support more types of functors.

(WebCore::colorWithOverridenAlpha):
(WebCore::invertedColorWithOverridenAlpha):
Use auto more consistently.

(WebCore::invertComponent): Deleted.
Replace invertComponent functions with ComponentTraits::maxValue - value inline at callsite.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (264576 => 264577)


--- trunk/Source/WebCore/ChangeLog	2020-07-19 13:59:36 UTC (rev 264576)
+++ trunk/Source/WebCore/ChangeLog	2020-07-19 17:59:04 UTC (rev 264577)
@@ -1,3 +1,33 @@
+2020-07-19  Sam Weinig  <[email protected]>
+
+        Additional Color related cleanups
+        https://bugs.webkit.org/show_bug.cgi?id=214514
+
+        Reviewed by Darin Adler.
+
+        * platform/graphics/ColorBuilder.h:
+        Remove redundant "public" and use decltype() rather than more wordy typename for static_assert.
+
+        * platform/graphics/ColorUtilities.h:
+        (WebCore::clampToComponentByte):
+        (WebCore::clampToComponentFloat):
+        Templatize to support any input.
+
+        (WebCore::clampToComponentBytes):
+        (WebCore::clampToComponentFloats):
+        Templatize using variadic templates to support any input and prepare for color types with more 
+        than four components. 
+
+        (WebCore::colorByModifingEachNonAlphaComponent):
+        Use std::invoke to support more types of functors.
+
+        (WebCore::colorWithOverridenAlpha):
+        (WebCore::invertedColorWithOverridenAlpha):
+        Use auto more consistently.
+
+        (WebCore::invertComponent): Deleted.
+        Replace invertComponent functions with ComponentTraits::maxValue - value inline at callsite.
+
 2020-07-18  Philippe Normand  <[email protected]>
 
         Unreviewed, fix build warnings in the WebXR backend

Modified: trunk/Source/WebCore/platform/graphics/ColorBuilder.h (264576 => 264577)


--- trunk/Source/WebCore/platform/graphics/ColorBuilder.h	2020-07-19 13:59:36 UTC (rev 264576)
+++ trunk/Source/WebCore/platform/graphics/ColorBuilder.h	2020-07-19 17:59:04 UTC (rev 264577)
@@ -29,7 +29,7 @@
 
 // Helper to allow convenient syntax for working with color types.
 // e.g. auto yellowWith50PercentAlpha = Color::yellow.colorWithAlpha(128);
-template<typename ColorType> struct ColorBuilder : public ColorType {
+template<typename ColorType> struct ColorBuilder : ColorType {
     using ColorType::ColorType;
 
     constexpr ColorType color() const
@@ -39,7 +39,7 @@
 
     constexpr ColorBuilder colorWithAlpha(uint8_t overrideAlpha) const
     {
-        static_assert(std::is_same_v<typename ColorType::ComponentType, uint8_t>, "Only uint8_t based color types are supported.");
+        static_assert(std::is_same_v<decltype(ColorType().alpha), uint8_t>, "Only uint8_t based color types are supported.");
 
         auto copy = *this;
         copy.alpha = overrideAlpha;

Modified: trunk/Source/WebCore/platform/graphics/ColorUtilities.h (264576 => 264577)


--- trunk/Source/WebCore/platform/graphics/ColorUtilities.h	2020-07-19 13:59:36 UTC (rev 264576)
+++ trunk/Source/WebCore/platform/graphics/ColorUtilities.h	2020-07-19 17:59:04 UTC (rev 264577)
@@ -50,8 +50,8 @@
 uint8_t convertToComponentByte(float);
 constexpr float convertToComponentFloat(uint8_t);
 
-constexpr uint8_t clampToComponentByte(int);
-constexpr float clampToComponentFloat(float);
+template<typename ComponentType> constexpr uint8_t clampToComponentByte(ComponentType);
+template<typename ComponentType> constexpr float clampToComponentFloat(ComponentType);
 
 template<typename T> T convertComponentByteTo(uint8_t);
 template<typename T> T convertComponentFloatTo(float);
@@ -59,10 +59,8 @@
 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);
-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);
-template<template<typename> typename ColorType> constexpr ColorType<float> clampToComponentFloats(float r, float g, float b, float a);
+template<template<typename> typename ColorType, typename... ComponentType> constexpr ColorType<uint8_t> clampToComponentBytes(ComponentType...);
+template<template<typename> typename ColorType, typename... ComponentType> constexpr ColorType<float> clampToComponentFloats(ComponentType...);
 
 template<typename ColorType, typename Functor> ColorType colorByModifingEachNonAlphaComponent(const ColorType&, Functor&&);
 
@@ -94,14 +92,14 @@
     return byte / 255.0f;
 }
 
-constexpr uint8_t clampToComponentByte(int c)
+template<typename ComponentType> constexpr uint8_t clampToComponentByte(ComponentType component)
 {
-    return std::clamp(c, 0, 255);
+    return std::clamp<ComponentType>(component, 0, 255);
 }
 
-constexpr float clampToComponentFloat(float f)
+template<typename ComponentType> constexpr float clampToComponentFloat(ComponentType component)
 {
-    return std::clamp(f, 0.0f, 1.0f);
+    return std::clamp<ComponentType>(component, 0, 1);
 }
 
 template<> constexpr uint8_t convertComponentByteTo<uint8_t>(uint8_t value)
@@ -136,39 +134,29 @@
     return { convertToComponentFloat(components[0]), convertToComponentFloat(components[1]), convertToComponentFloat(components[2]), convertToComponentFloat(components[3]) };
 }
 
-template<template<typename> typename ColorType> constexpr ColorType<uint8_t> clampToComponentBytes(int r, int g, int b)
+template<template<typename> typename ColorType, typename... ComponentType> constexpr ColorType<uint8_t> clampToComponentBytes(ComponentType... components)
 {
-    return { clampToComponentByte(r), clampToComponentByte(g), clampToComponentByte(b) };
+    return { clampToComponentByte(components)... };
 }
 
-template<template<typename> typename ColorType> constexpr ColorType<uint8_t> clampToComponentBytes(int r, int g, int b, int a)
+template<template<typename> typename ColorType, typename... ComponentType> constexpr ColorType<float> clampToComponentFloats(ComponentType... components)
 {
-    return { clampToComponentByte(r), clampToComponentByte(g), clampToComponentByte(b), clampToComponentByte(a) };
+    return { clampToComponentFloat(components)... };
 }
 
-template<template<typename> typename ColorType> constexpr ColorType<float> clampToComponentFloats(float r, float g, float b)
-{
-    return { clampToComponentFloat(r), clampToComponentFloat(g), clampToComponentFloat(b) };
-}
-
-template<template<typename> typename ColorType> constexpr ColorType<float> clampToComponentFloats(float r, float g, float b, float a)
-{
-    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);
+    copy.red = std::invoke(functor, color.red);
+    copy.green = std::invoke(functor, color.green);
+    copy.blue = std::invoke(std::forward<Functor>(functor), color.blue);
     return copy;
 }
 
 template<typename ColorType> constexpr ColorType colorWithOverridenAlpha(const ColorType& color, uint8_t overrideAlpha)
 {
-    ColorType copy = color;
+    auto copy = color;
     copy.alpha = convertComponentByteTo<decltype(copy.alpha)>(overrideAlpha);
     return copy;
 }
@@ -175,25 +163,15 @@
 
 template<typename ColorType> ColorType colorWithOverridenAlpha(const ColorType& color, float overrideAlpha)
 {
-    ColorType copy = color;
+    auto 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);
+        return ComponentTraits<decltype(component)>::maxValue - component;
     });
     copy.alpha = convertComponentByteTo<decltype(copy.alpha)>(overrideAlpha);
     return copy;
@@ -202,7 +180,7 @@
 template<typename ColorType> ColorType invertedColorWithOverridenAlpha(const ColorType& color, float overrideAlpha)
 {
     auto copy = colorByModifingEachNonAlphaComponent(color, [] (auto component) {
-        return invertComponent(component);
+        return ComponentTraits<decltype(component)>::maxValue - component;
     });
     copy.alpha = convertComponentFloatTo<decltype(copy.alpha)>(overrideAlpha);
     return copy;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to