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"
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:
44    virtual SkString onShortName() {
45        return fName;
46    }
47
48    virtual SkISize onISize() {
49        return SkISize::Make(640, 480);
50    }
51
52    virtual uint32_t onGetFlags() const SK_OVERRIDE {
53        return kSkipTiled_Flag;
54    }
55
56    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
57        SkPaint paint;
58        SkRect r;
59        r.setWH(600, 50);
60        paint.setShader(make_shader(r))->unref();
61
62        const struct {
63            InstallPaint    fProc;
64            uint32_t        fData0, fData1;
65        } rec[] = {
66            { install_nothing, 0, 0 },
67            { install_lighting, 0xFF0000, 0 },
68            { install_lighting, 0x00FF00, 0 },
69            { install_lighting, 0x0000FF, 0 },
70            { install_lighting, 0x000000, 0xFF0000 },
71            { install_lighting, 0x000000, 0x00FF00 },
72            { install_lighting, 0x000000, 0x0000FF },
73        };
74
75        canvas->translate(10, 10);
76        for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
77            rec[i].fProc(&paint, rec[i].fData0, rec[i].fData1);
78            canvas->drawRect(r, paint);
79            canvas->translate(0, r.height() + 10);
80        }
81    }
82
83private:
84    SkString fName;
85    typedef GM INHERITED;
86};
87
88
89//////////////////////////////////////////////////////////////////////////////
90
91DEF_GM( return SkNEW(ColorFiltersGM); )
92