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 "SkGradientShader.h"
11 typedef SkShader* (*MakeShaderProc)(const SkColor[], int count, const SkSize&);
13 static SkShader* shader_linear(const SkColor colors[], int count, const SkSize& size) {
14 SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } };
15 return SkGradientShader::CreateLinear(pts, colors, NULL, count,
16 SkShader::kClamp_TileMode);
19 static SkShader* shader_radial(const SkColor colors[], int count, const SkSize& size) {
20 SkPoint center = { size.width()/2, size.height()/2 };
21 return SkGradientShader::CreateRadial(center, size.width()/2, colors, NULL, count,
22 SkShader::kClamp_TileMode);
25 static SkShader* shader_conical(const SkColor colors[], int count, const SkSize& size) {
26 SkPoint center = { size.width()/2, size.height()/2 };
27 return SkGradientShader::CreateTwoPointConical(center, size.width()/64,
28 center, size.width()/2,
30 SkShader::kClamp_TileMode);
33 static SkShader* shader_sweep(const SkColor colors[], int count, const SkSize& size) {
34 return SkGradientShader::CreateSweep(size.width()/2, size.height()/2,
38 class ShallowGradientGM : public skiagm::GM {
40 ShallowGradientGM(MakeShaderProc proc, const char name[]) : fProc(proc) {
41 fName.printf("shallow_gradient_%s", name);
46 SkString onShortName() override {
50 SkISize onISize() override {
51 return SkISize::Make(800, 800);
54 void onDraw(SkCanvas* canvas) override {
55 const SkColor colors[] = { 0xFF555555, 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))->unref();
63 canvas->drawRect(r, paint);
70 typedef skiagm::GM INHERITED;
73 ///////////////////////////////////////////////////////////////////////////////
75 DEF_GM( return new ShallowGradientGM(shader_linear, "linear"); )
76 DEF_GM( return new ShallowGradientGM(shader_radial, "radial"); )
77 DEF_GM( return new ShallowGradientGM(shader_conical, "conical"); )
78 DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep"); )