Title: [263901] trunk
Revision
263901
Author
[email protected]
Date
2020-07-03 09:55:41 -0700 (Fri, 03 Jul 2020)

Log Message

Split color conversion functions out of ColorUtilities.h/cpp into their own file.
https://bugs.webkit.org/show_bug.cgi?id=213803

Reviewed by Simon Fraser.

Source/WebCore:

Moves color conversion functions (both between color spaces like toDisplayP3(const SRGBA&)
and between different representations like toHSLA(const SRGBA&)) to their own files.

* Headers.cmake:
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:
Add new files.

* css/parser/CSSPropertyParserHelpers.cpp:
* editing/cocoa/DataDetection.mm:
* html/canvas/CanvasRenderingContext2DBase.cpp:
* html/canvas/CanvasStyle.cpp:
Add include of ColorConversion.h

* platform/graphics/ColorConversion.cpp: Added.
* platform/graphics/ColorConversion.h: Added.
* platform/graphics/ColorUtilities.cpp:
* platform/graphics/ColorUtilities.h:
Move functions to the new files.

* platform/graphics/ExtendedColor.cpp:
* platform/graphics/cairo/ImageBufferCairoBackend.cpp:
* platform/graphics/filters/FELighting.cpp:
Add include of ColorConversion.h, remove unncessary include of ColorUtilities.h

* platform/graphics/cairo/ImageBufferCairoSurfaceBackend.cpp:
* platform/graphics/filters/FEDisplacementMap.cpp:
* platform/graphics/filters/FEMorphology.cpp:
* platform/graphics/filters/FilterOperations.cpp:
Remove unncessary include of ColorUtilities.h

* platform/graphics/filters/FilterOperation.cpp:
Add include of ColorConversion.h and ColorTypes.h, remove unncessary include of ColorUtilities.h.

* platform/graphics/gtk/ColorGtk.cpp:
* platform/graphics/win/ColorDirect2D.cpp:
Remove unncessary include of ColorUtilities.h. Update to use standard float -> SimpleColor idiom
using SRGBA<float>.

Tools:

* TestWebKitAPI/Tests/WebCore/ColorTests.cpp:
Update to include ColorConversion.h and ColorTypes.h rather than ColorUtilities.h

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (263900 => 263901)


--- trunk/Source/WebCore/ChangeLog	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/ChangeLog	2020-07-03 16:55:41 UTC (rev 263901)
@@ -1,5 +1,51 @@
 2020-07-03  Sam Weinig  <[email protected]>
 
+        Split color conversion functions out of ColorUtilities.h/cpp into their own file.
+        https://bugs.webkit.org/show_bug.cgi?id=213803
+
+        Reviewed by Simon Fraser.
+
+        Moves color conversion functions (both between color spaces like toDisplayP3(const SRGBA&)
+        and between different representations like toHSLA(const SRGBA&)) to their own files.
+
+        * Headers.cmake:
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        Add new files.
+
+        * css/parser/CSSPropertyParserHelpers.cpp:
+        * editing/cocoa/DataDetection.mm:
+        * html/canvas/CanvasRenderingContext2DBase.cpp:
+        * html/canvas/CanvasStyle.cpp:
+        Add include of ColorConversion.h
+
+        * platform/graphics/ColorConversion.cpp: Added.
+        * platform/graphics/ColorConversion.h: Added.
+        * platform/graphics/ColorUtilities.cpp:
+        * platform/graphics/ColorUtilities.h:
+        Move functions to the new files.
+
+        * platform/graphics/ExtendedColor.cpp:
+        * platform/graphics/cairo/ImageBufferCairoBackend.cpp:
+        * platform/graphics/filters/FELighting.cpp:
+        Add include of ColorConversion.h, remove unncessary include of ColorUtilities.h
+
+        * platform/graphics/cairo/ImageBufferCairoSurfaceBackend.cpp:
+        * platform/graphics/filters/FEDisplacementMap.cpp:
+        * platform/graphics/filters/FEMorphology.cpp:
+        * platform/graphics/filters/FilterOperations.cpp:
+        Remove unncessary include of ColorUtilities.h
+
+        * platform/graphics/filters/FilterOperation.cpp:
+        Add include of ColorConversion.h and ColorTypes.h, remove unncessary include of ColorUtilities.h.
+
+        * platform/graphics/gtk/ColorGtk.cpp:
+        * platform/graphics/win/ColorDirect2D.cpp:
+        Remove unncessary include of ColorUtilities.h. Update to use standard float -> SimpleColor idiom
+        using SRGBA<float>.
+
+2020-07-03  Sam Weinig  <[email protected]>
+
         Convert DateComponents parsing code to use Optional based return values rather than out-parameters
         https://bugs.webkit.org/show_bug.cgi?id=213440
 

Modified: trunk/Source/WebCore/Headers.cmake (263900 => 263901)


