| 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/SkGradient.h" |
| 27 |
|
| 28 |
#define WIDTH 500 |
| 29 |
#define HEIGHT 160 |
| 30 |
|
| 31 |
static void set_color_matrix(SkPaint* paint, const SkColorMatrix& matrix) { |
| 32 |
paint->setColorFilter(SkColorFilters::Matrix(matrix)); |
| 33 |
} |
| 34 |
|
| 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: |
| 41 |
ColorMatrixGM() { |
| 42 |
this->setBGColor(0xFF808080); |
| 43 |
} |
| 44 |
|
| 45 |
protected: |
| 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 |
|
| 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. |
| 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 |
const SkColor4f colors[] = {{0,0,0,0}, {1,1,1,1}}; |
| 80 |
SkPaint paint; |
| 81 |
paint.setShader(SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}})); |
| 82 |
canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint); |
| 83 |
return bm.asImage(); |
| 84 |
} |
| 85 |
|
| 86 |
void onDraw(SkCanvas* canvas) override { |
| 87 |
SkPaint paint; |
| 88 |
SkColorMatrix matrix; |
| 89 |
|
| 90 |
paint.setBlendMode(SkBlendMode::kSrc); |
| 91 |
const SkImage* bmps[] = { fSolidImg.get(), fTransparentImg.get() }; |
| 92 |
|
| 93 |
for (size_t i = 0; i < std::size(bmps); ++i) { |
| 94 |
matrix.setIdentity(); |
| 95 |
set_color_matrix(&paint, matrix); |
| 96 |
canvas->drawImage(bmps[i], 0, 0, SkSamplingOptions(), &paint); |
| 97 |
|
| 98 |
/////////////////////////////////////////////// |
| 99 |
|
| 100 |
matrix.setSaturation(0.0f); |
| 101 |
set_color_matrix(&paint, matrix); |
| 102 |
canvas->drawImage(bmps[i], 80, 0, SkSamplingOptions(), &paint); |
| 103 |
|
| 104 |
matrix.setSaturation(0.5f); |
| 105 |
set_color_matrix(&paint, matrix); |
| 106 |
canvas->drawImage(bmps[i], 160, 0, SkSamplingOptions(), &paint); |
| 107 |
|
| 108 |
matrix.setSaturation(1.0f); |
| 109 |
set_color_matrix(&paint, matrix); |
| 110 |
canvas->drawImage(bmps[i], 240, 0, SkSamplingOptions(), &paint); |
| 111 |
|
| 112 |
matrix.setSaturation(2.0f); |
| 113 |
set_color_matrix(&paint, matrix); |
| 114 |
canvas->drawImage(bmps[i], 320, 0, SkSamplingOptions(), &paint); |
| 115 |
|
| 116 |
/////////////////////////////////////////////// |
| 117 |
|
| 118 |
// Move red into alpha, set color to white |
| 119 |
float data[20] = { |
| 120 |
0, 0, 0, 0, 1, |
| 121 |
0, 0, 0, 0, 1, |
| 122 |
0, 0, 0, 0, 1, |
| 123 |
1, 0, 0, 0, 0, |
| 124 |
}; |
| 125 |
|
| 126 |
set_array(&paint, data); |
| 127 |
canvas->drawImage(bmps[i], 400, 0, SkSamplingOptions(), &paint); |
| 128 |
/////////////////////////////////////////////// |
| 129 |
canvas->translate(0, 80); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
private: |
| 134 |
sk_sp<SkImage> fSolidImg; |
| 135 |
sk_sp<SkImage> fTransparentImg; |
| 136 |
|
| 137 |
using INHERITED = skiagm::GM; |
| 138 |
}; |
| 139 |
DEF_GM( return new ColorMatrixGM; ) |