Flutter Engine
The Flutter Engine
CCTonerEffect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 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
18#include "src/base/SkVx.h"
20
21#include <cstddef>
22#include <utility>
23#include <vector>
24
25namespace skjson {
26class ArrayValue;
27}
28
29namespace skottie::internal {
30
31namespace {
32
33class CCTonerAdapter final : public DiscardableAdapterBase<CCTonerAdapter,
34 sksg::GradientColorFilter> {
35 public:
36 CCTonerAdapter(const skjson::ArrayValue& jprops,
38 const AnimationBuilder& abuilder,
39 std::vector<sk_sp<sksg::Color>> colorNodes)
40 : INHERITED(sksg::GradientColorFilter::Make(std::move(layer), colorNodes))
41 , fColorNodes(std::move(colorNodes))
42 {
43 enum : size_t {
44 kTone_Index = 0,
45 kHiColor_Index = 1,
46 kBrightColor_Index = 2,
47 kMidColor_Index = 3,
48 kDarkColor_Index = 4,
49 kShadowColor_Index = 5,
50 kBlendAmount_Index = 6,
51 };
52
53
54 EffectBinder(jprops, abuilder, this)
55 .bind( kTone_Index, fTone)
56 .bind( kHiColor_Index, fHighlights)
57 .bind(kBrightColor_Index, fBrights)
58 .bind( kMidColor_Index, fMidtones)
59 .bind( kDarkColor_Index, fDarktones)
60 .bind(kShadowColor_Index, fShadows);
61
62 }
63 private:
64 static SkColor lerpColor(SkColor c0, SkColor c1, float t) {
65 const auto c0_4f = Sk4f_fromL32(c0),
66 c1_4f = Sk4f_fromL32(c1),
67 c_4f = c0_4f + (c1_4f - c0_4f) * t;
68
69 return Sk4f_toL32(c_4f);
70 }
71
72 void onSync() override {
73 switch (SkScalarRoundToInt(fTone)) {
74 // duotone
75 case 1: fColorNodes.at(0)->setColor(fShadows);
76 fColorNodes.at(1)->setColor(lerpColor(fShadows, fHighlights, 0.25));
77 fColorNodes.at(2)->setColor(lerpColor(fShadows, fHighlights, 0.5));
78 fColorNodes.at(3)->setColor(lerpColor(fShadows, fHighlights, 0.75));
79 fColorNodes.at(4)->setColor(fHighlights);
80 break;
81 // tritone
82 case 2: fColorNodes.at(0)->setColor(fShadows);
83 fColorNodes.at(1)->setColor(lerpColor(fShadows, fMidtones, 0.5));
84 fColorNodes.at(2)->setColor(fMidtones);
85 fColorNodes.at(3)->setColor(lerpColor(fMidtones, fHighlights, 0.5));
86 fColorNodes.at(4)->setColor(fHighlights);
87 break;
88 // pentone
89 case 3: fColorNodes.at(0)->setColor(fShadows);
90 fColorNodes.at(1)->setColor(fDarktones);
91 fColorNodes.at(2)->setColor(fMidtones);
92 fColorNodes.at(3)->setColor(fBrights);
93 fColorNodes.at(4)->setColor(fHighlights);
94 break;
95 // solid
96 default: fColorNodes.at(0)->setColor(fMidtones);
97 fColorNodes.at(1)->setColor(fMidtones);
98 fColorNodes.at(2)->setColor(fMidtones);
99 fColorNodes.at(3)->setColor(fMidtones);
100 fColorNodes.at(4)->setColor(fMidtones);
101 break;
102 }
103
104 this->node()->setWeight((100-fBlend)/100);
105 }
106
107
108 const std::vector<sk_sp<sksg::Color>> fColorNodes;
109
110 ScalarValue fTone = 0;
111 ColorValue fHighlights,
112 fBrights,
113 fMidtones,
114 fDarktones,
115 fShadows;
116 ScalarValue fBlend = 0;
117
118 using INHERITED = DiscardableAdapterBase<CCTonerAdapter, sksg::GradientColorFilter>;
119};
120
121} // namespace
122
123sk_sp<sksg::RenderNode> EffectBuilder::attachCCTonerEffect(const skjson::ArrayValue& jprops,
124 sk_sp<sksg::RenderNode> layer) const {
125 std::vector<sk_sp<sksg::Color>> colorNodes = {
131 };
132 return fBuilder->attachDiscardableAdapter<CCTonerAdapter>(jprops,
133 std::move(layer),
134 *fBuilder,
135 std::move(colorNodes));
136}
137
138} // namespace skottie::internal
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
#define SkScalarRoundToInt(x)
Definition: SkScalar.h:37
static skvx::float4 Sk4f_fromL32(uint32_t px)
Definition: SkSwizzlePriv.h:49
static uint32_t Sk4f_toL32(const skvx::float4 &px)
Definition: SkSwizzlePriv.h:53
void attachDiscardableAdapter(sk_sp< T > adapter) const
Definition: SkottiePriv.h:139
static sk_sp< Color > Make(SkColor c)
Definition: SkSGPaint.cpp:44
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
SkScalar ScalarValue
Definition: SkottieValue.h:22
Definition: Skottie.h:32
Definition: ref_ptr.h:256