• 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(Color_Wheel, 256, 256, false, 0) {
draw_color_wheel(SkCanvas * canvas,float scale)5 void draw_color_wheel(SkCanvas* canvas, float scale) {
6     const float stroke = scale * 3 / 64;
7     const float size = scale * 9 / 32;
8     SkAutoCanvasRestore autoCanvasRestore(canvas, true);
9     canvas->translate(0.5f * scale, 0.5f * scale);
10     canvas->clear(0);
11     canvas->drawCircle({0, 0}, (scale - stroke) * 0.5f, SkPaint(SkColors::kWhite));
12     {
13         SkPaint sweep;
14         const SkMatrix rotate = SkMatrix::MakeAll(0, -1, 0, 1, 0, 0, 0, 0, 1);
15         constexpr unsigned kColorCount = 7;
16         static const SkColor4f kColors[kColorCount] = {
17             SkColors::kRed,  SkColors::kYellow,  SkColors::kGreen, SkColors::kCyan,
18             SkColors::kBlue, SkColors::kMagenta, SkColors::kRed};
19         sweep.setShader(SkGradientShader::MakeSweep(0, 0, kColors, nullptr, nullptr,
20                                                     kColorCount, 0, &rotate));
21         sweep.setStyle(SkPaint::kStroke_Style);
22         sweep.setStrokeWidth(stroke);
23         sweep.setAntiAlias(true);
24         canvas->drawCircle({0, 0}, (scale - stroke) * 0.5f, sweep);
25     }
26     sk_sp<SkFontMgr> fontMgr = SkFontMgr::RefDefault();
27     SkFont font(fontMgr->legacyMakeTypeface(nullptr, SkFontStyle::Bold()), size);
28     const struct { const char* str; float radius; float angle; SkColor4f color;} kLetters[] = {
29         {"K", 0,   0, SkColors::kBlack},
30         {"R", 1,  90, SkColors::kRed},
31         {"Y", 1, 150, SkColors::kYellow},
32         {"G", 1, 210, SkColors::kGreen},
33         {"C", 1, 270, SkColors::kCyan},
34         {"B", 1, 330, SkColors::kBlue},
35         {"M", 1,  30, SkColors::kMagenta},
36     };
37     for (const auto& v : kLetters) {
38         SkRect bnds;
39         font.measureText(v.str, strlen(v.str), SkTextEncoding::kUTF8, &bnds);
40         constexpr double pi_over_180 = 3.141592653589793 / 180;
41         float x = (float)(0.3 * scale * v.radius * cos(pi_over_180 * v.angle) - bnds.centerX());
42         float y = (float)(0.3 * scale * v.radius * sin(pi_over_180 * v.angle) - bnds.centerY());
43         canvas->drawString(v.str, x, y, font, SkPaint(v.color));
44     }
45 }
draw(SkCanvas * canvas)46 void draw(SkCanvas* canvas) { draw_color_wheel(canvas, 256); }
47 }  // END FIDDLE
48