Hal Canary | 6c8422c | 2020-01-10 15:22:09 -0500 | [diff] [blame] | 1 | // Copyright 2020 Google LLC. |
| 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 | #include "tools/fiddle/examples.h" |
| 4 | REG_FIDDLE(alpha_bitmap_color_filter_mask_filter, 256, 256, false, 0) { |
| 5 | static SkBitmap make_alpha_image(int w, int h) { |
| 6 | SkBitmap bm; |
| 7 | bm.allocPixels(SkImageInfo::MakeA8(w, h)); |
| 8 | bm.eraseARGB(10, 0, 0, 0); |
| 9 | for (int y = 0; y < bm.height(); ++y) { |
| 10 | for (int x = y; x < bm.width(); ++x) { |
| 11 | *bm.getAddr8(x, y) = 0xFF; |
| 12 | } |
| 13 | } |
| 14 | bm.setImmutable(); |
| 15 | return bm; |
| 16 | } |
| 17 | |
| 18 | static sk_sp<SkColorFilter> make_color_filter() { |
| 19 | SkScalar colorMatrix[20] = { |
| 20 | 1, 0, 0, 0, 0, |
| 21 | 0, 1, 0, 0, 0, |
| 22 | 0, 0, 0.5, 0.5, 0, |
| 23 | 0, 0, 0.5, 0.5, 0}; // mix G and A. |
| 24 | return SkColorFilters::Matrix(colorMatrix); |
| 25 | } |
| 26 | |
| 27 | void draw(SkCanvas* canvas) { |
| 28 | auto image = SkImage::MakeFromBitmap(make_alpha_image(96, 96)); |
| 29 | SkPaint paint; |
| 30 | |
| 31 | paint.setColorFilter(make_color_filter()); |
| 32 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10.0f, false)); |
| 33 | canvas->drawImage(image.get(), 16, 16, &paint); |
| 34 | |
| 35 | paint.setColorFilter(nullptr); |
| 36 | paint.setShader(SkShaders::Color(SK_ColorCYAN)); |
| 37 | canvas->drawImage(image.get(), 144, 16, &paint); |
| 38 | |
| 39 | paint.setColorFilter(make_color_filter()); |
| 40 | canvas->drawImage(image.get(), 16, 144, &paint); |
| 41 | |
| 42 | paint.setMaskFilter(nullptr); |
| 43 | canvas->drawImage(image.get(), 144, 144, &paint); |
| 44 | } |
| 45 | } // END FIDDLE |