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:
onShortName()46 SkString onShortName() override {
47 return SkString("colormatrix");
48 }
49
onISize()50 SkISize onISize() override {
51 return SkISize::Make(WIDTH, HEIGHT);
52 }
53
onOnceBeforeDraw()54 void onOnceBeforeDraw() override {
55 fSolidImg = CreateSolidBitmap(64, 64);
56 fTransparentImg = CreateTransparentBitmap(64, 64);
57 }
58
CreateSolidBitmap(int width,int height)59 static sk_sp<SkImage> CreateSolidBitmap(int width, int height) {
60 SkBitmap bm;
61 bm.allocN32Pixels(width, height);
62 SkCanvas canvas(bm);
63 canvas.clear(0x0);
64 for (int y = 0; y < height; ++y) {
65 for (int x = 0; x < width; ++x) {
66 SkPaint paint;
67 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0));
68 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x),
69 SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint);
70 }
71 }
72 return bm.asImage();
73 }
74
75 // creates a bitmap with shades of transparent gray.
CreateTransparentBitmap(int width,int height)76 static sk_sp<SkImage> CreateTransparentBitmap(int width, int height) {
77 SkBitmap bm;
78 bm.allocN32Pixels(width, height);
79 SkCanvas canvas(bm);
80 canvas.clear(0x0);
81
82 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}};
83 SkColor colors[] = {0x00000000, 0xFFFFFFFF};
84 SkPaint paint;
85 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
86 SkTileMode::kClamp));
87 canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint);
88 return bm.asImage();
89 }
90
onDraw(SkCanvas * canvas)91 void onDraw(SkCanvas* canvas) override {
92 SkPaint paint;
93 SkColorMatrix matrix;
94
95 paint.setBlendMode(SkBlendMode::kSrc);
96 const SkImage* bmps[] = { fSolidImg.get(), fTransparentImg.get() };
97
98 for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) {
99 matrix.setIdentity();
100 set_color_matrix(&paint, matrix);
101 canvas->drawImage(bmps[i], 0, 0, SkSamplingOptions(), &paint);
102
103 ///////////////////////////////////////////////
104
105 matrix.setSaturation(0.0f);
106 set_color_matrix(&paint, matrix);
107 canvas->drawImage(bmps[i], 80, 0, SkSamplingOptions(), &paint);
108
109 matrix.setSaturation(0.5f);
110 set_color_matrix(&paint, matrix);
111 canvas->drawImage(bmps[i], 160, 0, SkSamplingOptions(), &paint);
112
113 matrix.setSaturation(1.0f);
114 set_color_matrix(&paint, matrix);
115 canvas->drawImage(bmps[i], 240, 0, SkSamplingOptions(), &paint);
116
117 matrix.setSaturation(2.0f);
118 set_color_matrix(&paint, matrix);
119 canvas->drawImage(bmps[i], 320, 0, SkSamplingOptions(), &paint);
120
121 ///////////////////////////////////////////////
122
123 // Move red into alpha, set color to white
124 float data[20] = {
125 0, 0, 0, 0, 1,
126 0, 0, 0, 0, 1,
127 0, 0, 0, 0, 1,
128 1, 0, 0, 0, 0,
129 };
130
131 set_array(&paint, data);
132 canvas->drawImage(bmps[i], 400, 0, SkSamplingOptions(), &paint);
133 ///////////////////////////////////////////////
134 canvas->translate(0, 80);
135 }
136 }
137
138 private:
139 sk_sp<SkImage> fSolidImg;
140 sk_sp<SkImage> fTransparentImg;
141
142 using INHERITED = skiagm::GM;
143 };
144 DEF_GM( return new ColorMatrixGM; )
145