Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
colorfilters.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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"
14#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
22#include "tools/DecodeUtils.h"
23#include "tools/Resources.h"
24
25#include <vector>
26#include <tuple>
27
28static sk_sp<SkShader> make_shader(const SkRect& bounds) {
29 const SkPoint pts[] = {
30 { bounds.left(), bounds.top() },
31 { bounds.right(), bounds.bottom() },
32 };
33 const SkColor colors[] = {
36 };
37 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
39}
40
41typedef void (*InstallPaint)(SkPaint*, uint32_t, uint32_t);
42
43static void install_nothing(SkPaint* paint, uint32_t, uint32_t) {
44 paint->setColorFilter(nullptr);
45}
46
47static void install_lighting(SkPaint* paint, uint32_t mul, uint32_t add) {
48 paint->setColorFilter(SkColorFilters::Lighting(mul, add));
49}
50
51class ColorFiltersGM : public skiagm::GM {
52 SkString getName() const override { return SkString("lightingcolorfilter"); }
53
54 SkISize getISize() override { return {620, 430}; }
55
56 void onDraw(SkCanvas* canvas) override {
57 SkRect r = {0, 0, 600, 50};
58
60 paint.setShader(make_shader(r));
61
62 const struct {
64 uint32_t fData0, fData1;
65 } rec[] = {
66 { install_nothing, 0, 0 },
67 { install_lighting, 0xFF0000, 0 },
68 { install_lighting, 0x00FF00, 0 },
69 { install_lighting, 0x0000FF, 0 },
70 { install_lighting, 0x000000, 0xFF0000 },
71 { install_lighting, 0x000000, 0x00FF00 },
72 { install_lighting, 0x000000, 0x0000FF },
73 };
74
75 canvas->translate(10, 10);
76 for (size_t i = 0; i < std::size(rec); ++i) {
77 rec[i].fProc(&paint, rec[i].fData0, rec[i].fData1);
78 canvas->drawRect(r, paint);
79 canvas->translate(0, r.height() + 10);
80 }
81 }
82};
83
84DEF_GM(return new ColorFiltersGM;)
85
86class HSLColorFilterGM : public skiagm::GM {
87protected:
88 SkString getName() const override { return SkString("hslcolorfilter"); }
89
90 SkISize getISize() override { return {840, 1100}; }
91
92 void onOnceBeforeDraw() override {
93 sk_sp<SkImage> mandrill = ToolUtils::GetResourceAsImage("images/mandrill_256.png");
94 const auto lm = SkMatrix::RectToRect(SkRect::MakeWH(mandrill->width(), mandrill->height()),
95 SkRect::MakeWH(kWheelSize, kWheelSize));
96 fShaders.push_back(mandrill->makeShader(SkSamplingOptions(), &lm));
97
98 static constexpr SkColor gGrads[][4] = {
99 { 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff0000 },
100 { 0xdfc08040, 0xdf8040c0, 0xdf40c080, 0xdfc08040 },
101 };
102
103 for (const auto& cols : gGrads) {
104 fShaders.push_back(SkGradientShader::MakeSweep(kWheelSize / 2, kWheelSize / 2,
105 cols, nullptr, std::size(cols),
106 SkTileMode::kRepeat, -90, 270, 0,
107 nullptr));
108 }
109 }
110
111 void onDraw(SkCanvas* canvas) override {
112 using std::make_tuple;
113
114 static constexpr struct {
115 std::tuple<float, float> h, s, l;
116 } gTests[] = {
117 { make_tuple(-0.5f, 0.5f), make_tuple( 0.0f, 0.0f), make_tuple( 0.0f, 0.0f) },
118 { make_tuple( 0.0f, 0.0f), make_tuple(-1.0f, 1.0f), make_tuple( 0.0f, 0.0f) },
119 { make_tuple( 0.0f, 0.0f), make_tuple( 0.0f, 0.0f), make_tuple(-1.0f, 1.0f) },
120 };
121
122 const auto rect = SkRect::MakeWH(kWheelSize, kWheelSize);
123
124 canvas->drawColor(0xffcccccc);
126
127 for (const auto& shader : fShaders) {
128 paint.setShader(shader);
129
130 for (const auto& tst: gTests) {
131 canvas->translate(0, kWheelSize * 0.1f);
132
133 const auto dh = (std::get<1>(tst.h) - std::get<0>(tst.h)) / (kSteps - 1),
134 ds = (std::get<1>(tst.s) - std::get<0>(tst.s)) / (kSteps - 1),
135 dl = (std::get<1>(tst.l) - std::get<0>(tst.l)) / (kSteps - 1);
136 auto h = std::get<0>(tst.h),
137 s = std::get<0>(tst.s),
138 l = std::get<0>(tst.l);
139 {
140 SkAutoCanvasRestore acr(canvas, true);
141 for (size_t i = 0; i < kSteps; ++i) {
142 paint.setColorFilter(make_filter(h, s, l));
143 canvas->translate(kWheelSize * 0.1f, 0);
144 canvas->drawRect(rect, paint);
145 canvas->translate(kWheelSize * 1.1f, 0);
146 h += dh;
147 s += ds;
148 l += dl;
149 }
150 }
151 canvas->translate(0, kWheelSize * 1.1f);
152 }
153 canvas->translate(0, kWheelSize * 0.1f);
154 }
155 }
156
157private:
158 inline static constexpr SkScalar kWheelSize = 100;
159 inline static constexpr size_t kSteps = 7;
160
161 static sk_sp<SkColorFilter> make_filter(float h, float s, float l) {
162 // These are roughly AE semantics.
163 const auto h_bias = h,
164 h_scale = 1.0f,
165 s_bias = std::max(s, 0.0f),
166 s_scale = 1 - std::abs(s),
167 l_bias = std::max(l, 0.0f),
168 l_scale = 1 - std::abs(l);
169
170 const float cm[20] = {
171 h_scale, 0, 0, 0, h_bias,
172 0, s_scale, 0, 0, s_bias,
173 0, 0, l_scale, 0, l_bias,
174 0, 0, 0, 1, 0,
175 };
176
178 }
179
180 std::vector<sk_sp<SkShader>> fShaders;
181};
182
183DEF_GM(return new HSLColorFilterGM;)
static sk_sp< SkShader > make_shader()
static const TestCase gTests[]
static const struct @225 gGrads[]
IsFiniteProc fProc
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
constexpr SkColor SK_ColorMAGENTA
Definition SkColor.h:147
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
SkString getName() const override
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
void onOnceBeforeDraw() override
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
void drawRect(const SkRect &rect, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
static sk_sp< SkColorFilter > HSLAMatrix(const SkColorMatrix &)
static sk_sp< SkColorFilter > Lighting(SkColor mul, SkColor add)
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition SkMatrix.h:157
const Paint & paint
static void install_nothing(SkPaint *paint, uint32_t, uint32_t)
static void install_lighting(SkPaint *paint, uint32_t mul, uint32_t add)
void(* InstallPaint)(SkPaint *, uint32_t, uint32_t)
float SkScalar
Definition extension.cpp:12
struct MyStruct s
#define DEF_GM(CODE)
Definition gm.h:40
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition DecodeUtils.h:25
SkScalar h
static sk_sp< SkImageFilter > make_filter()
constexpr float height() const
Definition SkRect.h:769
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609