Debian Codegraph @

tools/viewer/GradientsSlide.cpp

raw
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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkFont.h"
11 #include "include/effects/SkGradient.h"
12 #include "tools/fonts/FontToolUtils.h"
13 #include "tools/viewer/Slide.h"
14
15 #include <vector>
16
17 ///////////////////////////////////////////////////////////////////////////////
18
19 class GradientsSlide : public Slide {
20 public:
21 GradientsSlide() {
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<SkGradient::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<SkGradient::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;
69 SkPaint paint;
70 paint.setDither(fDither);
71
72 canvas->save();
73 canvas->translate(10, 10);
74
75 using CS = SkGradient::Interpolation::ColorSpace;
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 };
93 SkFont font = ToolUtils::DefaultFont();
94
95 for (const Config& config : kConfigs) {
96 fInterpolation.fColorSpace = config.fColorSpace;
97
98 paint.setShader(SkShaders::LinearGradient(pts,
99 {{fColors, {}, SkTileMode::kClamp}, fInterpolation}));
100 canvas->drawRect(r, paint);
101 canvas->drawSimpleText(config.fLabel, strlen(config.fLabel), SkTextEncoding::kUTF8,
102 266, 20, font, labelPaint);
103 canvas->translate(0, 42);
104 }
105 canvas->restore();
106 }
107
108 private:
109 std::vector<SkColor4f> fColors;
110 SkGradient::Interpolation fInterpolation;
111 bool fDither = false;
112 };
113
114 ///////////////////////////////////////////////////////////////////////////////
115
116 DEF_SLIDE( return new GradientsSlide(); )