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 "sk_tool_utils.h"
10 #include "SkColorMatrixFilter.h"
11 #include "SkGradientShader.h"
17 static void set_color_matrix(SkPaint* paint, const SkColorMatrix& matrix) {
18 paint->setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix.fMat));
21 static void set_array(SkPaint* paint, const SkScalar array[]) {
22 paint->setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(array));
25 class ColorMatrixGM : public skiagm::GM {
28 this->setBGColor(sk_tool_utils::color_to_565(0xFF808080));
32 SkString onShortName() override {
33 return SkString("colormatrix");
36 SkISize onISize() override {
37 return SkISize::Make(WIDTH, HEIGHT);
40 void onOnceBeforeDraw() override {
41 fSolidImg = CreateSolidBitmap(64, 64);
42 fTransparentImg = CreateTransparentBitmap(64, 64);
45 static sk_sp<SkImage> CreateSolidBitmap(int width, int height) {
47 bm.allocN32Pixels(width, height);
50 for (int y = 0; y < height; ++y) {
51 for (int x = 0; x < width; ++x) {
53 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0));
54 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x),
55 SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint);
58 return SkImage::MakeFromBitmap(bm);
61 // creates a bitmap with shades of transparent gray.
62 static sk_sp<SkImage> CreateTransparentBitmap(int width, int height) {
64 bm.allocN32Pixels(width, height);
68 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}};
69 SkColor colors[] = {0x00000000, 0xFFFFFFFF};
71 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
72 SkShader::kClamp_TileMode));
73 canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint);
74 return SkImage::MakeFromBitmap(bm);
77 void onDraw(SkCanvas* canvas) override {
81 paint.setBlendMode(SkBlendMode::kSrc);
82 const SkImage* bmps[] = { fSolidImg.get(), fTransparentImg.get() };
84 for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) {
86 set_color_matrix(&paint, matrix);
87 canvas->drawImage(bmps[i], 0, 0, &paint);
89 matrix.setRotate(SkColorMatrix::kR_Axis, 90);
90 set_color_matrix(&paint, matrix);
91 canvas->drawImage(bmps[i], 80, 0, &paint);
93 matrix.setRotate(SkColorMatrix::kG_Axis, 90);
94 set_color_matrix(&paint, matrix);
95 canvas->drawImage(bmps[i], 160, 0, &paint);
97 matrix.setRotate(SkColorMatrix::kB_Axis, 90);
98 set_color_matrix(&paint, matrix);
99 canvas->drawImage(bmps[i], 240, 0, &paint);
100 ///////////////////////////////////////////////
101 matrix.setSaturation(0.0f);
102 set_color_matrix(&paint, matrix);
103 canvas->drawImage(bmps[i], 0, 80, &paint);
105 matrix.setSaturation(0.5f);
106 set_color_matrix(&paint, matrix);
107 canvas->drawImage(bmps[i], 80, 80, &paint);
109 matrix.setSaturation(1.0f);
110 set_color_matrix(&paint, matrix);
111 canvas->drawImage(bmps[i], 160, 80, &paint);
113 matrix.setSaturation(2.0f);
114 set_color_matrix(&paint, matrix);
115 canvas->drawImage(bmps[i], 240, 80, &paint);
116 ///////////////////////////////////////////////
118 set_color_matrix(&paint, matrix);
119 canvas->drawImage(bmps[i], 0, 160, &paint);
122 set_color_matrix(&paint, matrix);
123 canvas->drawImage(bmps[i], 80, 160, &paint);
125 SkScalar s1 = SK_Scalar1;
126 SkScalar s255 = SkIntToScalar(255);
127 // Move red into alpha, set color to white
128 SkScalar data[20] = {
135 set_array(&paint, data);
136 canvas->drawImage(bmps[i], 160, 160, &paint);
137 ///////////////////////////////////////////////
138 canvas->translate(0, 240);
143 sk_sp<SkImage> fSolidImg;
144 sk_sp<SkImage> fTransparentImg;
146 typedef skiagm::GM INHERITED;
148 DEF_GM( return new ColorMatrixGM; )