Extended Color: Move ColorMatrix to its own files
authorweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 27 May 2020 21:40:09 +0000 (21:40 +0000)
committerweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 27 May 2020 21:40:09 +0000 (21:40 +0000)
https://bugs.webkit.org/show_bug.cgi?id=212431

Reviewed by Dean Jackson.

Move ColorMatrix to its own files from ColorUtilities.h/cpp

* Headers.cmake:
* Sources.txt:
* WebCore.xcodeproj/project.pbxproj:
* platform/graphics/ColorMatrix.cpp: Copied from Source/WebCore/platform/graphics/ColorUtilities.cpp.
* platform/graphics/ColorMatrix.h: Copied from Source/WebCore/platform/graphics/ColorUtilities.h.
* platform/graphics/ColorUtilities.cpp:
* platform/graphics/ColorUtilities.h:
* platform/graphics/filters/FilterOperation.cpp:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@262218 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/Headers.cmake
Source/WebCore/Sources.txt
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/platform/graphics/ColorMatrix.cpp [new file with mode: 0644]
Source/WebCore/platform/graphics/ColorMatrix.h [new file with mode: 0644]
Source/WebCore/platform/graphics/ColorUtilities.cpp
Source/WebCore/platform/graphics/ColorUtilities.h
Source/WebCore/platform/graphics/filters/FilterOperation.cpp

index ad80004..4ac3e22 100644 (file)
@@ -1,3 +1,21 @@
+2020-05-27  Sam Weinig  <weinig@apple.com>
+
+        Extended Color: Move ColorMatrix to its own files
+        https://bugs.webkit.org/show_bug.cgi?id=212431
+
+        Reviewed by Dean Jackson.
+
+        Move ColorMatrix to its own files from ColorUtilities.h/cpp
+
+        * Headers.cmake:
+        * Sources.txt:
+        * WebCore.xcodeproj/project.pbxproj:
+        * platform/graphics/ColorMatrix.cpp: Copied from Source/WebCore/platform/graphics/ColorUtilities.cpp.
+        * platform/graphics/ColorMatrix.h: Copied from Source/WebCore/platform/graphics/ColorUtilities.h.
+        * platform/graphics/ColorUtilities.cpp:
+        * platform/graphics/ColorUtilities.h:
+        * platform/graphics/filters/FilterOperation.cpp:
+
 2020-05-27  David Kilzer  <ddkilzer@apple.com>
 
         Add OptionSetTraits<> and use with WebCore::DragDestinationAction
index 9badf45..361685e 100644 (file)
@@ -1080,6 +1080,7 @@ set(WebCore_PRIVATE_FRAMEWORK_HEADERS
     platform/graphics/BitmapImage.h
     platform/graphics/Color.h
     platform/graphics/ColorHash.h
+    platform/graphics/ColorMatrix.h
     platform/graphics/ColorSpace.h
     platform/graphics/ColorUtilities.h
     platform/graphics/ComplexTextController.h
index 59dc9c6..47622b9 100644 (file)
@@ -1872,6 +1872,7 @@ platform/encryptedmedia/CDMProxy.cpp
 platform/graphics/ANGLEWebKitBridge.cpp
 platform/graphics/BitmapImage.cpp
 platform/graphics/Color.cpp
+platform/graphics/ColorMatrix.cpp
 platform/graphics/ColorUtilities.cpp
 platform/graphics/ComplexTextController.cpp
 platform/graphics/CrossfadeGeneratedImage.cpp
index 182e295..2e17a30 100644 (file)
                7C9ACAC01F3CF1AF00F3AA09 /* JSCryptoRsaHashedKeyAlgorithm.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C9ACAB51F3CDC0200F3AA09 /* JSCryptoRsaHashedKeyAlgorithm.h */; };
                7C9ACAC21F3CF1AF00F3AA09 /* JSCryptoRsaKeyAlgorithm.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C9ACAB31F3CDC0100F3AA09 /* JSCryptoRsaKeyAlgorithm.h */; };
                7C9DBFEE1A9C49B1000D6B25 /* JSHTMLAttachmentElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C9DBFEC1A9C49B1000D6B25 /* JSHTMLAttachmentElement.h */; };