--- trunk/Source/WebCore/Headers.cmake	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/Headers.cmake	2020-07-03 16:55:41 UTC (rev 263901)
@@ -1083,6 +1083,7 @@
     platform/graphics/BitmapImage.h
     platform/graphics/Color.h
     platform/graphics/ColorComponents.h
+    platform/graphics/ColorConversion.h
     platform/graphics/ColorHash.h
     platform/graphics/ColorMatrix.h
     platform/graphics/ColorSerialization.h

Modified: trunk/Source/WebCore/Sources.txt (263900 => 263901)


--- trunk/Source/WebCore/Sources.txt	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/Sources.txt	2020-07-03 16:55:41 UTC (rev 263901)
@@ -1908,6 +1908,7 @@
 platform/graphics/BitmapImage.cpp
 platform/graphics/Color.cpp
 platform/graphics/ColorBlending.cpp
+platform/graphics/ColorConversion.cpp
 platform/graphics/ColorSerialization.cpp
 platform/graphics/ColorUtilities.cpp
 platform/graphics/ComplexTextController.cpp

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (263900 => 263901)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2020-07-03 16:55:41 UTC (rev 263901)
@@ -2252,6 +2252,7 @@
 		7C4C96DD1AD4483500365A60 /* JSWritableStream.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C4C96D91AD4483500365A60 /* JSWritableStream.h */; };
 		7C4C96DF1AD4483500363572 /* JSReadableStreamBYOBReader.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C4C96DB1AD4483500363572 /* JSReadableStreamBYOBReader.h */; };
 		7C4C96DF1AD4483500365A50 /* JSReadableStreamDefaultReader.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C4C96DB1AD4483500365A50 /* JSReadableStreamDefaultReader.h */; };
+		7C514E0324AF805E0050710F /* ColorConversion.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C514E0024AF80580050710F /* ColorConversion.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		7C516AD41F3525200034B6BF /* DOMPromiseProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C516AD21F3525200034B6BF /* DOMPromiseProxy.h */; };
 		7C5222961E1DAE03002CB8F7 /* IDLTypes.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C5222951E1DADF8002CB8F7 /* IDLTypes.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		7C5222991E1DAE1C002CB8F7 /* ActiveDOMCallback.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C5222981E1DAE16002CB8F7 /* ActiveDOMCallback.h */; settings = {ATTRIBUTES = (Private, ); }; };
@@ -10021,6 +10022,8 @@
 		7C4C96DB1AD4483500363572 /* JSReadableStreamBYOBReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSReadableStreamBYOBReader.h; sourceTree = "<group>"; };
 		7C4C96DB1AD4483500365A50 /* JSReadableStreamDefaultReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSReadableStreamDefaultReader.h; sourceTree = "<group>"; };
 		7C4EDD731A7B607800198C4D /* FontCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FontCocoa.mm; sourceTree = "<group>"; };
+		7C514E0024AF80580050710F /* ColorConversion.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ColorConversion.h; sourceTree = "<group>"; };
+		7C514E0224AF80580050710F /* ColorConversion.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ColorConversion.cpp; sourceTree = "<group>"; };
 		7C516AD21F3525200034B6BF /* DOMPromiseProxy.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DOMPromiseProxy.h; sourceTree = "<group>"; };
 		7C5222951E1DADF8002CB8F7 /* IDLTypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDLTypes.h; sourceTree = "<group>"; };
 		7C5222971E1DAE16002CB8F7 /* ActiveDOMCallback.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ActiveDOMCallback.cpp; sourceTree = "<group>"; };
@@ -25508,6 +25511,8 @@
 				7C1B4A6524A997590033727F /* ColorBlending.cpp */,
 				7C1B4A6724A997660033727F /* ColorBlending.h */,
 				7CAC6AEC247F1C5100E61D59 /* ColorComponents.h */,
+				7C514E0224AF80580050710F /* ColorConversion.cpp */,
+				7C514E0024AF80580050710F /* ColorConversion.h */,
 				3103B7DE1DB01556008BB890 /* ColorHash.h */,
 				7CAC6AE8247F082000E61D59 /* ColorMatrix.h */,
 				7CD1E69224ABF6240089C419 /* ColorSerialization.cpp */,
@@ -33912,6 +33917,7 @@
 				91278D6221DEDAF000B57184 /* WorkerAuditAgent.h in Headers */,
 				41A7D3531F438D16008988DE /* WorkerCacheStorageConnection.h in Headers */,
 				A5B81CCD1FAA44DE0037D1E6 /* WorkerConsoleAgent.h in Headers */,
+				7C514E0324AF805E0050710F /* ColorConversion.h in Headers */,
 				A55639D11C6F09E300806D8E /* WorkerConsoleClient.h in Headers */,
 				A5B81CCE1FAA44DE0037D1E6 /* WorkerDebuggerAgent.h in Headers */,
 				A52B349E1FA41703008B6246 /* WorkerDebuggerProxy.h in Headers */,

