aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/colortypexfermode.cpp
blob: 9541e0825856b65ee1e53f6949779cc5d97b91d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "gm.h"
#include "SkBitmap.h"
#include "SkGradientShader.h"
#include "SkShader.h"
#include "../src/fonts/SkGScalerContext.h"

namespace skiagm {

static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };

class ColorTypeXfermodeGM : public GM {
public:
    const static int W = 64;
    const static int H = 64;
    ColorTypeXfermodeGM() {}

protected:
    void onOnceBeforeDraw() override {
        const SkColor colors[] = {
            SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
            SK_ColorMAGENTA, SK_ColorCYAN, SK_ColorYELLOW
        };
        SkMatrix local;
        local.setRotate(180);
        SkPaint paint;
        paint.setAntiAlias(true);
        paint.setShader(SkGradientShader::MakeSweep(0, 0, colors, nullptr, SK_ARRAY_COUNT(colors),
                                                    0, &local));

        sk_sp<SkTypeface> orig(sk_tool_utils::create_portable_typeface("serif",
                                   SkFontStyle::FromOldStyle(SkTypeface::kBold)));
        if (nullptr == orig) {
            orig = SkTypeface::MakeDefault();
        }
        fColorType = sk_make_sp<SkGTypeface>(orig, paint);

        fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
                                            kOpaque_SkAlphaType), gData, 4);
    }

    virtual SkString onShortName() override {
        return SkString("colortype_xfermodes");
    }

    virtual SkISize onISize() override {
        return SkISize::Make(400, 640);
    }

    virtual void onDraw(SkCanvas* canvas) override {
        canvas->translate(SkIntToScalar(10), SkIntToScalar(20));

        const struct {
            SkBlendMode fMode;
            const char* fLabel;
        } gModes[] = {
            { SkBlendMode::kClear,        "Clear"       },
            { SkBlendMode::kSrc,          "Src"         },
            { SkBlendMode::kDst,          "Dst"         },
            { SkBlendMode::kSrcOver,      "SrcOver"     },
            { SkBlendMode::kDstOver,      "DstOver"     },
            { SkBlendMode::kSrcIn,        "SrcIn"       },
            { SkBlendMode::kDstIn,        "DstIn"       },
            { SkBlendMode::kSrcOut,       "SrcOut"      },
            { SkBlendMode::kDstOut,       "DstOut"      },
            { SkBlendMode::kSrcATop,      "SrcATop"     },
            { SkBlendMode::kDstATop,      "DstATop"     },

            { SkBlendMode::kXor,          "Xor"         },
            { SkBlendMode::kPlus,         "Plus"        },
            { SkBlendMode::kModulate,     "Modulate"    },
            { SkBlendMode::kScreen,       "Screen"      },
            { SkBlendMode::kOverlay,      "Overlay"     },
            { SkBlendMode::kDarken,       "Darken"      },
            { SkBlendMode::kLighten,      "Lighten"     },
            { SkBlendMode::kColorDodge,   "ColorDodge"  },
            { SkBlendMode::kColorBurn,    "ColorBurn"   },
            { SkBlendMode::kHardLight,    "HardLight"   },
            { SkBlendMode::kSoftLight,    "SoftLight"   },
            { SkBlendMode::kDifference,   "Difference"  },
            { SkBlendMode::kExclusion,    "Exclusion"   },
            { SkBlendMode::kMultiply,     "Multiply"    },
            { SkBlendMode::kHue,          "Hue"         },
            { SkBlendMode::kSaturation,   "Saturation"  },
            { SkBlendMode::kColor,        "Color"       },
            { SkBlendMode::kLuminosity,   "Luminosity"  },
        };

        const SkScalar w = SkIntToScalar(W);
        const SkScalar h = SkIntToScalar(H);
        SkMatrix m;
        m.setScale(SkIntToScalar(6), SkIntToScalar(6));
        auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
                                            SkShader::kRepeat_TileMode, &m);

        SkPaint labelP;
        labelP.setAntiAlias(true);
        sk_tool_utils::set_portable_typeface(&labelP);
        labelP.setTextAlign(SkPaint::kCenter_Align);

        SkPaint textP;
        textP.setAntiAlias(true);
        textP.setTypeface(fColorType);
        textP.setTextSize(SkIntToScalar(70));

        const int W = 5;

        SkScalar x0 = 0;
        SkScalar y0 = 0;
        SkScalar x = x0, y = y0;
        for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
            SkRect r;
            r.set(x, y, x+w, y+h);

            SkPaint p;
            p.setStyle(SkPaint::kFill_Style);
            p.setShader(s);
            canvas->drawRect(r, p);

            r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
            p.setStyle(SkPaint::kStroke_Style);
            p.setShader(nullptr);
            canvas->drawRect(r, p);

            textP.setBlendMode(gModes[i].fMode);
            canvas->drawText("H", 1, x+ w/10.f, y + 7.f*h/8.f, textP);
#if 1
            canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
                             x + w/2, y - labelP.getTextSize()/2, labelP);
#endif
            x += w + SkIntToScalar(10);
            if ((i % W) == W - 1) {
                x = x0;
                y += h + SkIntToScalar(30);
            }
        }
    }

private:
    SkBitmap            fBG;
    sk_sp<SkTypeface>   fColorType;

    typedef GM INHERITED;
};

//////////////////////////////////////////////////////////////////////////////

static GM* MyFactory(void*) { return new ColorTypeXfermodeGM; }
static GMRegistry reg(MyFactory);

}