• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(picture_shader, 256, 256, false, 5) {
draw_centered(const char * s,const SkFont & font,SkColor color,SkPoint xy,SkCanvas * c)5 static void draw_centered(
6         const char* s, const SkFont& font, SkColor color, SkPoint xy, SkCanvas* c) {
7     sk_sp<SkTextBlob> b = SkTextBlob::MakeFromString(s, font);
8     xy -= SkPoint{b->bounds().centerX(), b->bounds().centerY()};
9     SkPaint p;
10     p.setColor(color);
11     c->drawTextBlob(b.get(), xy.x(), xy.y(), p);
12 }
13 
from_polar_deg(float r,float d)14 SkPoint from_polar_deg(float r, float d) {
15     float a = d * 0.017453292519943295;
16     return {r * cosf(a), r * sinf(a)};
17 }
18 
draw_wheel(SkCanvas * c)19 void draw_wheel(SkCanvas* c) {
20     const SkScalar scale = 512;
21     SkAutoCanvasRestore autoCanvasRestore(c, true);
22     c->translate(0.5f * scale, 0.5f * scale);
23     SkPaint p;
24     p.setAntiAlias(true);
25     p.setColor(SK_ColorWHITE);
26     c->drawCircle(0.0f, 0.0f, scale * 0.475f, p);
27 
28     const SkColor sweep_colors[] = {SK_ColorRED,  SK_ColorYELLOW,  SK_ColorGREEN, SK_ColorCYAN,
29                                     SK_ColorBLUE, SK_ColorMAGENTA, SK_ColorRED};
30     SkMatrix rot;
31     rot.setRotate(90.0f);
32     p.setShader(SkGradientShader::MakeSweep(0, 0, sweep_colors, NULL,
33                                             SK_ARRAY_COUNT(sweep_colors), 0, &rot));
34     p.setStrokeWidth(0.05f * scale);
35     p.setStyle(SkPaint::kStroke_Style);
36     c->drawCircle(0.0f, 0.0f, 0.475f * scale, p);
37 
38     SkFont f(nullptr, 0.28125f * scale);
39     draw_centered("K", f, SK_ColorBLACK, {0.0f, 0.0f}, c);
40     draw_centered("R", f, SK_ColorRED, from_polar_deg(0.3f * scale, 90), c);
41     draw_centered("G", f, SK_ColorGREEN, from_polar_deg(0.3f * scale, 210), c);
42     draw_centered("B", f, SK_ColorBLUE, from_polar_deg(0.3f * scale, 330), c);
43     draw_centered("C", f, SK_ColorCYAN, from_polar_deg(0.3f * scale, 270), c);
44     draw_centered("M", f, SK_ColorMAGENTA, from_polar_deg(0.3f * scale, 30), c);
45     draw_centered("Y", f, SK_ColorYELLOW, from_polar_deg(0.3f * scale, 150), c);
46 }
47 
draw(SkCanvas * canvas)48 void draw(SkCanvas* canvas) {
49     canvas->clear(SK_ColorWHITE);
50     SkMatrix matrix;
51     matrix.setScale(0.25f, 0.25f);
52     matrix.preRotate(30.0f);
53     SkPaint paint;
54     SkPictureRecorder rec;
55     draw_wheel(rec.beginRecording(512, 512));
56     paint.setShader(rec.finishRecordingAsPicture()->makeShader(
57             SkTileMode::kRepeat, SkTileMode::kRepeat, SkFilterMode::kNearest, &matrix, nullptr));
58     canvas->drawPaint(paint);
59 }
60 }  // END FIDDLE
61