blob: 474f2b2485fe9bd8a61f1ca403ab591363d69b2f [file] [log] [blame]
Hal Canaryf828c1d2017-07-19 17:25:38 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlurTypes.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkColorFilter.h"
14#include "include/core/SkImage.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040015#include "include/core/SkImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkMaskFilter.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040017#include "include/core/SkPaint.h"
18#include "include/core/SkRefCnt.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkShader.h"
Hal Canaryf828c1d2017-07-19 17:25:38 -040020
21static 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
34static sk_sp<SkColorFilter> make_color_filter() {
Mike Reede869a1e2019-04-30 12:18:54 -040035 float colorMatrix[20] = {
Hal Canaryf828c1d2017-07-19 17:25:38 -040036 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 Reede869a1e2019-04-30 12:18:54 -040040 return SkColorFilters::Matrix(colorMatrix);
Hal Canaryf828c1d2017-07-19 17:25:38 -040041}
42
43DEF_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 Reed1be1f8d2018-03-14 13:01:17 -040048 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10.0f));
Hal Canaryf828c1d2017-07-19 17:25:38 -040049 canvas->drawImage(image.get(), 16, 16, &paint);
50
51 paint.setColorFilter(nullptr);
Mike Reedc8bea7d2019-04-09 13:55:36 -040052 paint.setShader(SkShaders::Color(SK_ColorCYAN));
Hal Canaryf828c1d2017-07-19 17:25:38 -040053 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}