Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
pathops.cpp
Go to the documentation of this file.
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.
4REG_FIDDLE(pathops, 1000, 600, false, 0) {
5void makePaint(SkPaint * paint, SkColor color) {
6 paint->setAntiAlias(true);
8 paint->setColor(color);
9}
10
11SkColor 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
21void draw(SkCanvas* canvas) {
22 SkPaint fOnePaint;
23 SkPaint fTwoPaint;
24 SkPaint fOutlinePaint;
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 SkPath one, two;
41 int yPos = 0;
42 for (int oneFill = 0; oneFill <= 1; ++oneFill) {
43 SkPathFillType oneF =
45 for (int twoFill = 0; twoFill <= 1; ++twoFill) {
46 SkPathFillType twoF =
48 one.reset();
49 one.setFillType(oneF);
50
51 one.moveTo(10, 10);
52 one.conicTo(0, 90, 50, 50, 3);
53 one.conicTo(90, 0, 90, 90, 2);
54 one.close();
55
56 two.reset();
57 two.setFillType(twoF);
58 two.addRect(40, 40, 100, 100);
59 canvas->save();
60 canvas->translate(0, SkIntToScalar(yPos));
61 canvas->clipRect(SkRect::MakeWH(110, 110), SkClipOp::kIntersect, true);
62 canvas->drawPath(one, fOnePaint);
63 canvas->drawPath(one, fOutlinePaint);
64 canvas->drawPath(two, fTwoPaint);
65 canvas->drawPath(two, fOutlinePaint);
66 canvas->restore();
67 int xPos = 150;
68 for (int op = kDifference_SkPathOp; op <= kReverseDifference_SkPathOp; ++op) {
70 Op(one, two, (SkPathOp)op, &result);
71 canvas->save();
72 canvas->translate(SkIntToScalar(xPos), SkIntToScalar(yPos));
73 canvas->clipRect(SkRect::MakeWH(110, 110), SkClipOp::kIntersect, true);
74 canvas->drawPath(result, fOpPaint[op]);
75 canvas->drawPath(result, fOutlinePaint);
76 canvas->restore();
77 xPos += 150;
78 }
79 yPos += 150;
80 }
81 }
82}
83
84} // END FIDDLE
SkColor4f color
pathops
uint32_t SkColor
Definition SkColor.h:37
SkPathOp
Definition SkPathOps.h:22
@ kReverseDifference_SkPathOp
subtract the first path from the op path
Definition SkPathOps.h:27
@ kDifference_SkPathOp
subtract the op path from the first path
Definition SkPathOps.h:23
@ kIntersect_SkPathOp
intersect the two paths
Definition SkPathOps.h:24
@ kUnion_SkPathOp
union (inclusive-or) the two paths
Definition SkPathOps.h:25
@ kXOR_SkPathOp
exclusive-or the two paths
Definition SkPathOps.h:26
SkPathFillType
Definition SkPathTypes.h:11
#define SkIntToScalar(x)
Definition SkScalar.h:57
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition aaclip.cpp:27
void * getPixels() const
Definition SkBitmap.h:283
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
int save()
Definition SkCanvas.cpp:451
void drawPath(const SkPath &path, const SkPaint &paint)
void setStyle(Style style)
Definition SkPaint.cpp:105
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
SkPath & moveTo(SkScalar x, SkScalar y)
Definition SkPath.cpp:678
void setFillType(SkPathFillType ft)
Definition SkPath.h:235
SkPath & reset()
Definition SkPath.cpp:360
SkPath & conicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar w)
Definition SkPath.cpp:756
SkPath & close()
Definition SkPath.cpp:813
const Paint & paint
#define REG_FIDDLE(NAME, W, H, TEXT, I)
Definition examples.h:60
GAsyncResult * result
static SkColor blend(SkColor dst, SkColor src, void(*mode)(float, float, float, float *, float *, float *))
Definition hsl.cpp:142
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609