Flutter Engine
The Flutter Engine
GradientsSlide.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "imgui.h"
10#include "include/core/SkFont.h"
13#include "tools/viewer/Slide.h"
14
15#include <vector>
16
17///////////////////////////////////////////////////////////////////////////////
18
19class GradientsSlide : public Slide {
20public:
22 fName = "Gradients";
23 fColors.push_back(SkColors::kBlue);
24 fColors.push_back(SkColors::kYellow);
25 }
26
27 void drawUI() {
28 ImGui::Begin("Gradient");
29
30 ImGui::Checkbox("Dither", &fDither);
31
32 bool premul = static_cast<bool>(fInterpolation.fInPremul);
33 ImGui::Checkbox("Premul", &premul);
34 fInterpolation.fInPremul = static_cast<SkGradientShader::Interpolation::InPremul>(premul);
35
36 int hm = static_cast<int>(fInterpolation.fHueMethod);
37 ImGui::Combo("Hue Method", &hm, "Shorter\0Longer\0Increasing\0Decreasing\0\0");
38 fInterpolation.fHueMethod = static_cast<SkGradientShader::Interpolation::HueMethod>(hm);
39
40 int removeIdx = -1;
41 for (int i = 0; i < (int)fColors.size(); ++i) {
42 ImGui::PushID(i);
43 if (ImGui::Button("X")) {
44 removeIdx = i;
45 }
46 ImGui::SameLine();
47 ImGui::ColorEdit4("##Color", fColors[i].vec());
48 ImGui::PopID();
49 }
50 if (removeIdx >= 0 && fColors.size() > 2) {
51 fColors.erase(fColors.begin() + removeIdx);
52 }
53
54 if (ImGui::Button("+")) {
55 fColors.push_back(SkColors::kBlack);
56 }
57
58 ImGui::End();
59 }
60
61 void draw(SkCanvas* canvas) override {
62 canvas->clear(SK_ColorGRAY);
63
64 this->drawUI();
65
66 SkPoint pts[2] = {{0, 0}, {256, 0}};
67 SkRect r = {0, 0, 256, 32};
68 SkPaint labelPaint;
70 paint.setDither(fDither);
71
72 canvas->save();
73 canvas->translate(10, 10);
74
76 struct Config {
77 CS fColorSpace;
78 const char* fLabel;
79 };
80 static const Config kConfigs[] = {
81 { CS::kDestination, "Destination" },
82 { CS::kSRGB, "sRGB" },
83 { CS::kSRGBLinear, "Linear sRGB" },
84 { CS::kLab, "CIELAB" },
85 { CS::kOKLab, "Oklab" },
86 { CS::kOKLabGamutMap, "OklabGamutMap" },
87 { CS::kLCH, "LCH" },
88 { CS::kOKLCH, "Oklch" },
89 { CS::kOKLCHGamutMap, "OklchGamutMap" },
90 { CS::kHSL, "HSL" },
91 { CS::kHWB, "HWB" },
92 };
94
95 for (const Config& config : kConfigs) {
96 fInterpolation.fColorSpace = config.fColorSpace;
97
98 paint.setShader(SkGradientShader::MakeLinear(pts, fColors.data(),
99 SkColorSpace::MakeSRGB(), nullptr,
100 (int)fColors.size(), SkTileMode::kClamp,
101 fInterpolation, nullptr));
102 canvas->drawRect(r, paint);
103 canvas->drawSimpleText(config.fLabel, strlen(config.fLabel), SkTextEncoding::kUTF8,
104 266, 20, font, labelPaint);
105 canvas->translate(0, 42);
106 }
107 canvas->restore();
108 }
109
110private:
111 std::vector<SkColor4f> fColors;
112 SkGradientShader::Interpolation fInterpolation;
113 bool fDither = false;
114};
115
116///////////////////////////////////////////////////////////////////////////////
117
118DEF_SLIDE( return new GradientsSlide(); )
constexpr SkColor SK_ColorGRAY
Definition: SkColor.h:113
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define DEF_SLIDE(code)
Definition: Slide.h:25
static uint32_t premul(uint32_t color)
void draw(SkCanvas *canvas) override
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clear(SkColor color)
Definition: SkCanvas.h:1199
int save()
Definition: SkCanvas.cpp:447
static sk_sp< SkColorSpace > MakeSRGB()
Definition: SkFont.h:35
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
Definition: Slide.h:29
SkString fName
Definition: Slide.h:54
const Paint & paint
Definition: color_source.cc:38
constexpr SkColor4f kBlack
Definition: SkColor.h:435
constexpr SkColor4f kBlue
Definition: SkColor.h:442
constexpr SkColor4f kYellow
Definition: SkColor.h:443
SkFont DefaultFont()
font
Font Metadata and Metrics.