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"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorFilter.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkShader.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkTileMode.h"
24 #include "include/core/SkTypes.h"
25 #include "include/effects/SkColorMatrix.h"
26 #include "include/effects/SkGradientShader.h"
27
28 #define WIDTH 500
29 #define HEIGHT 160
30
set_color_matrix(SkPaint * paint,const SkColorMatrix & matrix)31 static void set_color_matrix(SkPaint* paint, const SkColorMatrix& matrix) {
32 paint->setColorFilter(SkColorFilters::Matrix(matrix));
33 }
34
set_array(SkPaint * paint,const float array[])35 static void set_array(SkPaint* paint, const float array[]) {
36 paint->setColorFilter(SkColorFilters::Matrix(array));
37 }
38
39 class ColorMatrixGM : public skiagm::GM {
40 public:
ColorMatrixGM()41 ColorMatrixGM() {
42 this->setBGColor(0xFF808080);
43 }
44
45 protected:
getName() const46 SkString getName() const override { return SkString("colormatrix"); }
47
getISize()48 SkISize getISize() override { return SkISize::Make(WIDTH, HEIGHT); }
49
onOnceBeforeDraw()50 void onOnceBeforeDraw() override {
51 fSolidImg = CreateSolidBitmap(64, 64);
52 fTransparentImg = CreateTransparentBitmap(64, 64);
53 }
54
CreateSolidBitmap(int width,int height)55 static sk_sp<SkImage> CreateSolidBitmap(int width, int height) {
56 SkBitmap bm;
57 bm.allocN32Pixels(width, height);
58 SkCanvas canvas(bm);
59 canvas.clear(0x0);
60 for (int y = 0; y < height; ++y) {
61 for (int x = 0; x < width; ++x) {
62 SkPaint paint;
63 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0));
64 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x),
65 SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint);
66 }
67 }
68 return bm.asImage();
69 }
70
71 // creates a bitmap with shades of transparent gray.
CreateTransparentBitmap(int width,int height)72 static sk_sp<SkImage> CreateTransparentBitmap(int width, int height) {
73 SkBitmap bm;
74 bm.allocN32Pixels(width, height);
75 SkCanvas canvas(bm);
76 canvas.clear(0x0);
77
78 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}};
79 SkColor colors[] = {0x00000000, 0xFFFFFFFF};
80 SkPaint paint;
81 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
82 SkTileMode::kClamp));
83 canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint);
84 return bm.asImage();
85 }
86
onDraw(SkCanvas * canvas)87 void onDraw(SkCanvas* canvas) override {
88 SkPaint paint;
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
134 private:
135 sk_sp<SkImage> fSolidImg;
136 sk_sp<SkImage> fTransparentImg;
137
138 using INHERITED = skiagm::GM;
139 };
140 DEF_GM( return new ColorMatrixGM; )
141