blob: bedb7f6058ce9e3e8c2002003eff85f6319da478 [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"
10#include "SkColorFilter.h"
11#include "SkGradientShader.h"
12
13static SkShader* make_shader(const SkRect& bounds) {
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 };
22 return SkGradientShader::CreateLinear(pts,
23 colors, NULL, SK_ARRAY_COUNT(colors),
24 SkShader::kClamp_TileMode);
25}
26
27typedef void (*InstallPaint)(SkPaint*, uint32_t, uint32_t);
28
29static void install_nothing(SkPaint* paint, uint32_t, uint32_t) {
30 paint->setColorFilter(NULL);
31}
32
33static void install_lighting(SkPaint* paint, uint32_t mul, uint32_t add) {
34 paint->setColorFilter(SkColorFilter::CreateLightingFilter(mul, add))->unref();
35}
36
37class ColorFiltersGM : public skiagm::GM {
38public:
39 ColorFiltersGM() {
40 fName.set("lightingcolorfilter");
41 }
42
43protected:
mtklein36352bf2015-03-25 18:17:31 -070044 virtual SkString onShortName() override {
reed@google.comd5808bd2014-02-06 16:53:25 +000045 return fName;
46 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 virtual SkISize onISize() override {
reed@google.comd5808bd2014-02-06 16:53:25 +000049 return SkISize::Make(640, 480);
50 }
51
mtklein36352bf2015-03-25 18:17:31 -070052 void onDraw(SkCanvas* canvas) override {
reed@google.comd5808bd2014-02-06 16:53:25 +000053 SkPaint paint;
54 SkRect r;
55 r.setWH(600, 50);
56 paint.setShader(make_shader(r))->unref();
57
58 const struct {
59 InstallPaint fProc;
60 uint32_t fData0, fData1;
61 } rec[] = {
62 { install_nothing, 0, 0 },
63 { install_lighting, 0xFF0000, 0 },
64 { install_lighting, 0x00FF00, 0 },
65 { install_lighting, 0x0000FF, 0 },
66 { install_lighting, 0x000000, 0xFF0000 },
67 { install_lighting, 0x000000, 0x00FF00 },
68 { install_lighting, 0x000000, 0x0000FF },
69 };
70
71 canvas->translate(10, 10);
72 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
73 rec[i].fProc(&paint, rec[i].fData0, rec[i].fData1);
74 canvas->drawRect(r, paint);
75 canvas->translate(0, r.height() + 10);
76 }
77 }
78
79private:
80 SkString fName;
81 typedef GM INHERITED;
82};
83
84
85//////////////////////////////////////////////////////////////////////////////
86
87DEF_GM( return SkNEW(ColorFiltersGM); )