Modified: trunk/Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp (263900 => 263901)


--- trunk/Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/css/parser/CSSPropertyParserHelpers.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -41,6 +41,7 @@
 #include "CSSPaintImageValue.h"
 #include "CSSParserIdioms.h"
 #include "CSSValuePool.h"
+#include "ColorConversion.h"
 #include "Pair.h"
 #include "RuntimeEnabledFeatures.h"
 #include "StyleColor.h"

Modified: trunk/Source/WebCore/editing/cocoa/DataDetection.mm (263900 => 263901)


--- trunk/Source/WebCore/editing/cocoa/DataDetection.mm	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/editing/cocoa/DataDetection.mm	2020-07-03 16:55:41 UTC (rev 263901)
@@ -30,6 +30,7 @@
 
 #import "Attr.h"
 #import "CSSStyleDeclaration.h"
+#import "ColorConversion.h"
 #import "ColorSerialization.h"
 #import "Editing.h"
 #import "ElementAncestorIterator.h"

Modified: trunk/Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp (263900 => 263901)


--- trunk/Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/html/canvas/CanvasRenderingContext2DBase.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -40,6 +40,7 @@
 #include "CachedImage.h"
 #include "CanvasGradient.h"
 #include "CanvasPattern.h"
+#include "ColorConversion.h"
 #include "ColorSerialization.h"
 #include "DOMMatrix.h"
 #include "DOMMatrix2DInit.h"

Modified: trunk/Source/WebCore/html/canvas/CanvasStyle.cpp (263900 => 263901)


--- trunk/Source/WebCore/html/canvas/CanvasStyle.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/html/canvas/CanvasStyle.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -33,6 +33,7 @@
 #include "CSSPropertyNames.h"
 #include "CanvasGradient.h"
 #include "CanvasPattern.h"
+#include "ColorConversion.h"
 #include "GraphicsContext.h"
 #include "HTMLCanvasElement.h"
 #if ENABLE(OFFSCREEN_CANVAS)

Copied: trunk/Source/WebCore/platform/graphics/ColorConversion.cpp (from rev 263900, trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp) (0 => 263901)


