|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +part of engine; |
| 6 | + |
| 7 | +/// A description of a color filter to apply when drawing a shape or compositing |
| 8 | +/// a layer with a particular [Paint]. A color filter is a function that takes |
| 9 | +/// two colors, and outputs one color. When applied during compositing, it is |
| 10 | +/// independently applied to each pixel of the layer being drawn before the |
| 11 | +/// entire layer is merged with the destination. |
| 12 | +/// |
| 13 | +/// Instances of this class are used with [Paint.colorFilter] on [Paint] |
| 14 | +/// objects. |
| 15 | +class EngineColorFilter implements ui.ColorFilter { |
| 16 | + /// Creates a color filter that applies the blend mode given as the second |
| 17 | + /// argument. The source color is the one given as the first argument, and the |
| 18 | + /// destination color is the one from the layer being composited. |
| 19 | + /// |
| 20 | + /// The output of this filter is then composited into the background according |
| 21 | + /// to the [Paint.blendMode], using the output of this filter as the source |
| 22 | + /// and the background as the destination. |
| 23 | + const EngineColorFilter.mode(ui.Color color, ui.BlendMode blendMode) |
| 24 | + : _color = color, |
| 25 | + _blendMode = blendMode, |
| 26 | + _matrix = null, |
| 27 | + _type = _TypeMode; |
| 28 | + |
| 29 | + /// Construct a color filter that transforms a color by a 5x5 matrix, where |
| 30 | + /// the fifth row is implicitly added in an identity configuration. |
| 31 | + /// |
| 32 | + /// Every pixel's color value, repsented as an `[R, G, B, A]`, is matrix |
| 33 | + /// multiplied to create a new color: |
| 34 | + /// |
| 35 | + /// ```text |
| 36 | + /// | R' | | a00 a01 a02 a03 a04 | | R | |
| 37 | + /// | G' | | a10 a11 a22 a33 a44 | | G | |
| 38 | + /// | B' | = | a20 a21 a22 a33 a44 | * | B | |
| 39 | + /// | A' | | a30 a31 a22 a33 a44 | | A | |
| 40 | + /// | 1 | | 0 0 0 0 1 | | 1 | |
| 41 | + /// ``` |
| 42 | + /// |
| 43 | + /// The matrix is in row-major order and the translation column is specified |
| 44 | + /// in unnormalized, 0...255, space. For example, the identity matrix is: |
| 45 | + /// |
| 46 | + /// ``` |
| 47 | + /// const ColorMatrix identity = ColorFilter.matrix(<double>[ |
| 48 | + /// 1, 0, 0, 0, 0, |
| 49 | + /// 0, 1, 0, 0, 0, |
| 50 | + /// 0, 0, 1, 0, 0, |
| 51 | + /// 0, 0, 0, 1, 0, |
| 52 | + /// ]); |
| 53 | + /// ``` |
| 54 | + /// |
| 55 | + /// ## Examples |
| 56 | + /// |
| 57 | + /// An inversion color matrix: |
| 58 | + /// |
| 59 | + /// ``` |
| 60 | + /// const ColorFilter invert = ColorFilter.matrix(<double>[ |
| 61 | + /// -1, 0, 0, 0, 255, |
| 62 | + /// 0, -1, 0, 0, 255, |
| 63 | + /// 0, 0, -1, 0, 255, |
| 64 | + /// 0, 0, 0, 1, 0, |
| 65 | + /// ]); |
| 66 | + /// ``` |
| 67 | + /// |
| 68 | + /// A sepia-toned color matrix (values based on the [Filter Effects Spec](https://www.w3.org/TR/filter-effects-1/#sepiaEquivalent)): |
| 69 | + /// |
| 70 | + /// ``` |
| 71 | + /// const ColorFilter sepia = ColorFilter.matrix(<double>[ |
| 72 | + /// 0.393, 0.769, 0.189, 0, 0, |
| 73 | + /// 0.349, 0.686, 0.168, 0, 0, |
| 74 | + /// 0.272, 0.534, 0.131, 0, 0, |
| 75 | + /// 0, 0, 0, 1, 0, |
| 76 | + /// ]); |
| 77 | + /// ``` |
| 78 | + /// |
| 79 | + /// A greyscale color filter (values based on the [Filter Effects Spec](https://www.w3.org/TR/filter-effects-1/#grayscaleEquivalent)): |
| 80 | + /// |
| 81 | + /// ``` |
| 82 | + /// const ColorFilter greyscale = ColorFilter.matrix(<double>[ |
| 83 | + /// 0.2126, 0.7152, 0.0722, 0, 0, |
| 84 | + /// 0.2126, 0.7152, 0.0722, 0, 0, |
| 85 | + /// 0.2126, 0.7152, 0.0722, 0, 0, |
| 86 | + /// 0, 0, 0, 1, 0, |
| 87 | + /// ]); |
| 88 | + /// ``` |
| 89 | + const EngineColorFilter.matrix(List<double> matrix) |
| 90 | + : _color = null, |
| 91 | + _blendMode = null, |
| 92 | + _matrix = matrix, |
| 93 | + _type = _TypeMatrix; |
| 94 | + |
| 95 | + /// Construct a color filter that applies the sRGB gamma curve to the RGB |
| 96 | + /// channels. |
| 97 | + const EngineColorFilter.linearToSrgbGamma() |
| 98 | + : _color = null, |
| 99 | + _blendMode = null, |
| 100 | + _matrix = null, |
| 101 | + _type = _TypeLinearToSrgbGamma; |
| 102 | + |
| 103 | + /// Creates a color filter that applies the inverse of the sRGB gamma curve |
| 104 | + /// to the RGB channels. |
| 105 | + const EngineColorFilter.srgbToLinearGamma() |
| 106 | + : _color = null, |
| 107 | + _blendMode = null, |
| 108 | + _matrix = null, |
| 109 | + _type = _TypeSrgbToLinearGamma; |
| 110 | + |
| 111 | + final ui.Color _color; |
| 112 | + final ui.BlendMode _blendMode; |
| 113 | + final List<double> _matrix; |
| 114 | + final int _type; |
| 115 | + |
| 116 | + // The type of SkColorFilter class to create for Skia. |
| 117 | + // These constants must be kept in sync with ColorFilterType in paint.cc. |
| 118 | + static const int _TypeNone = 0; // null |
| 119 | + static const int _TypeMode = 1; // MakeModeFilter |
| 120 | + static const int _TypeMatrix = 2; // MakeMatrixFilterRowMajor255 |
| 121 | + static const int _TypeLinearToSrgbGamma = 3; // MakeLinearToSRGBGamma |
| 122 | + static const int _TypeSrgbToLinearGamma = 4; // MakeSRGBToLinearGamma |
| 123 | + |
| 124 | + @override |
| 125 | + bool operator ==(dynamic other) { |
| 126 | + if (other is! EngineColorFilter) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + final EngineColorFilter typedOther = other; |
| 130 | + |
| 131 | + if (_type != typedOther._type) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + if (!_listEquals<double>(_matrix, typedOther._matrix)) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + return _color == typedOther._color && _blendMode == typedOther._blendMode; |
| 139 | + } |
| 140 | + |
| 141 | + SkColorFilter _toSkColorFilter() { |
| 142 | + switch (_type) { |
| 143 | + case _TypeMode: |
| 144 | + if (_color == null || _blendMode == null) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + return SkColorFilter.mode(this); |
| 148 | + case _TypeMatrix: |
| 149 | + if (_matrix == null) { |
| 150 | + return null; |
| 151 | + } |
| 152 | + assert(_matrix.length == 20, 'Color Matrix must have 20 entries.'); |
| 153 | + return SkColorFilter.matrix(this); |
| 154 | + case _TypeLinearToSrgbGamma: |
| 155 | + return SkColorFilter.linearToSrgbGamma(this); |
| 156 | + case _TypeSrgbToLinearGamma: |
| 157 | + return SkColorFilter.srgbToLinearGamma(this); |
| 158 | + default: |
| 159 | + throw StateError('Unknown mode $_type for ColorFilter.'); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @override |
| 164 | + int get hashCode => ui.hashValues(_color, _blendMode, ui.hashList(_matrix), _type); |
| 165 | + |
| 166 | + @override |
| 167 | + String toString() { |
| 168 | + switch (_type) { |
| 169 | + case _TypeMode: |
| 170 | + return 'ColorFilter.mode($_color, $_blendMode)'; |
| 171 | + case _TypeMatrix: |
| 172 | + return 'ColorFilter.matrix($_matrix)'; |
| 173 | + case _TypeLinearToSrgbGamma: |
| 174 | + return 'ColorFilter.linearToSrgbGamma()'; |
| 175 | + case _TypeSrgbToLinearGamma: |
| 176 | + return 'ColorFilter.srgbToLinearGamma()'; |
| 177 | + default: |
| 178 | + return 'Unknown ColorFilter type. This is an error. If you\'re seeing this, please file an issue at https://github.com/flutter/flutter/issues/new.'; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + List<dynamic> webOnlySerializeToCssPaint() { |
| 183 | + throw UnsupportedError('ColorFilter for CSS paint not yet supported'); |
| 184 | + } |
| 185 | +} |
0 commit comments