reed@google.com | d5808bd | 2014-02-06 16:53:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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.h" |
| 9 | #include "SkCanvas.h" |
reed | 0daf5dd | 2016-01-11 12:34:04 -0800 | [diff] [blame] | 10 | #include "SkColorMatrixFilter.h" |
reed@google.com | d5808bd | 2014-02-06 16:53:25 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
| 12 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 13 | static sk_sp<SkShader> make_shader(const SkRect& bounds) { |
reed@google.com | d5808bd | 2014-02-06 16:53:25 +0000 | [diff] [blame] | 14 | const SkPoint pts[] = { |
| 15 | { bounds.left(), bounds.top() }, |
| 16 | { bounds.right(), bounds.bottom() }, |
| 17 | }; |
| 18 | const SkColor colors[] = { |
| 19 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorBLACK, |
| 20 | SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW, |
| 21 | }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 22 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 23 | SkShader::kClamp_TileMode); |
reed@google.com | d5808bd | 2014-02-06 16:53:25 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | typedef void (*InstallPaint)(SkPaint*, uint32_t, uint32_t); |
| 27 | |
| 28 | static void install_nothing(SkPaint* paint, uint32_t, uint32_t) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 29 | paint->setColorFilter(nullptr); |
reed@google.com | d5808bd | 2014-02-06 16:53:25 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static void install_lighting(SkPaint* paint, uint32_t mul, uint32_t add) { |
reed | 0daf5dd | 2016-01-11 12:34:04 -0800 | [diff] [blame] | 33 | paint->setColorFilter(SkColorMatrixFilter::CreateLightingFilter(mul, add))->unref(); |
reed@google.com | d5808bd | 2014-02-06 16:53:25 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | class ColorFiltersGM : public skiagm::GM { |
| 37 | public: |
| 38 | ColorFiltersGM() { |
| 39 | fName.set("lightingcolorfilter"); |
| 40 | } |
| 41 | |
| 42 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | virtual SkString onShortName() override { |
reed@google.com | d5808bd | 2014-02-06 16:53:25 +0000 | [diff] [blame] | 44 | return fName; |
| 45 | } |
| 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | virtual SkISize onISize() override { |
reed@google.com | d5808bd | 2014-02-06 16:53:25 +0000 | [diff] [blame] | 48 | return SkISize::Make(640, 480); |
| 49 | } |
| 50 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 51 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | d5808bd | 2014-02-06 16:53:25 +0000 | [diff] [blame] | 52 | SkPaint paint; |
| 53 | SkRect r; |
| 54 | r.setWH(600, 50); |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 55 | paint.setShader(make_shader(r)); |
reed@google.com | d5808bd | 2014-02-06 16:53:25 +0000 | [diff] [blame] | 56 | |
| 57 | const struct { |
| 58 | InstallPaint fProc; |
| 59 | uint32_t fData0, fData1; |
| 60 | } rec[] = { |
| 61 | { install_nothing, 0, 0 }, |
| 62 | { install_lighting, 0xFF0000, 0 }, |
| 63 | { install_lighting, 0x00FF00, 0 }, |
| 64 | { install_lighting, 0x0000FF, 0 }, |
| 65 | { install_lighting, 0x000000, 0xFF0000 }, |
| 66 | { install_lighting, 0x000000, 0x00FF00 }, |
| 67 | { install_lighting, 0x000000, 0x0000FF }, |
| 68 | }; |
| 69 | |
| 70 | canvas->translate(10, 10); |
| 71 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 72 | rec[i].fProc(&paint, rec[i].fData0, rec[i].fData1); |
| 73 | canvas->drawRect(r, paint); |
| 74 | canvas->translate(0, r.height() + 10); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | SkString fName; |
| 80 | typedef GM INHERITED; |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | ////////////////////////////////////////////////////////////////////////////// |
| 85 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 86 | DEF_GM(return new ColorFiltersGM;) |