2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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"
31 static void set_color_matrix(SkPaint* paint, const SkColorMatrix& matrix) {
32 paint->setColorFilter(SkColorFilters::Matrix(matrix));
35 static void set_array(SkPaint* paint, const float array[]) {
36 paint->setColorFilter(SkColorFilters::Matrix(array));
39 class ColorMatrixGM : public skiagm::GM {
42 this->setBGColor(0xFF808080);
46 SkString onShortName() override {
47 return SkString("colormatrix");
50 SkISize onISize() override {
51 return SkISize::Make(WIDTH, HEIGHT);
54 void onOnceBeforeDraw() override {
55 fSolidImg = CreateSolidBitmap(64, 64);
56 fTransparentImg = CreateTransparentBitmap(64, 64);
59 static sk_sp<SkImage> CreateSolidBitmap(int width, int height) {
61 bm.allocN32Pixels(width, height);
64 for (int y = 0; y < height; ++y) {
65 for (int x = 0; x < width; ++x) {
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);
75 // creates a bitmap with shades of transparent gray.
76 static sk_sp<SkImage> CreateTransparentBitmap(int width, int height) {
78 bm.allocN32Pixels(width, height);
82 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}};
83 SkColor colors[] = {0x00000000, 0xFFFFFFFF};
85 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
87 canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint);
91 void onDraw(SkCanvas* canvas) override {
95 paint.setBlendMode(SkBlendMode::kSrc);
96 const SkImage* bmps[] = { fSolidImg.get(), fTransparentImg.get() };
98 for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) {
100 set_color_matrix(&paint, matrix);
101 canvas->drawImage(bmps[i], 0, 0, SkSamplingOptions(), &paint);
103 ///////////////////////////////////////////////
105 matrix.setSaturation(0.0f);
106 set_color_matrix(&paint, matrix);
107 canvas->drawImage(bmps[i], 80, 0, SkSamplingOptions(), &paint);
109 matrix.setSaturation(0.5f);
110 set_color_matrix(&paint, matrix);
111 canvas->drawImage(bmps[i], 160, 0, SkSamplingOptions(), &paint);
113 matrix.setSaturation(1.0f);
114 set_color_matrix(&paint, matrix);
115 canvas->drawImage(bmps[i], 240, 0, SkSamplingOptions(), &paint);
117 matrix.setSaturation(2.0f);
118 set_color_matrix(&paint, matrix);
119 canvas->drawImage(bmps[i], 320, 0, SkSamplingOptions(), &paint);
121 ///////////////////////////////////////////////
123 // Move red into alpha, set color to white
131 set_array(&paint, data);
132 canvas->drawImage(bmps[i], 400, 0, SkSamplingOptions(), &paint);
133 ///////////////////////////////////////////////
134 canvas->translate(0, 80);
139 sk_sp<SkImage> fSolidImg;
140 sk_sp<SkImage> fTransparentImg;
142 using INHERITED = skiagm::GM;
144 DEF_GM( return new ColorMatrixGM; )