--- trunk/Source/WebCore/platform/graphics/ColorConversion.cpp	                        (rev 0)
+++ trunk/Source/WebCore/platform/graphics/ColorConversion.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -0,0 +1,242 @@
+/*
+ * Copyright (C) 2017, 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 AND ITS CONTRIBUTORS "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 OR ITS 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 "ColorConversion.h"
+
+#include "ColorComponents.h"
+#include "ColorMatrix.h"
+#include "ColorTypes.h"
+
+namespace WebCore {
+
+float linearToRGBColorComponent(float c)
+{
+    if (c < 0.0031308f)
+        return 12.92f * c;
+
+    return clampTo<float>(1.055f * std::pow(c, 1.0f / 2.4f) - 0.055f, 0, 1);
+}
+
+float rgbToLinearColorComponent(float c)
+{
+    if (c <= 0.04045f)
+        return c / 12.92f;
+
+    return clampTo<float>(std::pow((c + 0.055f) / 1.055f, 2.4f), 0, 1);
+}
+
+LinearSRGBA<float> toLinearSRGBA(const SRGBA<float>& color)
+{
+    return {
+        rgbToLinearColorComponent(color.red),
+        rgbToLinearColorComponent(color.green),
+        rgbToLinearColorComponent(color.blue),
+        color.alpha
+    };
+}
+
+SRGBA<float> toSRGBA(const LinearSRGBA<float>& color)
+{
+    return {
+        linearToRGBColorComponent(color.red),
+        linearToRGBColorComponent(color.green),
+        linearToRGBColorComponent(color.blue),
+        color.alpha
+    };
+}
+
+LinearDisplayP3<float> toLinearDisplayP3(const DisplayP3<float>& color)
+{
+    return {
+        rgbToLinearColorComponent(color.red),
+        rgbToLinearColorComponent(color.green),
+        rgbToLinearColorComponent(color.blue),
+        color.alpha
+    };
+}
+
+DisplayP3<float> toDisplayP3(const LinearDisplayP3<float>& color)
+{
+    return {
+        linearToRGBColorComponent(color.red),
+        linearToRGBColorComponent(color.green),
+        linearToRGBColorComponent(color.blue),
+        color.alpha
+    };
+}
+
+static LinearSRGBA<float> toLinearSRGBA(const XYZA<float>& color)
+{
+    // https://en.wikipedia.org/wiki/SRGB
+    // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
+    constexpr ColorMatrix<3, 3> xyzToLinearSRGBMatrix {
+         3.2404542f, -1.5371385f, -0.4985314f,
+        -0.9692660f,  1.8760108f,  0.0415560f,
+         0.0556434f, -0.2040259f,  1.0572252f
+    };
+    return asLinearSRGBA(xyzToLinearSRGBMatrix.transformedColorComponents(asColorComponents(color)));
+}
+
+static XYZA<float> toXYZ(const LinearSRGBA<float>& color)
+{
+    // https://en.wikipedia.org/wiki/SRGB
+    // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
+    constexpr ColorMatrix<3, 3> linearSRGBToXYZMatrix {
+        0.4124564f,  0.3575761f,  0.1804375f,
+        0.2126729f,  0.7151522f,  0.0721750f,
+        0.0193339f,  0.1191920f,  0.9503041f
+    };
+    return asXYZA(linearSRGBToXYZMatrix.transformedColorComponents(asColorComponents(color)));
+}
+
+static LinearDisplayP3<float> toLinearDisplayP3(const XYZA<float>& color)
+{
+    // https://drafts.csswg.org/css-color/#color-conversion-code
+    constexpr ColorMatrix<3, 3> xyzToLinearDisplayP3Matrix {
+         2.493496911941425f,  -0.9313836179191239f, -0.4027107844507168f,
+        -0.8294889695615747f,  1.7626640603183463f,  0.0236246858419436f,
+         0.0358458302437845f, -0.0761723892680418f,  0.9568845240076872f
+    };
+    return asLinearDisplayP3(xyzToLinearDisplayP3Matrix.transformedColorComponents(asColorComponents(color)));
+}
+
+static XYZA<float> toXYZ(const LinearDisplayP3<float>& color)
+{
+    // https://drafts.csswg.org/css-color/#color-conversion-code
+    constexpr ColorMatrix<3, 3> linearDisplayP3ToXYZMatrix {
+        0.4865709486482162f, 0.2656676931690931f, 0.198217285234363f,
+        0.2289745640697488f, 0.6917385218365064f, 0.079286914093745f,
+        0.0f,                0.0451133818589026f, 1.043944368900976f
+    };
+    return asXYZA(linearDisplayP3ToXYZMatrix.transformedColorComponents(asColorComponents(color)));
+}
+
+SRGBA<float> toSRGBA(const DisplayP3<float>& color)
+{
+    return toSRGBA(toLinearSRGBA(toXYZ(toLinearDisplayP3(color))));
+}
+
+DisplayP3<float> toDisplayP3(const SRGBA<float>& color)
+{
+    return toDisplayP3(toLinearDisplayP3(toXYZ(toLinearSRGBA(color))));
+}
+
+HSLA<float> toHSLA(const SRGBA<float>& color)
+{
+    // http://en.wikipedia.org/wiki/HSL_color_space.
+    auto [r, g, b, alpha] = color;
+
+    auto [min, max] = std::minmax({ r, g, b });
+    float chroma = max - min;
+
+    float hue;
+    if (!chroma)
+        hue = 0;
+    else if (max == r)
+        hue = (60.0f * ((g - b) / chroma)) + 360.0f;
+    else if (max == g)
+        hue = (60.0f * ((b - r) / chroma)) + 120.0f;
+    else
+        hue = (60.0f * ((r - g) / chroma)) + 240.0f;
+
+    if (hue >= 360.0f)
+        hue -= 360.0f;
+
+    hue /= 360.0f;
+
+    float lightness = 0.5f * (max + min);
+    float saturation;
+    if (!chroma)
+        saturation = 0;
+    else if (lightness <= 0.5f)
+        saturation = (chroma / (max + min));
+    else
+        saturation = (chroma / (2.0f - (max + min)));
+
+    return {
+        hue,
+        saturation,
+        lightness,
+        alpha
+    };
+}
+
+// Hue is in the range 0-6, other args in 0-1.
+static float calcHue(float temp1, float temp2, float hueVal)
+{
+    if (hueVal < 0.0f)
+        hueVal += 6.0f;
+    else if (hueVal >= 6.0f)
+        hueVal -= 6.0f;
+    if (hueVal < 1.0f)
+        return temp1 + (temp2 - temp1) * hueVal;
+    if (hueVal < 3.0f)
+        return temp2;
+    if (hueVal < 4.0f)
+        return temp1 + (temp2 - temp1) * (4.0f - hueVal);
+    return temp1;
+}
+
+// Explanation of this algorithm can be found in the CSS Color 4 Module
+// specification at https://drafts.csswg.org/css-color-4/#hsl-to-rgb with
+// further explanation available at http://en.wikipedia.org/wiki/HSL_color_space
+SRGBA<float> toSRGBA(const HSLA<float>& color)
+{
+    auto [hue, saturation, lightness, alpha] = color;
+
+    // Convert back to RGB.
+    if (!saturation) {
+        return {
+            lightness,
+            lightness,
+            lightness,
+            alpha
+        };
+    }
+    
+    float temp2 = lightness <= 0.5f ? lightness * (1.0f + saturation) : lightness + saturation - lightness * saturation;
+    float temp1 = 2.0f * lightness - temp2;
+    
+    hue *= 6.0f; // calcHue() wants hue in the 0-6 range.
+    return {
+        calcHue(temp1, temp2, hue + 2.0f),
+        calcHue(temp1, temp2, hue),
+        calcHue(temp1, temp2, hue - 2.0f),
+        alpha
+    };
+}
+
+SRGBA<float> toSRGBA(const CMYKA<float>& color)
+{
+    auto [c, m, y, k, a] = color;
+    float colors = 1 - k;
+    float r = colors * (1.0f - c);
+    float g = colors * (1.0f - m);
+    float b = colors * (1.0f - y);
+    return { r, g, b, a };
+}
+
+} // namespace WebCore

Copied: trunk/Source/WebCore/platform/graphics/ColorConversion.h (from rev 263900, trunk/Source/WebCore/platform/graphics/ColorUtilities.h) (0 => 263901)


--- trunk/Source/WebCore/platform/graphics/ColorConversion.h	                        (rev 0)
+++ trunk/Source/WebCore/platform/graphics/ColorConversion.h	2020-07-03 16:55:41 UTC (rev 263901)
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2017, 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 AND ITS CONTRIBUTORS "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 OR ITS 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
+
+namespace WebCore {
+
+template<typename> struct CMYKA;
+template<typename> struct DisplayP3;
+template<typename> struct HSLA;
+template<typename> struct LinearDisplayP3;
+template<typename> struct LinearSRGBA;
+template<typename> struct SRGBA;
+
+// These are the standard sRGB <-> LinearRGB / DisplayP3 <-> LinearDisplayP3 conversion functions (https://en.wikipedia.org/wiki/SRGB).
+float linearToRGBColorComponent(float);
+float rgbToLinearColorComponent(float);
+
+LinearSRGBA<float> toLinearSRGBA(const SRGBA<float>&);
+SRGBA<float> toSRGBA(const LinearSRGBA<float>&);
+
+LinearDisplayP3<float> toLinearDisplayP3(const DisplayP3<float>&);
+DisplayP3<float> toDisplayP3(const LinearDisplayP3<float>&);
+
+SRGBA<float> toSRGBA(const DisplayP3<float>&);
+DisplayP3<float> toDisplayP3(const SRGBA<float>&);
+
+WEBCORE_EXPORT HSLA<float> toHSLA(const SRGBA<float>&);
+WEBCORE_EXPORT SRGBA<float> toSRGBA(const HSLA<float>&);
+
+SRGBA<float> toSRGBA(const CMYKA<float>&);
+
+} // namespace WebCore

Modified: trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/ColorUtilities.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -26,125 +26,11 @@
 #include "config.h"
 #include "ColorUtilities.h"
 
-#include "ColorComponents.h"
-#include "ColorMatrix.h"
+#include "ColorConversion.h"
 #include "ColorTypes.h"
 
 namespace WebCore {
 
-// These are the standard sRGB <-> linearRGB / standard DisplayP3 <-> LinearDisplayP3 conversion functions (https://en.wikipedia.org/wiki/SRGB).
-float linearToRGBColorComponent(float c)
-{
-    if (c < 0.0031308f)
-        return 12.92f * c;
-
-    return clampTo<float>(1.055f * std::pow(c, 1.0f / 2.4f) - 0.055f, 0, 1);
-}
-
-float rgbToLinearColorComponent(float c)
-{
-    if (c <= 0.04045f)
-        return c / 12.92f;
-
-    return clampTo<float>(std::pow((c + 0.055f) / 1.055f, 2.4f), 0, 1);
-}
-
-LinearSRGBA<float> toLinearSRGBA(const SRGBA<float>& color)
-{
-    return {
-        rgbToLinearColorComponent(color.red),
-        rgbToLinearColorComponent(color.green),
-        rgbToLinearColorComponent(color.blue),
-        color.alpha
-    };
-}
-
-SRGBA<float> toSRGBA(const LinearSRGBA<float>& color)
-{
-    return {
-        linearToRGBColorComponent(color.red),
-        linearToRGBColorComponent(color.green),
-        linearToRGBColorComponent(color.blue),
-        color.alpha
-    };
-}
-
-LinearDisplayP3<float> toLinearDisplayP3(const DisplayP3<float>& color)
-{
-    return {
-        rgbToLinearColorComponent(color.red),
-        rgbToLinearColorComponent(color.green),
-        rgbToLinearColorComponent(color.blue),
-        color.alpha
-    };
-}
-
-DisplayP3<float> toDisplayP3(const LinearDisplayP3<float>& color)
-{
-    return {
-        linearToRGBColorComponent(color.red),
-        linearToRGBColorComponent(color.green),
-        linearToRGBColorComponent(color.blue),
-        color.alpha
-    };
-}
-
-static LinearSRGBA<float> toLinearSRGBA(const XYZA<float>& color)
-{
-    // https://en.wikipedia.org/wiki/SRGB
-    // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
-    constexpr ColorMatrix<3, 3> xyzToLinearSRGBMatrix {
-         3.2404542f, -1.5371385f, -0.4985314f,
-        -0.9692660f,  1.8760108f,  0.0415560f,
-         0.0556434f, -0.2040259f,  1.0572252f
-    };
-    return asLinearSRGBA(xyzToLinearSRGBMatrix.transformedColorComponents(asColorComponents(color)));
-}
-
-static XYZA<float> toXYZ(const LinearSRGBA<float>& color)
-{
-    // https://en.wikipedia.org/wiki/SRGB
-    // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
-    constexpr ColorMatrix<3, 3> linearSRGBToXYZMatrix {
-        0.4124564f,  0.3575761f,  0.1804375f,
-        0.2126729f,  0.7151522f,  0.0721750f,
-        0.0193339f,  0.1191920f,  0.9503041f
-    };
-    return asXYZA(linearSRGBToXYZMatrix.transformedColorComponents(asColorComponents(color)));
-}
-
-static LinearDisplayP3<float> toLinearDisplayP3(const XYZA<float>& color)
-{
-    // https://drafts.csswg.org/css-color/#color-conversion-code
-    constexpr ColorMatrix<3, 3> xyzToLinearDisplayP3Matrix {
-         2.493496911941425f,  -0.9313836179191239f, -0.4027107844507168f,
-        -0.8294889695615747f,  1.7626640603183463f,  0.0236246858419436f,
-         0.0358458302437845f, -0.0761723892680418f,  0.9568845240076872f
-    };
-    return asLinearDisplayP3(xyzToLinearDisplayP3Matrix.transformedColorComponents(asColorComponents(color)));
-}
-
-static XYZA<float> toXYZ(const LinearDisplayP3<float>& color)
-{
-    // https://drafts.csswg.org/css-color/#color-conversion-code
-    constexpr ColorMatrix<3, 3> linearDisplayP3ToXYZMatrix {
-        0.4865709486482162f, 0.2656676931690931f, 0.198217285234363f,
-        0.2289745640697488f, 0.6917385218365064f, 0.079286914093745f,
-        0.0f,                0.0451133818589026f, 1.043944368900976f
-    };
-    return asXYZA(linearDisplayP3ToXYZMatrix.transformedColorComponents(asColorComponents(color)));
-}
-
-SRGBA<float> toSRGBA(const DisplayP3<float>& color)
-{
-    return toSRGBA(toLinearSRGBA(toXYZ(toLinearDisplayP3(color))));
-}
-
-DisplayP3<float> toDisplayP3(const SRGBA<float>& color)
-{
-    return toDisplayP3(toLinearDisplayP3(toXYZ(toLinearSRGBA(color))));
-}
-
 float lightness(const SRGBA<float>& color)
 {
     auto [r, g, b, a] = color;
@@ -174,110 +60,10 @@
     return (lighterLuminance + 0.05) / (darkerLuminance + 0.05);
 }
 
-HSLA<float> toHSLA(const SRGBA<float>& color)
-{
-    // http://en.wikipedia.org/wiki/HSL_color_space.
-    auto [r, g, b, alpha] = color;
-
-    auto [min, max] = std::minmax({ r, g, b });
-    float chroma = max - min;
-
-    float hue;
-    if (!chroma)
-        hue = 0;
-    else if (max == r)
-        hue = (60.0f * ((g - b) / chroma)) + 360.0f;
-    else if (max == g)
-        hue = (60.0f * ((b - r) / chroma)) + 120.0f;
-    else
-        hue = (60.0f * ((r - g) / chroma)) + 240.0f;
-
-    if (hue >= 360.0f)
-        hue -= 360.0f;
-
-    hue /= 360.0f;
-
-    float lightness = 0.5f * (max + min);
-    float saturation;
-    if (!chroma)
-        saturation = 0;
-    else if (lightness <= 0.5f)
-        saturation = (chroma / (max + min));
-    else
-        saturation = (chroma / (2.0f - (max + min)));
-
-    return {
-        hue,
-        saturation,
-        lightness,
-        alpha
-    };
-}
-
-// Hue is in the range 0-6, other args in 0-1.
-static float calcHue(float temp1, float temp2, float hueVal)
-{
-    if (hueVal < 0.0f)
-        hueVal += 6.0f;
-    else if (hueVal >= 6.0f)
-        hueVal -= 6.0f;
-    if (hueVal < 1.0f)
-        return temp1 + (temp2 - temp1) * hueVal;
-    if (hueVal < 3.0f)
-        return temp2;
-    if (hueVal < 4.0f)
-        return temp1 + (temp2 - temp1) * (4.0f - hueVal);
-    return temp1;
-}
-
-// Explanation of this algorithm can be found in the CSS Color 4 Module
-// specification at https://drafts.csswg.org/css-color-4/#hsl-to-rgb with
-// further explanation available at http://en.wikipedia.org/wiki/HSL_color_space
-SRGBA<float> toSRGBA(const HSLA<float>& color)
-{
-    auto [hue, saturation, lightness, alpha] = color;
-
-    // Convert back to RGB.
-    if (!saturation) {
-        return {
-            lightness,
-            lightness,
-            lightness,
-            alpha
-        };
-    }
-    
-    float temp2 = lightness <= 0.5f ? lightness * (1.0f + saturation) : lightness + saturation - lightness * saturation;
-    float temp1 = 2.0f * lightness - temp2;
-    
-    hue *= 6.0f; // calcHue() wants hue in the 0-6 range.
-    return {
-        calcHue(temp1, temp2, hue + 2.0f),
-        calcHue(temp1, temp2, hue),
-        calcHue(temp1, temp2, hue - 2.0f),
-        alpha
-    };
-}
-
-SRGBA<float> toSRGBA(const CMYKA<float>& color)
-{
-    auto [c, m, y, k, a] = color;
-    float colors = 1 - k;
-    float r = colors * (1.0f - c);
-    float g = colors * (1.0f - m);
-    float b = colors * (1.0f - y);
-    return { r, g, b, a };
-}
-
 SRGBA<float> premultiplied(const SRGBA<float>& color)
 {
     auto [r, g, b, a] = color;
-    return {
-        r * a,
-        g * a,
-        b * a,
-        a
-    };
+    return { r * a, g * a, b * a, a };
 }
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/platform/graphics/ColorUtilities.h (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/ColorUtilities.h	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/ColorUtilities.h	2020-07-03 16:55:41 UTC (rev 263901)
@@ -31,32 +31,8 @@
 
 namespace WebCore {
 
-template<typename> struct CMYKA;
-template<typename> struct DisplayP3;
-template<typename> struct HSLA;
-template<typename> struct LinearDisplayP3;
-template<typename> struct LinearSRGBA;
 template<typename> struct SRGBA;
 
-// 0-1 components, result is clamped.
-float linearToRGBColorComponent(float);
-float rgbToLinearColorComponent(float);
-
-LinearSRGBA<float> toLinearSRGBA(const SRGBA<float>&);
-SRGBA<float> toSRGBA(const LinearSRGBA<float>&);
-
-LinearDisplayP3<float> toLinearDisplayP3(const DisplayP3<float>&);
-DisplayP3<float> toDisplayP3(const LinearDisplayP3<float>&);
-
-SRGBA<float> toSRGBA(const DisplayP3<float>&);
-DisplayP3<float> toDisplayP3(const SRGBA<float>&);
-
-WEBCORE_EXPORT HSLA<float> toHSLA(const SRGBA<float>&);
-WEBCORE_EXPORT SRGBA<float> toSRGBA(const HSLA<float>&);
-
-SRGBA<float> toSRGBA(const CMYKA<float>&);
-
-
 float lightness(const SRGBA<float>&);
 float luminance(const SRGBA<float>&);
 float contrastRatio(const SRGBA<float>&, const SRGBA<float>&);

Modified: trunk/Source/WebCore/platform/graphics/ExtendedColor.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/ExtendedColor.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/ExtendedColor.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -27,8 +27,8 @@
 #include "ExtendedColor.h"
 
 #include "Color.h"
+#include "ColorConversion.h"
 #include "ColorTypes.h"
-#include "ColorUtilities.h"
 #include <wtf/Hasher.h>
 #include <wtf/MathExtras.h>
 

Modified: trunk/Source/WebCore/platform/graphics/cairo/ImageBufferCairoBackend.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/cairo/ImageBufferCairoBackend.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/cairo/ImageBufferCairoBackend.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -33,7 +33,7 @@
 #include "BitmapImage.h"
 #include "CairoOperations.h"
 #include "Color.h"
-#include "ColorUtilities.h"
+#include "ColorConversion.h"
 #include "GraphicsContext.h"
 #include "GraphicsContextImplCairo.h"
 #include "ImageBufferUtilitiesCairo.h"

Modified: trunk/Source/WebCore/platform/graphics/cairo/ImageBufferCairoSurfaceBackend.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/cairo/ImageBufferCairoSurfaceBackend.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/cairo/ImageBufferCairoSurfaceBackend.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -33,7 +33,6 @@
 #include "BitmapImage.h"
 #include "CairoOperations.h"
 #include "Color.h"
-#include "ColorUtilities.h"
 #include "GraphicsContext.h"
 #include "GraphicsContextImplCairo.h"
 #include "ImageBufferUtilitiesCairo.h"

Modified: trunk/Source/WebCore/platform/graphics/filters/FEDisplacementMap.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/filters/FEDisplacementMap.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/filters/FEDisplacementMap.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -24,7 +24,6 @@
 #include "config.h"
 #include "FEDisplacementMap.h"
 
-#include "ColorUtilities.h"
 #include "Filter.h"
 #include "GraphicsContext.h"
 #include "ImageData.h"

Modified: trunk/Source/WebCore/platform/graphics/filters/FELighting.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/filters/FELighting.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/filters/FELighting.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -28,7 +28,7 @@
 #include "config.h"
 #include "FELighting.h"
 
-#include "ColorUtilities.h"
+#include "ColorConversion.h"
 #include "FELightingNEON.h"
 #include "ImageData.h"
 #include <wtf/ParallelJobs.h>

Modified: trunk/Source/WebCore/platform/graphics/filters/FEMorphology.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/filters/FEMorphology.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/filters/FEMorphology.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -26,7 +26,6 @@
 #include "FEMorphology.h"
 
 #include "ColorComponents.h"
-#include "ColorUtilities.h"
 #include "Filter.h"
 #include "ImageData.h"
 #include <wtf/ParallelJobs.h>

Modified: trunk/Source/WebCore/platform/graphics/filters/FilterOperation.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/filters/FilterOperation.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/filters/FilterOperation.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -30,8 +30,9 @@
 #include "CachedResourceLoader.h"
 #include "CachedSVGDocumentReference.h"
 #include "ColorBlending.h"
+#include "ColorConversion.h"
 #include "ColorMatrix.h"
-#include "ColorUtilities.h"
+#include "ColorTypes.h"
 #include "FilterEffect.h"
 #include "SVGURIReference.h"
 #include <wtf/text/TextStream.h>

Modified: trunk/Source/WebCore/platform/graphics/filters/FilterOperations.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/filters/FilterOperations.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/filters/FilterOperations.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -26,7 +26,6 @@
 #include "config.h"
 #include "FilterOperations.h"
 
-#include "ColorUtilities.h"
 #include "FEGaussianBlur.h"
 #include "IntSize.h"
 #include "LengthFunctions.h"

Modified: trunk/Source/WebCore/platform/graphics/gtk/ColorGtk.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/gtk/ColorGtk.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/gtk/ColorGtk.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -21,19 +21,13 @@
 #include "config.h"
 #include "Color.h"
 
-#include "ColorUtilities.h"
 #include <gdk/gdk.h>
 
 namespace WebCore {
 
-Color::Color(const GdkRGBA& c)
+Color::Color(const GdkRGBA& color)
 {
-    setSimpleColor(makeSimpleColor(
-        static_cast<int>(c.red * 255),
-        static_cast<int>(c.green * 255),
-        static_cast<int>(c.blue * 255),
-        static_cast<int>(c.alpha * 255)
-    ));
+    setSimpleColor(makeSimpleColor(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/win/ColorDirect2D.cpp (263900 => 263901)


--- trunk/Source/WebCore/platform/graphics/win/ColorDirect2D.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Source/WebCore/platform/graphics/win/ColorDirect2D.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -28,7 +28,6 @@
 
 #if PLATFORM(WIN)
 
-#include "ColorUtilities.h"
 #include <d2d1.h>
 #include <d2d1_1helper.h>
 
@@ -36,7 +35,7 @@
 
 Color::Color(D2D1_COLOR_F color)
 {
-    setSimpleColor(makeSimpleColor(color.r * 255, color.g * 255, color.b * 255, color.a * 255));
+    setSimpleColor(makeSimpleColor(SRGBA { color.r, color.g, color.b, color.a }));
 }
 
 Color::operator D2D1_COLOR_F() const

Modified: trunk/Tools/ChangeLog (263900 => 263901)


--- trunk/Tools/ChangeLog	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Tools/ChangeLog	2020-07-03 16:55:41 UTC (rev 263901)
@@ -1,3 +1,13 @@
+2020-07-03  Sam Weinig  <[email protected]>
+
+        Split color conversion functions out of ColorUtilities.h/cpp into their own file.
+        https://bugs.webkit.org/show_bug.cgi?id=213803
+
+        Reviewed by Simon Fraser.
+
+        * TestWebKitAPI/Tests/WebCore/ColorTests.cpp:
+        Update to include ColorConversion.h and ColorTypes.h rather than ColorUtilities.h
+
 2020-07-03  Carlos Alberto Lopez Perez  <[email protected]>
 
         [WPE][webkitpy] Use headless driver instead of wayland driver

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/ColorTests.cpp (263900 => 263901)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/ColorTests.cpp	2020-07-03 16:45:29 UTC (rev 263900)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/ColorTests.cpp	2020-07-03 16:55:41 UTC (rev 263901)
@@ -27,7 +27,8 @@
 
 #include "Test.h"
 #include <WebCore/Color.h>
-#include <WebCore/ColorUtilities.h>
+#include <WebCore/ColorConversion.h>
+#include <WebCore/ColorTypes.h>
 
 using namespace WebCore;
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to