2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkColorPriv.h"
13 #include "SkArithmeticMode.h"
14 #include "SkGradientShader.h"
18 static SkBitmap make_bm() {
20 bm.allocN32Pixels(WW, HH);
21 bm.eraseColor(SK_ColorTRANSPARENT);
25 static SkBitmap make_src() {
26 SkBitmap bm = make_bm();
29 SkPoint pts[] = { {0, 0}, {SkIntToScalar(WW), SkIntToScalar(HH)} };
31 SK_ColorTRANSPARENT, SK_ColorGREEN, SK_ColorCYAN,
32 SK_ColorRED, SK_ColorMAGENTA, SK_ColorWHITE,
34 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
35 SkShader::kClamp_TileMode);
36 paint.setShader(s)->unref();
37 canvas.drawPaint(paint);
41 static SkBitmap make_dst() {
42 SkBitmap bm = make_bm();
45 SkPoint pts[] = { {0, SkIntToScalar(HH)}, {SkIntToScalar(WW), 0} };
47 SK_ColorBLUE, SK_ColorYELLOW, SK_ColorBLACK, SK_ColorGREEN, SK_ColorGRAY
49 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
50 SkShader::kClamp_TileMode);
51 paint.setShader(s)->unref();
52 canvas.drawPaint(paint);
56 static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar k[]) {
58 paint.setTextSize(SkIntToScalar(24));
59 paint.setAntiAlias(true);
60 sk_tool_utils::set_portable_typeface(&paint);
61 for (int i = 0; i < 4; ++i) {
63 str.appendScalar(k[i]);
64 SkScalar width = paint.measureText(str.c_str(), str.size());
65 canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint);
66 x += width + SkIntToScalar(10);
70 class ArithmodeGM : public skiagm::GM {
76 virtual SkString onShortName() {
77 return SkString("arithmode");
80 virtual SkISize onISize() { return SkISize::Make(640, 480); }
82 virtual void onDraw(SkCanvas* canvas) {
83 SkBitmap src = make_src();
84 SkBitmap dst = make_dst();
86 const SkScalar one = SK_Scalar1;
87 static const SkScalar K[] = {
95 0, one/2, one/2, one/4,
96 0, one/2, one/2, -one/4,
97 one/4, one/2, one/2, 0,
98 -one/4, one/2, one/2, 0,
101 const SkScalar* k = K;
102 const SkScalar* stop = k + SK_ARRAY_COUNT(K);
104 SkScalar gap = SkIntToScalar(src.width() + 20);
107 canvas->drawBitmap(src, x, y, NULL);
109 canvas->drawBitmap(dst, x, y, NULL);
111 SkRect rect = SkRect::MakeXYWH(x, y, SkIntToScalar(WW), SkIntToScalar(HH));
112 canvas->saveLayer(&rect, NULL);
113 canvas->drawBitmap(dst, x, y, NULL);
114 SkXfermode* xfer = SkArithmeticMode::Create(k[0], k[1], k[2], k[3]);
116 paint.setXfermode(xfer)->unref();
117 canvas->drawBitmap(src, x, y, &paint);
120 show_k_text(canvas, x, y, k);
122 y += SkIntToScalar(src.height() + 12);
127 typedef GM INHERITED;
130 ///////////////////////////////////////////////////////////////////////////////
132 static skiagm::GM* MyFactory(void*) { return new ArithmodeGM; }
133 static skiagm::GMRegistry reg(MyFactory);