Flutter Engine
The Flutter Engine
InvertEffect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
17
18#include <array>
19#include <cstddef>
20#include <cstdint>
21#include <utility>
22
23namespace skjson {
24class ArrayValue;
25}
26
27namespace skottie {
28namespace internal {
29
30namespace {
31
32class InvertEffectAdapter final : public AnimatablePropertyContainer {
33public:
36 const AnimationBuilder* abuilder) {
38 new InvertEffectAdapter(jprops, std::move(layer), abuilder));
39 }
40
41 const sk_sp<sksg::ExternalColorFilter>& node() const { return fColorFilter; }
42
43private:
44 InvertEffectAdapter(const skjson::ArrayValue& jprops,
46 const AnimationBuilder* abuilder)
47 : fColorFilter(sksg::ExternalColorFilter::Make(std::move(layer))) {
48 enum : size_t {
49 kChannel_Index = 0,
50 };
51
52 EffectBinder(jprops, *abuilder, this).bind(kChannel_Index, fChannel);
53 }
54
55 void onSync() override {
56 enum class CS { kRGB, kHSL, kYIQ };
57
58 struct STColorMatrix {
59 std::array<float,4> scale,
60 trans;
61 CS cs;
62 };
63
64 const auto stcm = [this]() -> STColorMatrix {
65 // https://helpx.adobe.com/after-effects/using/channel-effects.html#invert_effect
66 enum : uint8_t {
67 kRGB_Channel = 1,
68 kR_Channel = 2,
69 kG_Channel = 3,
70 kB_Channel = 4,
71
72 // NB: HLS vs. HSL
73 kHLS_Channel = 6,
74 kH_Channel = 7,
75 kL_Channel = 8,
76 kS_Channel = 9,
77
78 kYIQ_Channel = 11,
79 kY_Channel = 12,
80 kI_Channel = 13,
81 kQ_Channel = 14,
82
83 kA_Channel = 16,
84 };
85
86 switch (static_cast<uint8_t>(fChannel)) {
87 case kR_Channel: return { {-1, 1, 1, 1}, { 1,0,0,0}, CS::kRGB }; // r' = 1 - r
88 case kG_Channel: return { { 1,-1, 1, 1}, { 0,1,0,0}, CS::kRGB }; // g' = 1 - g
89 case kB_Channel: return { { 1, 1,-1, 1}, { 0,0,1,0}, CS::kRGB }; // b' = 1 - b
90 case kA_Channel: return { { 1, 1, 1,-1}, { 0,0,0,1}, CS::kRGB }; // a' = 1 - a
91 case kRGB_Channel: return { {-1,-1,-1, 1}, { 1,1,1,0}, CS::kRGB };
92
93 case kH_Channel: return { {-1, 1, 1, 1}, {.5f,0,0,0}, CS::kHSL }; // h' = .5 - h
94 case kS_Channel: return { { 1,-1, 1, 1}, { 0,1,0,0}, CS::kHSL }; // s' = 1 - s
95 case kL_Channel: return { { 1, 1,-1, 1}, { 0,0,1,0}, CS::kHSL }; // l' = 1 - l
96 case kHLS_Channel: return { {-1,-1,-1, 1}, {.5f,1,1,0}, CS::kHSL };
97
98 case kY_Channel: return { {-1, 1, 1, 1}, { 1,0,0,0}, CS::kYIQ }; // y' = 1 - y
99 case kI_Channel: return { { 1,-1, 1, 1}, { 0,0,0,0}, CS::kYIQ }; // i' = -i
100 case kQ_Channel: return { { 1, 1,-1, 1}, { 0,0,0,0}, CS::kYIQ }; // q' = -q
101 case kYIQ_Channel: return { {-1,-1,-1, 1}, { 1,0,0,0}, CS::kYIQ };
102
103 default: return { { 1, 1, 1, 1}, { 0,0,0,0}, CS::kRGB };
104 }
105
107 }();
108
110 stcm.scale[0], 0, 0, 0, stcm.trans[0],
111 0, stcm.scale[1], 0, 0, stcm.trans[1],
112 0, 0, stcm.scale[2], 0, stcm.trans[2],
113 0, 0, 0, stcm.scale[3], stcm.trans[3]
114
115 );
116
117 if (stcm.cs == CS::kYIQ) {
118 // https://en.wikipedia.org/wiki/YIQ
119 static constexpr SkColorMatrix RGB2YIQ(
120 0.2990f, 0.5870f, 0.1140f, 0, 0,
121 0.5959f, -0.2746f, -0.3213f, 0, 0,
122 0.2115f, -0.5227f, 0.3112f, 0, 0,
123 0, 0, 0, 1, 0
124 );
125 static constexpr SkColorMatrix YIQ2RGB(
126 1, 0.9560f, 0.6190f, 0, 0,
127 1, -0.2720f, -0.6470f, 0, 0,
128 1, -1.1060f, 1.7030f, 0, 0,
129 0, 0, 0, 1, 0
130 );
131
132 m.preConcat (RGB2YIQ);
133 m.postConcat(YIQ2RGB);
134 }
135
136 fColorFilter->setColorFilter(stcm.cs == CS::kHSL ? SkColorFilters::HSLAMatrix(m)
138 }
139
140 const sk_sp<sksg::ExternalColorFilter> fColorFilter;
141
142 float fChannel = 0;
143};
144
145} // namespace
146
147sk_sp<sksg::RenderNode> EffectBuilder::attachInvertEffect(const skjson::ArrayValue& jprops,
148 sk_sp<sksg::RenderNode> layer) const {
149 return fBuilder->attachDiscardableAdapter<InvertEffectAdapter>(jprops,
150 std::move(layer),
151 fBuilder);
152}
153
154} // namespace internal
155} // namespace skottie
#define SkUNREACHABLE
Definition: SkAssert.h:135
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
static sk_sp< SkColorFilter > HSLAMatrix(const SkColorMatrix &)
void attachDiscardableAdapter(sk_sp< T > adapter) const
Definition: SkottiePriv.h:139
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
Definition: Skottie.h:32
Definition: ref_ptr.h:256
const Scalar scale