• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
attachTritoneEffect(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer) const18 sk_sp<sksg::RenderNode> EffectBuilder::attachTritoneEffect(const skjson::ArrayValue& jprops,
19                                                            sk_sp<sksg::RenderNode> layer) const {
20     enum : size_t {
21         kHiColor_Index     = 0,
22         kMiColor_Index     = 1,
23         kLoColor_Index     = 2,
24         kBlendAmount_Index = 3,
25 
26         kMax_Index         = kBlendAmount_Index,
27     };
28 
29     if (jprops.size() <= kMax_Index) {
30         return nullptr;
31     }
32 
33     const skjson::ObjectValue* hicolor_prop = jprops[    kHiColor_Index];
34     const skjson::ObjectValue* micolor_prop = jprops[    kMiColor_Index];
35     const skjson::ObjectValue* locolor_prop = jprops[    kLoColor_Index];
36     const skjson::ObjectValue*   blend_prop = jprops[kBlendAmount_Index];
37 
38     if (!hicolor_prop || !micolor_prop || !locolor_prop || !blend_prop) {
39         return nullptr;
40     }
41 
42     auto tritone_node =
43             sksg::GradientColorFilter::Make(std::move(layer), {
44                                             fBuilder->attachColor(*locolor_prop, "v"),
45                                             fBuilder->attachColor(*micolor_prop, "v"),
46                                             fBuilder->attachColor(*hicolor_prop, "v") });
47     if (!tritone_node) {
48         return nullptr;
49     }
50 
51     fBuilder->bindProperty<ScalarValue>((*blend_prop)["v"],
52         [tritone_node](const ScalarValue& w) {
53             tritone_node->setWeight((100 - w) / 100); // 100-based, inverted (!?).
54         });
55 
56     return tritone_node;
57 }
58 
59 } // namespace internal
60 } // namespace skottie
61