1 /* 2 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann (at) kde.org> 3 * Copyright (C) 2004, 2005 Rob Buis <buis (at) kde.org> 4 * Copyright (C) 2005 Eric Seidel <eric (at) webkit.org> 5 * Copyright (C) 2013 Google Inc. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Library General Public 9 * License as published by the Free Software Foundation; either 10 * version 2 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Library General Public License for more details. 16 * 17 * You should have received a copy of the GNU Library General Public License 18 * along with this library; see the file COPYING.LIB. If not, write to 19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 20 * Boston, MA 02110-1301, USA. 21 */ 22 23 #ifndef FEColorMatrix_h 24 #define FEColorMatrix_h 25 26 #include "core/platform/graphics/filters/FilterEffect.h" 27 28 #include "core/platform/graphics/filters/Filter.h" 29 #include "wtf/Vector.h" 30 31 namespace WebCore { 32 33 enum ColorMatrixType { 34 FECOLORMATRIX_TYPE_UNKNOWN = 0, 35 FECOLORMATRIX_TYPE_MATRIX = 1, 36 FECOLORMATRIX_TYPE_SATURATE = 2, 37 FECOLORMATRIX_TYPE_HUEROTATE = 3, 38 FECOLORMATRIX_TYPE_LUMINANCETOALPHA = 4 39 }; 40 41 class FEColorMatrix : public FilterEffect { 42 public: 43 static PassRefPtr<FEColorMatrix> create(Filter*, ColorMatrixType, const Vector<float>&); 44 45 ColorMatrixType type() const; 46 bool setType(ColorMatrixType); 47 48 const Vector<float>& values() const; 49 bool setValues(const Vector<float>&); 50 51 virtual PassRefPtr<SkImageFilter> createImageFilter(SkiaImageFilterBuilder*) OVERRIDE; 52 53 virtual TextStream& externalRepresentation(TextStream&, int indention) const; 54 55 static inline void calculateSaturateComponents(float* components, float value); 56 static inline void calculateHueRotateComponents(float* components, float value); 57 58 private: 59 FEColorMatrix(Filter*, ColorMatrixType, const Vector<float>&); 60 61 virtual void applySoftware() OVERRIDE; 62 virtual bool applySkia() OVERRIDE; 63 64 ColorMatrixType m_type; 65 Vector<float> m_values; 66 }; 67 68 inline void FEColorMatrix::calculateSaturateComponents(float* components, float value) 69 { 70 components[0] = (0.213 + 0.787 * value); 71 components[1] = (0.715 - 0.715 * value); 72 components[2] = (0.072 - 0.072 * value); 73 components[3] = (0.213 - 0.213 * value); 74 components[4] = (0.715 + 0.285 * value); 75 components[5] = (0.072 - 0.072 * value); 76 components[6] = (0.213 - 0.213 * value); 77 components[7] = (0.715 - 0.715 * value); 78 components[8] = (0.072 + 0.928 * value); 79 } 80 81 inline void FEColorMatrix::calculateHueRotateComponents(float* components, float value) 82 { 83 float cosHue = cos(value * piFloat / 180); 84 float sinHue = sin(value * piFloat / 180); 85 components[0] = 0.213 + cosHue * 0.787 - sinHue * 0.213; 86 components[1] = 0.715 - cosHue * 0.715 - sinHue * 0.715; 87 components[2] = 0.072 - cosHue * 0.072 + sinHue * 0.928; 88 components[3] = 0.213 - cosHue * 0.213 + sinHue * 0.143; 89 components[4] = 0.715 + cosHue * 0.285 + sinHue * 0.140; 90 components[5] = 0.072 - cosHue * 0.072 - sinHue * 0.283; 91 components[6] = 0.213 - cosHue * 0.213 - sinHue * 0.787; 92 components[7] = 0.715 - cosHue * 0.715 + sinHue * 0.715; 93 components[8] = 0.072 + cosHue * 0.928 + sinHue * 0.072; 94 } 95 96 97 } // namespace WebCore 98 99 #endif // FEColorMatrix_h 100