Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
colormatrix.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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"
17#include "include/core/SkRect.h"
21#include "include/core/SkSize.h"
27
28#define WIDTH 500
29#define HEIGHT 160
30
31static void set_color_matrix(SkPaint* paint, const SkColorMatrix& matrix) {
32 paint->setColorFilter(SkColorFilters::Matrix(matrix));
33}
34
35static void set_array(SkPaint* paint, const float array[]) {
36 paint->setColorFilter(SkColorFilters::Matrix(array));
37}
38
39class ColorMatrixGM : public skiagm::GM {
40public:
42 this->setBGColor(0xFF808080);
43 }
44
45protected:
46 SkString getName() const override { return SkString("colormatrix"); }
47
48 SkISize getISize() override { return SkISize::Make(WIDTH, HEIGHT); }
49
50 void onOnceBeforeDraw() override {
51 fSolidImg = CreateSolidBitmap(64, 64);
52 fTransparentImg = CreateTransparentBitmap(64, 64);
53 }
54
56 SkBitmap bm;
58 SkCanvas canvas(bm);
59 canvas.clear(0x0);
60 for (int y = 0; y < height; ++y) {
61 for (int x = 0; x < width; ++x) {
63 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0));
66 }
67 }
68 return bm.asImage();
69 }
70
71 // creates a bitmap with shades of transparent gray.
73 SkBitmap bm;
75 SkCanvas canvas(bm);
76 canvas.clear(0x0);
77
78 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}};
79 SkColor colors[] = {0x00000000, 0xFFFFFFFF};
81 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
84 return bm.asImage();
85 }
86
87 void onDraw(SkCanvas* canvas) override {
89 SkColorMatrix matrix;
90
91 paint.setBlendMode(SkBlendMode::kSrc);
92 const SkImage* bmps[] = { fSolidImg.get(), fTransparentImg.get() };
93
94 for (size_t i = 0; i < std::size(bmps); ++i) {
95 matrix.setIdentity();
96 set_color_matrix(&paint, matrix);
97 canvas->drawImage(bmps[i], 0, 0, SkSamplingOptions(), &paint);
98
99 ///////////////////////////////////////////////
100
101 matrix.setSaturation(0.0f);
102 set_color_matrix(&paint, matrix);
103 canvas->drawImage(bmps[i], 80, 0, SkSamplingOptions(), &paint);
104
105 matrix.setSaturation(0.5f);
106 set_color_matrix(&paint, matrix);
107 canvas->drawImage(bmps[i], 160, 0, SkSamplingOptions(), &paint);
108
109 matrix.setSaturation(1.0f);
110 set_color_matrix(&paint, matrix);
111 canvas->drawImage(bmps[i], 240, 0, SkSamplingOptions(), &paint);
112
113 matrix.setSaturation(2.0f);
114 set_color_matrix(&paint, matrix);
115 canvas->drawImage(bmps[i], 320, 0, SkSamplingOptions(), &paint);
116
117 ///////////////////////////////////////////////
118
119 // Move red into alpha, set color to white
120 float data[20] = {
121 0, 0, 0, 0, 1,
122 0, 0, 0, 0, 1,
123 0, 0, 0, 0, 1,
124 1, 0, 0, 0, 0,
125 };
126
127 set_array(&paint, data);
128 canvas->drawImage(bmps[i], 400, 0, SkSamplingOptions(), &paint);
129 ///////////////////////////////////////////////
130 canvas->translate(0, 80);
131 }
132 }
133
134private:
135 sk_sp<SkImage> fSolidImg;
136 sk_sp<SkImage> fTransparentImg;
137
138 using INHERITED = skiagm::GM;
139};
140DEF_GM( return new ColorMatrixGM; )
uint32_t SkColor
Definition SkColor.h:37
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
#define SK_Scalar1
Definition SkScalar.h:18
#define SkIntToScalar(x)
Definition SkScalar.h:57
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
static sk_sp< SkImage > CreateSolidBitmap(int width, int height)
void onOnceBeforeDraw() override
static sk_sp< SkImage > CreateTransparentBitmap(int width, int height)
SkString getName() const override
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
void drawRect(const SkRect &rect, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void clear(SkColor color)
Definition SkCanvas.h:1199
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
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)
T * get() const
Definition SkRefCnt.h:303
SkScalar width()
Definition gm.h:159
SkScalar height()
Definition gm.h:162
void setBGColor(SkColor)
Definition gm.cpp:159
const Paint & paint
#define WIDTH
static void set_array(SkPaint *paint, const float array[])
static void set_color_matrix(SkPaint *paint, const SkColorMatrix &matrix)
#define HEIGHT
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609