+               7CAC6AE9247F082F00E61D59 /* ColorMatrix.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CAC6AE8247F082000E61D59 /* ColorMatrix.h */; settings = {ATTRIBUTES = (Private, ); }; };
                7CB5CA401E525C7300FAEF13 /* MediaQueryExpression.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CB5CA3E1E525C6C00FAEF13 /* MediaQueryExpression.h */; settings = {ATTRIBUTES = (Private, ); }; };
                7CBBBCC71F560581005EFAAC /* DOMMatrix2DInit.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CBBBCC61F560568005EFAAC /* DOMMatrix2DInit.h */; };
                7CBBBCCA1F5617C5005EFAAC /* JSDOMMatrix2DInit.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CBBBCC91F5617C0005EFAAC /* JSDOMMatrix2DInit.h */; };
                7C9DBFEA1A9C489F000D6B25 /* HTMLAttachmentElement.idl */ = {isa = PBXFileReference; lastKnownFileType = text; path = HTMLAttachmentElement.idl; sourceTree = "<group>"; };
                7C9DBFEB1A9C49B1000D6B25 /* JSHTMLAttachmentElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSHTMLAttachmentElement.cpp; sourceTree = "<group>"; };
                7C9DBFEC1A9C49B1000D6B25 /* JSHTMLAttachmentElement.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSHTMLAttachmentElement.h; sourceTree = "<group>"; };
+               7CAC6AE6247F081F00E61D59 /* ColorMatrix.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ColorMatrix.cpp; sourceTree = "<group>"; };
+               7CAC6AE8247F082000E61D59 /* ColorMatrix.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ColorMatrix.h; sourceTree = "<group>"; };
                7CB5CA3D1E525C6C00FAEF13 /* MediaQueryExpression.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MediaQueryExpression.cpp; sourceTree = "<group>"; };
                7CB5CA3E1E525C6C00FAEF13 /* MediaQueryExpression.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MediaQueryExpression.h; sourceTree = "<group>"; };
                7CBA5BA61F0B4BDE0034D745 /* JSDOMConvertWebGL.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = JSDOMConvertWebGL.cpp; sourceTree = "<group>"; };
                                B27535380B053814002CE64F /* Color.cpp */,
                                B27535390B053814002CE64F /* Color.h */,
                                3103B7DE1DB01556008BB890 /* ColorHash.h */,
+                               7CAC6AE6247F081F00E61D59 /* ColorMatrix.cpp */,
+                               7CAC6AE8247F082000E61D59 /* ColorMatrix.h */,
                                9382DF5710A8D5C900925652 /* ColorSpace.h */,
                                0FE6C76B1FBFB7A60025C053 /* ColorUtilities.cpp */,
                                0FE6C76C1FBFB7A60025C053 /* ColorUtilities.h */,
                                F55B3DB41251F12D003EF269 /* ColorInputType.h in Headers */,
                                CE5169E721F1B84700EA4F78 /* ColorIOS.h in Headers */,
                                EDE3A5000C7A430600956A37 /* ColorMac.h in Headers */,
+                               7CAC6AE9247F082F00E61D59 /* ColorMatrix.h in Headers */,
                                9382DF5810A8D5C900925652 /* ColorSpace.h in Headers */,
                                0FEAF66B23BFC39E004030DA /* ColorUtilities.h in Headers */,
                                BCDD454E1236C95C009A7985 /* ColumnInfo.h in Headers */,
                                93309EA3099EB78C0056E581 /* SharedTimer.h in Headers */,
                                DF19E2AC24772BC1007BDACB /* ShouldRelaxThirdPartyCookieBlocking.h in Headers */,
                                8362E8C120CEF9CB00245886 /* ShouldTreatAsContinuingLoad.h in Headers */,
