2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
10 #include "SkGradientShader.h"
12 typedef sk_sp<SkShader> (*MakeShaderProc)(const SkColor[], int count, const SkSize&);
14 static sk_sp<SkShader> shader_linear(const SkColor colors[], int count, const SkSize& size) {
15 SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } };
16 return SkGradientShader::MakeLinear(pts, colors, nullptr, count, SkShader::kClamp_TileMode);
19 static sk_sp<SkShader> shader_radial(const SkColor colors[], int count, const SkSize& size) {
20 SkPoint center = { size.width()/2, size.height()/2 };
21 return SkGradientShader::MakeRadial(center, size.width()/2, colors, nullptr, count,
22 SkShader::kClamp_TileMode);
25 static sk_sp<SkShader> shader_conical(const SkColor colors[], int count, const SkSize& size) {
26 SkPoint center = { size.width()/2, size.height()/2 };
27 return SkGradientShader::MakeTwoPointConical(center, size.width()/64, center, size.width()/2,
28 colors, nullptr, count, SkShader::kClamp_TileMode);
31 static sk_sp<SkShader> shader_sweep(const SkColor colors[], int count, const SkSize& size) {
32 return SkGradientShader::MakeSweep(size.width()/2, size.height()/2, colors, nullptr, count);
35 class ShallowGradientGM : public skiagm::GM {
37 ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither)
40 fName.printf("shallow_gradient_%s", name);
45 SkString onShortName() override {
49 SkISize onISize() override {
50 return SkISize::Make(800, 800);
53 void onDraw(SkCanvas* canvas) override {
54 const SkColor colors[] = { sk_tool_utils::color_to_565(0xFF555555),
55 sk_tool_utils::color_to_565(0xFF444444) };
56 const int colorCount = SK_ARRAY_COUNT(colors);
58 SkRect r = { 0, 0, this->width(), this->height() };
59 SkSize size = SkSize::Make(r.width(), r.height());
62 paint.setShader(fProc(colors, colorCount, size));
63 paint.setDither(fDither);
64 canvas->drawRect(r, paint);
72 typedef skiagm::GM INHERITED;
75 ///////////////////////////////////////////////////////////////////////////////
77 DEF_GM( return new ShallowGradientGM(shader_linear, "linear", true); )
78 DEF_GM( return new ShallowGradientGM(shader_radial, "radial", true); )
79 DEF_GM( return new ShallowGradientGM(shader_conical, "conical", true); )
80 DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep", true); )
82 DEF_GM( return new ShallowGradientGM(shader_linear, "linear_nodither", false); )
83 DEF_GM( return new ShallowGradientGM(shader_radial, "radial_nodither", false); )
84 DEF_GM( return new ShallowGradientGM(shader_conical, "conical_nodither", false); )
85 DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep_nodither", false); )