Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkBlurTypes.h" |
| 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkColorFilter.h" |
| 14 | #include "include/core/SkImage.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 15 | #include "include/core/SkImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | d1701ba | 2019-04-30 13:44:26 -0400 | [diff] [blame] | 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkRefCnt.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkShader.h" |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 20 | |
| 21 | static SkBitmap make_alpha_image(int w, int h) { |
| 22 | SkBitmap bm; |
| 23 | bm.allocPixels(SkImageInfo::MakeA8(w, h)); |
| 24 | bm.eraseARGB(10, 0, 0 , 0); |
| 25 | for (int y = 0; y < bm.height(); ++y) { |
| 26 | for (int x = y; x < bm.width(); ++x) { |
| 27 | *bm.getAddr8(x, y) = 0xFF; |
| 28 | } |
| 29 | } |
| 30 | bm.setImmutable(); |
| 31 | return bm; |
| 32 | } |
| 33 | |
| 34 | static sk_sp<SkColorFilter> make_color_filter() { |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 35 | float colorMatrix[20] = { |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 36 | 1, 0, 0, 0, 0, |
| 37 | 0, 1, 0, 0, 0, |
| 38 | 0, 0, 0.5, 0.5, 0, |
| 39 | 0, 0, 0.5, 0.5, 0}; // mix G and A. |
Mike Reed | e869a1e | 2019-04-30 12:18:54 -0400 | [diff] [blame] | 40 | return SkColorFilters::Matrix(colorMatrix); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | DEF_SIMPLE_GM(alpha_image, canvas, 256, 256) { |
| 44 | auto image = SkImage::MakeFromBitmap(make_alpha_image(96, 96)); |
| 45 | SkPaint paint; |
| 46 | |
| 47 | paint.setColorFilter(make_color_filter()); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 48 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10.0f)); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 49 | canvas->drawImage(image.get(), 16, 16, &paint); |
| 50 | |
| 51 | paint.setColorFilter(nullptr); |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 52 | paint.setShader(SkShaders::Color(SK_ColorCYAN)); |
Hal Canary | f828c1d | 2017-07-19 17:25:38 -0400 | [diff] [blame] | 53 | canvas->drawImage(image.get(), 144, 16, &paint); |
| 54 | |
| 55 | paint.setColorFilter(make_color_filter()); |
| 56 | canvas->drawImage(image.get(), 16, 144, &paint); |
| 57 | |
| 58 | paint.setMaskFilter(nullptr); |
| 59 | canvas->drawImage(image.get(), 144, 144, &paint); |
| 60 | } |