blob: 2accf536db192600702c0962ffdf54525d52b3f5 [file] [log] [blame]
reed@google.comd5808bd2014-02-06 16:53:25 +00001/*
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"
reed0daf5dd2016-01-11 12:34:04 -080010#include "SkColorMatrixFilter.h"
reed@google.comd5808bd2014-02-06 16:53:25 +000011#include "SkGradientShader.h"
12
reed2ad1aa62016-03-09 09:50:50 -080013static sk_sp<SkShader> make_shader(const SkRect& bounds) {
reed@google.comd5808bd2014-02-06 16:53:25 +000014 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 };
reed2ad1aa62016-03-09 09:50:50 -080022 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
23 SkShader::kClamp_TileMode);
reed@google.comd5808bd2014-02-06 16:53:25 +000024}
25
26typedef void (*InstallPaint)(SkPaint*, uint32_t, uint32_t);
27
28static void install_nothing(SkPaint* paint, uint32_t, uint32_t) {
halcanary96fcdcc2015-08-27 07:41:13 -070029 paint->setColorFilter(nullptr);
reed@google.comd5808bd2014-02-06 16:53:25 +000030}
31
32static void install_lighting(SkPaint* paint, uint32_t mul, uint32_t add) {
reedd053ce92016-03-22 10:17:23 -070033 paint->setColorFilter(SkColorMatrixFilter::MakeLightingFilter(mul, add));
reed@google.comd5808bd2014-02-06 16:53:25 +000034}
35
36class ColorFiltersGM : public skiagm::GM {
37public:
38 ColorFiltersGM() {
39 fName.set("lightingcolorfilter");
40 }
41
42protected:
mtklein36352bf2015-03-25 18:17:31 -070043 virtual SkString onShortName() override {
reed@google.comd5808bd2014-02-06 16:53:25 +000044 return fName;
45 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 virtual SkISize onISize() override {
reed6dc14aa2016-04-11 07:46:38 -070048 return SkISize::Make(620, 430);
reed@google.comd5808bd2014-02-06 16:53:25 +000049 }
50
mtklein36352bf2015-03-25 18:17:31 -070051 void onDraw(SkCanvas* canvas) override {
reed@google.comd5808bd2014-02-06 16:53:25 +000052 SkPaint paint;
53 SkRect r;
54 r.setWH(600, 50);
reed2ad1aa62016-03-09 09:50:50 -080055 paint.setShader(make_shader(r));
reed@google.comd5808bd2014-02-06 16:53:25 +000056
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
78private:
79 SkString fName;
80 typedef GM INHERITED;
81};
82
83
84//////////////////////////////////////////////////////////////////////////////
85
halcanary385fe4d2015-08-26 13:07:48 -070086DEF_GM(return new ColorFiltersGM;)