Hal Canary | 6c8422c | 2020-01-10 15:22:09 -0500 | [diff] [blame] | 1 | // Copyright 2020 Google LLC. |
| 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 | #include "tools/fiddle/examples.h" |
| 4 | REG_FIDDLE(skpaint_xfer, 576, 640, false, 0) { |
| 5 | static void draw_utf8_string(SkCanvas* c, const char* t, SkScalar x, SkScalar y, |
| 6 | const SkFont& f, const SkPaint& p) { |
| 7 | c->drawTextBlob(SkTextBlob::MakeFromString(t, f).get(), x, y, p); |
| 8 | } |
| 9 | |
| 10 | void draw(SkCanvas* canvas) { |
| 11 | SkBlendMode modes[] = { |
| 12 | SkBlendMode::kClear, |
| 13 | SkBlendMode::kSrc, |
| 14 | SkBlendMode::kDst, |
| 15 | SkBlendMode::kSrcOver, |
| 16 | SkBlendMode::kDstOver, |
| 17 | SkBlendMode::kSrcIn, |
| 18 | SkBlendMode::kDstIn, |
| 19 | SkBlendMode::kSrcOut, |
| 20 | SkBlendMode::kDstOut, |
| 21 | SkBlendMode::kSrcATop, |
| 22 | SkBlendMode::kDstATop, |
| 23 | SkBlendMode::kXor, |
| 24 | SkBlendMode::kPlus, |
| 25 | SkBlendMode::kModulate, |
| 26 | SkBlendMode::kScreen, |
| 27 | SkBlendMode::kOverlay, |
| 28 | SkBlendMode::kDarken, |
| 29 | SkBlendMode::kLighten, |
| 30 | SkBlendMode::kColorDodge, |
| 31 | SkBlendMode::kColorBurn, |
| 32 | SkBlendMode::kHardLight, |
| 33 | SkBlendMode::kSoftLight, |
| 34 | SkBlendMode::kDifference, |
| 35 | SkBlendMode::kExclusion, |
| 36 | SkBlendMode::kMultiply, |
| 37 | SkBlendMode::kHue, |
| 38 | SkBlendMode::kSaturation, |
| 39 | SkBlendMode::kColor, |
| 40 | SkBlendMode::kLuminosity, |
| 41 | }; |
| 42 | SkRect rect = SkRect::MakeWH(64.0f, 64.0f); |
| 43 | SkPaint stroke, src, dst; |
| 44 | stroke.setStyle(SkPaint::kStroke_Style); |
| 45 | SkFont font(nullptr, 24); |
| 46 | SkPoint srcPoints[2] = { |
| 47 | SkPoint::Make(0.0f, 0.0f), |
| 48 | SkPoint::Make(64.0f, 0.0f) |
| 49 | }; |
| 50 | SkColor srcColors[2] = { |
| 51 | SK_ColorMAGENTA & 0x00FFFFFF, |
| 52 | SK_ColorMAGENTA}; |
| 53 | src.setShader(SkGradientShader::MakeLinear( |
| 54 | srcPoints, srcColors, nullptr, 2, |
| 55 | SkTileMode::kClamp, 0, nullptr)); |
| 56 | |
| 57 | SkPoint dstPoints[2] = { |
| 58 | SkPoint::Make(0.0f, 0.0f), |
| 59 | SkPoint::Make(0.0f, 64.0f) |
| 60 | }; |
| 61 | SkColor dstColors[2] = { |
| 62 | SK_ColorCYAN & 0x00FFFFFF, |
| 63 | SK_ColorCYAN}; |
| 64 | dst.setShader(SkGradientShader::MakeLinear( |
| 65 | dstPoints, dstColors, nullptr, 2, |
| 66 | SkTileMode::kClamp, 0, nullptr)); |
| 67 | canvas->clear(SK_ColorWHITE); |
| 68 | size_t N = sizeof(modes) / sizeof(modes[0]); |
| 69 | size_t K = (N - 1) / 3 + 1; |
| 70 | SkASSERT(K * 64 == 640); // tall enough |
| 71 | for (size_t i = 0; i < N; ++i) { |
| 72 | SkAutoCanvasRestore autoCanvasRestore(canvas, true); |
| 73 | canvas->translate(192.0f * (i / K), 64.0f * (i % K)); |
| 74 | const char* desc = SkBlendMode_Name(modes[i]); |
| 75 | draw_utf8_string(canvas, desc, 68.0f, 30.0f, font, SkPaint()); |
| 76 | canvas->clipRect(SkRect::MakeWH(64.0f, 64.0f)); |
| 77 | canvas->drawColor(SK_ColorLTGRAY); |
| 78 | (void)canvas->saveLayer(nullptr, nullptr); |
| 79 | canvas->clear(SK_ColorTRANSPARENT); |
| 80 | canvas->drawPaint(dst); |
| 81 | src.setBlendMode(modes[i]); |
| 82 | canvas->drawPaint(src); |
| 83 | canvas->drawRect(rect, stroke); |
| 84 | } |
| 85 | } |
| 86 | } // END FIDDLE |