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
8 #include "modules/skottie/src/effects/Effects.h"
9
10 #include "modules/skottie/src/SkottieValue.h"
11 #include "modules/sksg/include/SkSGColorFilter.h"
12 #include "modules/sksg/include/SkSGPaint.h"
13 #include "src/utils/SkJSON.h"
14
15 namespace skottie {
16 namespace internal {
17
attachTintEffect(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer) const18 sk_sp<sksg::RenderNode> EffectBuilder::attachTintEffect(const skjson::ArrayValue& jprops,
19 sk_sp<sksg::RenderNode> layer) const {
20 enum : size_t {
21 kMapBlackTo_Index = 0,
22 kMapWhiteTo_Index = 1,
23 kAmount_Index = 2,
24 // kOpacity_Index = 3, // currently unused (not exported)
25
26 kMax_Index = kAmount_Index,
27 };
28
29 if (jprops.size() <= kMax_Index) {
30 return nullptr;
31 }
32
33 const skjson::ObjectValue* color0_prop = jprops[kMapBlackTo_Index];
34 const skjson::ObjectValue* color1_prop = jprops[kMapWhiteTo_Index];
35 const skjson::ObjectValue* amount_prop = jprops[ kAmount_Index];
36
37 if (!color0_prop || !color1_prop || !amount_prop) {
38 return nullptr;
39 }
40
41 auto tint_node =
42 sksg::GradientColorFilter::Make(std::move(layer),
43 fBuilder->attachColor(*color0_prop, "v"),
44 fBuilder->attachColor(*color1_prop, "v"));
45 if (!tint_node) {
46 return nullptr;
47 }
48
49 fBuilder->bindProperty<ScalarValue>((*amount_prop)["v"],
50 [tint_node](const ScalarValue& w) {
51 tint_node->setWeight(w / 100); // 100-based
52 });
53
54 return tint_node;
55 }
56
57 } // namespace internal
58 } // namespace skottie
59