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/SkGradientShader.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: GradientsSlide()21 GradientsSlide() { 22 fName = "Gradients"; 23 fColors.push_back(SkColors::kBlue); 24 fColors.push_back(SkColors::kYellow); 25 } 26 drawUI()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 draw(SkCanvas * canvas)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 = SkGradientShader::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(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 110 private: 111 std::vector<SkColor4f> fColors; 112 SkGradientShader::Interpolation fInterpolation; 113 bool fDither = false; 114 }; 115 116 /////////////////////////////////////////////////////////////////////////////// 117 118 DEF_SLIDE( return new GradientsSlide(); ) 119