+                               7C31C88E247AD595005BF319 /* SimpleColor.h in Headers */,
                                E48944A3180B57D800F165D8 /* SimpleLineLayout.h in Headers */,
                                11E067EE1E6246E500162D16 /* SimpleLineLayoutCoverage.h in Headers */,
                                585D6E041A1A792E00FA4F12 /* SimpleLineLayoutFlowContents.h in Headers */,
                                CD336F6417FA0A4D00DDDCD0 /* VideoTrackPrivateAVF.h in Headers */,
                                CD336F6817FA0AC600DDDCD0 /* VideoTrackPrivateAVFObjC.h in Headers */,
                                CD8B5A43180D149A008B8E65 /* VideoTrackPrivateMediaSourceAVFObjC.h in Headers */,
-                               7C31C88E247AD595005BF319 /* SimpleColor.h in Headers */,
                                070E81D11BF27656001FDA48 /* VideoTrackPrivateMediaStream.h in Headers */,
                                CEF418CF1179678C009D112C /* ViewportArguments.h in Headers */,
                                26F9A83918A046AC00AEB88A /* ViewportConfiguration.h in Headers */,
diff --git a/Source/WebCore/platform/graphics/ColorMatrix.cpp b/Source/WebCore/platform/graphics/ColorMatrix.cpp
new file mode 100644 (file)
index 0000000..589f329
--- /dev/null
@@ -0,0 +1,183 @@
+/*
+ * Copyright (C) 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 "ColorMatrix.h"
+
+#include <wtf/MathExtras.h>
+
+namespace WebCore {
+
+ColorMatrix::ColorMatrix()
+{
+    makeIdentity();
+}
+
+ColorMatrix::ColorMatrix(const float values[20])
+{
+    m_matrix[0][0] = values[0];
+    m_matrix[0][1] = values[1];
+    m_matrix[0][2] = values[2];
+    m_matrix[0][3] = values[3];
+    m_matrix[0][4] = values[4];
+
+    m_matrix[1][0] = values[5];
+    m_matrix[1][1] = values[6];
+    m_matrix[1][2] = values[7];
+    m_matrix[1][3] = values[8];
+    m_matrix[1][4] = values[9];
+
+    m_matrix[2][0] = values[10];
+    m_matrix[2][1] = values[11];
+    m_matrix[2][2] = values[12];
+    m_matrix[2][3] = values[13];
+    m_matrix[2][4] = values[14];
+
+    m_matrix[3][0] = values[15];
+    m_matrix[3][1] = values[16];
+    m_matrix[3][2] = values[17];
+    m_matrix[3][3] = values[18];
+    m_matrix[3][4] = values[19];
+}
+
+void ColorMatrix::makeIdentity()
+{
+    memset(m_matrix, 0, sizeof(m_matrix));
+    m_matrix[0][0] = 1;
+    m_matrix[1][1] = 1;
+    m_matrix[2][2] = 1;
+    m_matrix[3][3] = 1;
+}
+
+ColorMatrix ColorMatrix::grayscaleMatrix(float amount)
+{
+    ColorMatrix matrix;
+
+    float oneMinusAmount = clampTo(1 - amount, 0.0, 1.0);
+
+    // Values from https://www.w3.org/TR/filter-effects-1/#grayscaleEquivalent
+    matrix.m_matrix[0][0] = 0.2126f + 0.7874f * oneMinusAmount;
+    matrix.m_matrix[0][1] = 0.7152f - 0.7152f * oneMinusAmount;
+    matrix.m_matrix[0][2] = 0.0722f - 0.0722f * oneMinusAmount;
+
+    matrix.m_matrix[1][0] = 0.2126f - 0.2126f * oneMinusAmount;
+    matrix.m_matrix[1][1] = 0.7152f + 0.2848f * oneMinusAmount;
+    matrix.m_matrix[1][2] = 0.0722f - 0.0722f * oneMinusAmount;
+
+    matrix.m_matrix[2][0] = 0.2126f - 0.2126f * oneMinusAmount;
+    matrix.m_matrix[2][1] = 0.7152f - 0.7152f * oneMinusAmount;
+    matrix.m_matrix[2][2] = 0.0722f + 0.9278f * oneMinusAmount;
+    
+    return matrix;
+}
+
+ColorMatrix ColorMatrix::saturationMatrix(float amount)
+{
+    ColorMatrix matrix;
+
+    // Values from https://www.w3.org/TR/filter-effects-1/#feColorMatrixElement
+    matrix.m_matrix[0][0] = 0.213f + 0.787f * amount;
+    matrix.m_matrix[0][1] = 0.715f - 0.715f * amount;
+    matrix.m_matrix[0][2] = 0.072f - 0.072f * amount;
+
+    matrix.m_matrix[1][0] = 0.213f - 0.213f * amount;
+    matrix.m_matrix[1][1] = 0.715f + 0.285f * amount;
+    matrix.m_matrix[1][2] = 0.072f - 0.072f * amount;
+
+    matrix.m_matrix[2][0] = 0.213f - 0.213f * amount;
+    matrix.m_matrix[2][1] = 0.715f - 0.715f * amount;
+    matrix.m_matrix[2][2] = 0.072f + 0.928f * amount;
+
+    return matrix;
+}
+
+ColorMatrix ColorMatrix::hueRotateMatrix(float angleInDegrees)
+{
+    float cosHue = cos(deg2rad(angleInDegrees));
+    float sinHue = sin(deg2rad(angleInDegrees));
+
+    ColorMatrix matrix;
+
+    // Values from https://www.w3.org/TR/filter-effects-1/#feColorMatrixElement
+    matrix.m_matrix[0][0] = 0.213f + cosHue * 0.787f - sinHue * 0.213f;
+    matrix.m_matrix[0][1] = 0.715f - cosHue * 0.715f - sinHue * 0.715f;
+    matrix.m_matrix[0][2] = 0.072f - cosHue * 0.072f + sinHue * 0.928f;
+
+    matrix.m_matrix[1][0] = 0.213f - cosHue * 0.213f + sinHue * 0.143f;
+    matrix.m_matrix[1][1] = 0.715f + cosHue * 0.285f + sinHue * 0.140f;
+    matrix.m_matrix[1][2] = 0.072f - cosHue * 0.072f - sinHue * 0.283f;
+
+    matrix.m_matrix[2][0] = 0.213f - cosHue * 0.213f - sinHue * 0.787f;
+    matrix.m_matrix[2][1] = 0.715f - cosHue * 0.715f + sinHue * 0.715f;
+    matrix.m_matrix[2][2] = 0.072f + cosHue * 0.928f + sinHue * 0.072f;
+
+    return matrix;
+}
+
+ColorMatrix ColorMatrix::sepiaMatrix(float amount)
+{
+    ColorMatrix matrix;
+
+    float oneMinusAmount = clampTo(1 - amount, 0.0, 1.0);
+
+    // Values from https://www.w3.org/TR/filter-effects-1/#sepiaEquivalent
+    matrix.m_matrix[0][0] = 0.393f + 0.607f * oneMinusAmount;
+    matrix.m_matrix[0][1] = 0.769f - 0.769f * oneMinusAmount;
+    matrix.m_matrix[0][2] = 0.189f - 0.189f * oneMinusAmount;
+
+    matrix.m_matrix[1][0] = 0.349f - 0.349f * oneMinusAmount;
+    matrix.m_matrix[1][1] = 0.686f + 0.314f * oneMinusAmount;
+    matrix.m_matrix[1][2] = 0.168f - 0.168f * oneMinusAmount;
+
+    matrix.m_matrix[2][0] = 0.272f - 0.272f * oneMinusAmount;
+    matrix.m_matrix[2][1] = 0.534f - 0.534f * oneMinusAmount;
+    matrix.m_matrix[2][2] = 0.131f + 0.869f * oneMinusAmount;
+
+    return matrix;
+}
+
+void ColorMatrix::transformColorComponents(FloatComponents& colorComponents) const
+{
+    auto [red, green, blue, alpha] = colorComponents;
+
+    colorComponents.components[0] = m_matrix[0][0] * red + m_matrix[0][1] * green + m_matrix[0][2] * blue + m_matrix[0][3] * alpha + m_matrix[0][4];
+    colorComponents.components[1] = m_matrix[1][0] * red + m_matrix[1][1] * green + m_matrix[1][2] * blue + m_matrix[1][3] * alpha + m_matrix[1][4];
+    colorComponents.components[2] = m_matrix[2][0] * red + m_matrix[2][1] * green + m_matrix[2][2] * blue + m_matrix[2][3] * alpha + m_matrix[2][4];
+    colorComponents.components[3] = m_matrix[3][0] * red + m_matrix[3][1] * green + m_matrix[3][2] * blue + m_matrix[3][3] * alpha + m_matrix[3][4];
+}
+
+FloatComponents ColorMatrix::transformedColorComponents(const FloatComponents& colorComponents) const
+{
+    auto [red, green, blue, alpha] = colorComponents;
+
+    return {
+        m_matrix[0][0] * red + m_matrix[0][1] * green + m_matrix[0][2] * blue + m_matrix[0][3] * alpha + m_matrix[0][4],
+        m_matrix[1][0] * red + m_matrix[1][1] * green + m_matrix[1][2] * blue + m_matrix[1][3] * alpha + m_matrix[1][4],
+        m_matrix[2][0] * red + m_matrix[2][1] * green + m_matrix[2][2] * blue + m_matrix[2][3] * alpha + m_matrix[2][4],
+        m_matrix[3][0] * red + m_matrix[3][1] * green + m_matrix[3][2] * blue + m_matrix[3][3] * alpha + m_matrix[3][4]
+    };
+}
+
+} // namespace WebCore
diff --git a/Source/WebCore/platform/graphics/ColorMatrix.h b/Source/WebCore/platform/graphics/ColorMatrix.h
new file mode 100644 (file)
index 0000000..61adfe0
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 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 {
+
+struct FloatComponents;
+
+// FIXME: This should likely be replaced by a generic matrix class. Other than
+// the specific matrix constuctors, there is nothing color specific about this.
+// Futhermore, most these don't need to be 4x5 matrices with most only needing
+// a 3x3 matrix.
+class ColorMatrix {
+public:
+    static ColorMatrix grayscaleMatrix(float);
+    static ColorMatrix saturationMatrix(float);
+    static ColorMatrix hueRotateMatrix(float angleInDegrees);
+    static ColorMatrix sepiaMatrix(float);
+
+    ColorMatrix();
+    ColorMatrix(const float[20]);
+    
+    void transformColorComponents(FloatComponents&) const;
+    FloatComponents transformedColorComponents(const FloatComponents&) const;
+
+private:
+    void makeIdentity();
+
+    float m_matrix[4][5];
+};
+
+} // namespace WebCore
index c63273a..fdb44d1 100644 (file)
@@ -26,6 +26,7 @@
 #include "config.h"
 #include "ColorUtilities.h"
 
+#include "ColorMatrix.h"
 #include <wtf/MathExtras.h>
 
 namespace WebCore {
@@ -294,160 +295,4 @@ FloatComponents premultiplied(const FloatComponents& sRGBComponents)
     };
 }
 
-ColorMatrix::ColorMatrix()
-{
-    makeIdentity();
-}
-
-ColorMatrix::ColorMatrix(const float values[20])
-{
-    m_matrix[0][0] = values[0];
-    m_matrix[0][1] = values[1];
-    m_matrix[0][2] = values[2];
-    m_matrix[0][3] = values[3];
-    m_matrix[0][4] = values[4];
-
-    m_matrix[1][0] = values[5];
-    m_matrix[1][1] = values[6];
-    m_matrix[1][2] = values[7];
-    m_matrix[1][3] = values[8];
-    m_matrix[1][4] = values[9];
-
-    m_matrix[2][0] = values[10];
-    m_matrix[2][1] = values[11];
-    m_matrix[2][2] = values[12];
-    m_matrix[2][3] = values[13];
-    m_matrix[2][4] = values[14];
-
-    m_matrix[3][0] = values[15];
-    m_matrix[3][1] = values[16];
-    m_matrix[3][2] = values[17];
-    m_matrix[3][3] = values[18];
-    m_matrix[3][4] = values[19];
-}
-
-void ColorMatrix::makeIdentity()
-{
-    memset(m_matrix, 0, sizeof(m_matrix));
-    m_matrix[0][0] = 1;
-    m_matrix[1][1] = 1;
-    m_matrix[2][2] = 1;
-    m_matrix[3][3] = 1;
-}
-
-ColorMatrix ColorMatrix::grayscaleMatrix(float amount)
-{
-    ColorMatrix matrix;
-
-    float oneMinusAmount = clampTo(1 - amount, 0.0, 1.0);
-
-    // Values from https://www.w3.org/TR/filter-effects-1/#grayscaleEquivalent
-    matrix.m_matrix[0][0] = 0.2126f + 0.7874f * oneMinusAmount;
-    matrix.m_matrix[0][1] = 0.7152f - 0.7152f * oneMinusAmount;
-    matrix.m_matrix[0][2] = 0.0722f - 0.0722f * oneMinusAmount;
-
-    matrix.m_matrix[1][0] = 0.2126f - 0.2126f * oneMinusAmount;
-    matrix.m_matrix[1][1] = 0.7152f + 0.2848f * oneMinusAmount;
-    matrix.m_matrix[1][2] = 0.0722f - 0.0722f * oneMinusAmount;
-
-    matrix.m_matrix[2][0] = 0.2126f - 0.2126f * oneMinusAmount;
-    matrix.m_matrix[2][1] = 0.7152f - 0.7152f * oneMinusAmount;
-    matrix.m_matrix[2][2] = 0.0722f + 0.9278f * oneMinusAmount;
-    
-    return matrix;
-}
-
-ColorMatrix ColorMatrix::saturationMatrix(float amount)
-{
-    ColorMatrix matrix;
-
-    // Values from https://www.w3.org/TR/filter-effects-1/#feColorMatrixElement
-    matrix.m_matrix[0][0] = 0.213f + 0.787f * amount;
-    matrix.m_matrix[0][1] = 0.715f - 0.715f * amount;
-    matrix.m_matrix[0][2] = 0.072f - 0.072f * amount;
-
-    matrix.m_matrix[1][0] = 0.213f - 0.213f * amount;
-    matrix.m_matrix[1][1] = 0.715f + 0.285f * amount;
-    matrix.m_matrix[1][2] = 0.072f - 0.072f * amount;
-
-    matrix.m_matrix[2][0] = 0.213f - 0.213f * amount;
-    matrix.m_matrix[2][1] = 0.715f - 0.715f * amount;
-    matrix.m_matrix[2][2] = 0.072f + 0.928f * amount;
-
-    return matrix;
-}
-
-ColorMatrix ColorMatrix::hueRotateMatrix(float angleInDegrees)
-{
-    float cosHue = cos(deg2rad(angleInDegrees));
-    float sinHue = sin(deg2rad(angleInDegrees));
-
-    ColorMatrix matrix;
-
-    // Values from https://www.w3.org/TR/filter-effects-1/#feColorMatrixElement
-    matrix.m_matrix[0][0] = 0.213f + cosHue * 0.787f - sinHue * 0.213f;
-    matrix.m_matrix[0][1] = 0.715f - cosHue * 0.715f - sinHue * 0.715f;
-    matrix.m_matrix[0][2] = 0.072f - cosHue * 0.072f + sinHue * 0.928f;
-
-    matrix.m_matrix[1][0] = 0.213f - cosHue * 0.213f + sinHue * 0.143f;
-    matrix.m_matrix[1][1] = 0.715f + cosHue * 0.285f + sinHue * 0.140f;
-    matrix.m_matrix[1][2] = 0.072f - cosHue * 0.072f - sinHue * 0.283f;
-
-    matrix.m_matrix[2][0] = 0.213f - cosHue * 0.213f - sinHue * 0.787f;
-    matrix.m_matrix[2][1] = 0.715f - cosHue * 0.715f + sinHue * 0.715f;
-    matrix.m_matrix[2][2] = 0.072f + cosHue * 0.928f + sinHue * 0.072f;
-
-    return matrix;
-}
-
-ColorMatrix ColorMatrix::sepiaMatrix(float amount)
-{
-    ColorMatrix matrix;
-
-    float oneMinusAmount = clampTo(1 - amount, 0.0, 1.0);
-
-    // Values from https://www.w3.org/TR/filter-effects-1/#sepiaEquivalent
-    matrix.m_matrix[0][0] = 0.393f + 0.607f * oneMinusAmount;
-    matrix.m_matrix[0][1] = 0.769f - 0.769f * oneMinusAmount;
-    matrix.m_matrix[0][2] = 0.189f - 0.189f * oneMinusAmount;
-
-    matrix.m_matrix[1][0] = 0.349f - 0.349f * oneMinusAmount;
-    matrix.m_matrix[1][1] = 0.686f + 0.314f * oneMinusAmount;
-    matrix.m_matrix[1][2] = 0.168f - 0.168f * oneMinusAmount;
-
-    matrix.m_matrix[2][0] = 0.272f - 0.272f * oneMinusAmount;
-    matrix.m_matrix[2][1] = 0.534f - 0.534f * oneMinusAmount;
-    matrix.m_matrix[2][2] = 0.131f + 0.869f * oneMinusAmount;
-
-    return matrix;
-}
-
-void ColorMatrix::transformColorComponents(FloatComponents& colorComponents) const
-{
-    float red = colorComponents.components[0];
-    float green = colorComponents.components[1];
-    float blue = colorComponents.components[2];
-    float alpha = colorComponents.components[3];
-
-    colorComponents.components[0] = m_matrix[0][0] * red + m_matrix[0][1] * green + m_matrix[0][2] * blue + m_matrix[0][3] * alpha + m_matrix[0][4];
-    colorComponents.components[1] = m_matrix[1][0] * red + m_matrix[1][1] * green + m_matrix[1][2] * blue + m_matrix[1][3] * alpha + m_matrix[1][4];
-    colorComponents.components[2] = m_matrix[2][0] * red + m_matrix[2][1] * green + m_matrix[2][2] * blue + m_matrix[2][3] * alpha + m_matrix[2][4];
-    colorComponents.components[3] = m_matrix[3][0] * red + m_matrix[3][1] * green + m_matrix[3][2] * blue + m_matrix[3][3] * alpha + m_matrix[3][4];
-}
-
-FloatComponents ColorMatrix::transformedColorComponents(const FloatComponents& colorComponents) const
-{
-    float red = colorComponents.components[0];
-    float green = colorComponents.components[1];
-    float blue = colorComponents.components[2];
-    float alpha = colorComponents.components[3];
-
-    return {
-        m_matrix[0][0] * red + m_matrix[0][1] * green + m_matrix[0][2] * blue + m_matrix[0][3] * alpha + m_matrix[0][4],
-        m_matrix[1][0] * red + m_matrix[1][1] * green + m_matrix[1][2] * blue + m_matrix[1][3] * alpha + m_matrix[1][4],
-        m_matrix[2][0] * red + m_matrix[2][1] * green + m_matrix[2][2] * blue + m_matrix[2][3] * alpha + m_matrix[2][4],
-        m_matrix[3][0] * red + m_matrix[3][1] * green + m_matrix[3][2] * blue + m_matrix[3][3] * alpha + m_matrix[3][4]
-    };
-}
-
 } // namespace WebCore
index ee11573..4d1eeb0 100644 (file)
@@ -193,25 +193,6 @@ float contrastRatio(const FloatComponents&, const FloatComponents&);
 
 FloatComponents premultiplied(const FloatComponents& sRGBCompontents);
 
-class ColorMatrix {
-public:
-    static ColorMatrix grayscaleMatrix(float);
-    static ColorMatrix saturationMatrix(float);
-    static ColorMatrix hueRotateMatrix(float angleInDegrees);
-    static ColorMatrix sepiaMatrix(float);
-
-    ColorMatrix();
-    ColorMatrix(const float[20]);
-    
-    void transformColorComponents(FloatComponents&) const;
-    FloatComponents transformedColorComponents(const FloatComponents&) const;
-
-private:
-    void makeIdentity();
-
-    float m_matrix[4][5];
-};
-
 } // namespace WebCore
 
 namespace std {
index 2963bf5..cdd6f6b 100644 (file)
@@ -29,6 +29,7 @@
 #include "AnimationUtilities.h"
 #include "CachedResourceLoader.h"
 #include "CachedSVGDocumentReference.h"
+#include "ColorMatrix.h"
 #include "ColorUtilities.h"
 #include "FilterEffect.h"
 #include "SVGURIReference.h"