| 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(pathops, 1000, 600, false, 0) { |
| 5 |
void makePaint(SkPaint * paint, SkColor color) { |
| 6 |
paint->setAntiAlias(true); |
| 7 |
paint->setStyle(SkPaint::kFill_Style); |
| 8 |
paint->setColor(color); |
| 9 |
} |
| 10 |
|
| 11 |
SkColor blend(SkColor one, SkColor two) { |
| 12 |
SkBitmap temp; |
| 13 |
temp.allocN32Pixels(1, 1); |
| 14 |
SkCanvas canvas(temp); |
| 15 |
canvas.drawColor(one); |
| 16 |
canvas.drawColor(two); |
| 17 |
void* pixels = temp.getPixels(); |
| 18 |
return *(SkColor*)pixels; |
| 19 |
} |
| 20 |
|
| 21 |
void draw(SkCanvas* canvas) { |
| 22 |
SkPaint fOnePaint; |
| 23 |
SkPaint fTwoPaint; |
| 24 |
SkPaint fOutlinePaint; |
| 25 |
SkPaint fOpPaint[kReverseDifference_SkPathOp - kDifference_SkPathOp + 1]; |
| 26 |
|
| 27 |
const unsigned oneColor = 0xFF8080FF; |
| 28 |
const unsigned twoColor = 0x807F1f1f; |
| 29 |
SkColor blendColor = blend(oneColor, twoColor); |
| 30 |
makePaint(&fOnePaint, oneColor); |
| 31 |
makePaint(&fTwoPaint, twoColor); |
| 32 |
makePaint(&fOpPaint[kDifference_SkPathOp], oneColor); |
| 33 |
makePaint(&fOpPaint[kIntersect_SkPathOp], blendColor); |
| 34 |
makePaint(&fOpPaint[kUnion_SkPathOp], 0xFFc0FFc0); |
| 35 |
makePaint(&fOpPaint[kReverseDifference_SkPathOp], twoColor); |
| 36 |
makePaint(&fOpPaint[kXOR_SkPathOp], 0xFFa0FFe0); |
| 37 |
makePaint(&fOutlinePaint, 0xFF000000); |
| 38 |
fOutlinePaint.setStyle(SkPaint::kStroke_Style); |
| 39 |
|
| 40 |
int yPos = 0; |
| 41 |
for (int oneFill = 0; oneFill <= 1; ++oneFill) { |
| 42 |
SkPathFillType oneF = |
| 43 |
oneFill ? SkPathFillType::kInverseEvenOdd : SkPathFillType::kEvenOdd; |
| 44 |
for (int twoFill = 0; twoFill <= 1; ++twoFill) { |
| 45 |
SkPathFillType twoF = |
| 46 |
twoFill ? SkPathFillType::kInverseEvenOdd : SkPathFillType::kEvenOdd; |
| 47 |
SkPath one = SkPathBuilder(oneF) |
| 48 |
.moveTo(10, 10) |
| 49 |
.conicTo(0, 90, 50, 50, 3) |
| 50 |
.conicTo(90, 0, 90, 90, 2) |
| 51 |
.close() |
| 52 |
.detach(); |
| 53 |
|
| 54 |
SkPath two = SkPath::Rect({40, 40, 100, 100}, twoF); |
| 55 |
canvas->save(); |
| 56 |
canvas->translate(0, SkIntToScalar(yPos)); |
| 57 |
canvas->clipRect(SkRect::MakeWH(110, 110), SkClipOp::kIntersect, true); |
| 58 |
canvas->drawPath(one, fOnePaint); |
| 59 |
canvas->drawPath(one, fOutlinePaint); |
| 60 |
canvas->drawPath(two, fTwoPaint); |
| 61 |
canvas->drawPath(two, fOutlinePaint); |
| 62 |
canvas->restore(); |
| 63 |
int xPos = 150; |
| 64 |
for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) { |
| 65 |
SkPath result = Op(one, two, (SkPathOp)op).value_or(SkPath()); |
| 66 |
canvas->save(); |
| 67 |
canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos)); |
| 68 |
canvas->clipRect(SkRect::MakeWH(110, 110), SkClipOp::kIntersect, true); |
| 69 |
canvas->drawPath(result, fOpPaint[op]); |
| 70 |
canvas->drawPath(result, fOutlinePaint); |
| 71 |
canvas->restore(); |
| 72 |
xPos += 150; |
| 73 |
} |
| 74 |
yPos += 150; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
} // END FIDDLE |