Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / gm / mixercolorfilter.cpp
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 "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorFilter.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTileMode.h"
23 #include "include/core/SkTypes.h"
24 #include "include/effects/SkGradientShader.h"
25 #include "include/effects/SkLumaColorFilter.h"
26 #include "tools/Resources.h"
27
28 #include <math.h>
29
30 // A tint filter maps colors to a given range (gradient), based on the input luminance:
31 //
32 //   c' = lerp(lo, hi, luma(c))
33 //
34 // TODO: move to public headers/API?
35 //
36 static sk_sp<SkColorFilter> MakeTintColorFilter(SkColor lo, SkColor hi) {
37     const auto r_lo = SkColorGetR(lo),
38     g_lo = SkColorGetG(lo),
39     b_lo = SkColorGetB(lo),
40     a_lo = SkColorGetA(lo),
41     r_hi = SkColorGetR(hi),
42     g_hi = SkColorGetG(hi),
43     b_hi = SkColorGetB(hi),
44     a_hi = SkColorGetA(hi);
45
46     // We map component-wise:
47     //
48     //   r' = lo.r + (hi.r - lo.r) * luma
49     //   g' = lo.g + (hi.g - lo.g) * luma
50     //   b' = lo.b + (hi.b - lo.b) * luma
51     //   a' = lo.a + (hi.a - lo.a) * luma
52     //
53     // The input luminance is stored in the alpha channel
54     // (and RGB are cleared -- see SkLumaColorFilter). Thus:
55     const float tint_matrix[] = {
56         0, 0, 0, (r_hi - r_lo) / 255.0f, SkIntToScalar(r_lo) / 255.0f,
57         0, 0, 0, (g_hi - g_lo) / 255.0f, SkIntToScalar(g_lo) / 255.0f,
58         0, 0, 0, (b_hi - b_lo) / 255.0f, SkIntToScalar(b_lo) / 255.0f,
59         0, 0, 0, (a_hi - a_lo) / 255.0f, SkIntToScalar(a_lo) / 255.0f,
60     };
61
62     return SkColorFilters::Matrix(tint_matrix)->makeComposed(SkLumaColorFilter::Make());
63 }
64
65 namespace {
66
67 class MixerCFGM final : public skiagm::GM {
68 public:
69     MixerCFGM(const SkSize& tileSize, size_t tileCount)
70         : fTileSize(tileSize)
71         , fTileCount(tileCount) {}
72
73 protected:
74     SkString onShortName() override {
75         return SkString("mixerCF");
76     }
77
78     SkISize onISize() override {
79         return SkISize::Make(fTileSize.width()  * 1.2f * fTileCount,
80                              fTileSize.height() * 1.2f * 3);         // 3 rows
81     }
82
83     void onDraw(SkCanvas* canvas) override {
84         SkPaint paint;
85
86         const SkColor gradient_colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorRED };
87         paint.setShader(SkGradientShader::MakeSweep(fTileSize.width()  / 2,
88                                                     fTileSize.height() / 2,
89                                                     gradient_colors, nullptr,
90                                                     SK_ARRAY_COUNT(gradient_colors)));
91
92         auto cf0 = MakeTintColorFilter(0xff300000, 0xffa00000);  // red tint
93         auto cf1 = MakeTintColorFilter(0xff003000, 0xff00a000);  // green tint
94
95         this->mixRow(canvas, paint, nullptr,     cf1);
96         this->mixRow(canvas, paint,     cf0, nullptr);
97         this->mixRow(canvas, paint,     cf0,     cf1);
98     }
99
100 private:
101     const SkSize fTileSize;
102     const size_t fTileCount;
103
104     void mixRow(SkCanvas* canvas, SkPaint& paint,
105                 sk_sp<SkColorFilter> cf0, sk_sp<SkColorFilter> cf1) {
106         // We cycle through paint colors on each row, to test how the paint color flows through
107         // the color-filter network
108         const SkColor4f paintColors[] = {
109             { 1.0f, 1.0f, 1.0f, 1.0f },  // Opaque white
110             { 1.0f, 1.0f, 1.0f, 0.5f },  // Translucent white
111             { 0.5f, 0.5f, 1.0f, 1.0f },  // Opaque pale blue
112             { 0.5f, 0.5f, 1.0f, 0.5f },  // Translucent pale blue
113         };
114
115         canvas->translate(0, fTileSize.height() * 0.1f);
116         {
117             SkAutoCanvasRestore arc(canvas, true);
118             for (size_t i = 0; i < fTileCount; ++i) {
119                 paint.setColor4f(paintColors[i % SK_ARRAY_COUNT(paintColors)]);
120                 float t = static_cast<float>(i) / (fTileCount - 1);
121                 paint.setColorFilter(SkColorFilters::Lerp(t, cf0, cf1));
122                 canvas->translate(fTileSize.width() * 0.1f, 0);
123                 canvas->drawRect(SkRect::MakeWH(fTileSize.width(), fTileSize.height()), paint);
124                 canvas->translate(fTileSize.width() * 1.1f, 0);
125             }
126         }
127         canvas->translate(0, fTileSize.height() * 1.1f);
128     }
129
130     using INHERITED = skiagm::GM;
131 };
132
133 } // namespace
134
135 DEF_GM( return new MixerCFGM(SkSize::Make(200, 250), 5); )