Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
shallowgradient.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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 "gm/gm.h"
13#include "include/core/SkRect.h"
16#include "include/core/SkSize.h"
21
22typedef sk_sp<SkShader> (*MakeShaderProc)(const SkColor[], int count, const SkSize&);
23
24static sk_sp<SkShader> shader_linear(const SkColor colors[], int count, const SkSize& size) {
25 SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } };
26 return SkGradientShader::MakeLinear(pts, colors, nullptr, count, SkTileMode::kClamp);
27}
28
29static sk_sp<SkShader> shader_radial(const SkColor colors[], int count, const SkSize& size) {
30 SkPoint center = { size.width()/2, size.height()/2 };
31 return SkGradientShader::MakeRadial(center, size.width()/2, colors, nullptr, count,
33}
34
35static sk_sp<SkShader> shader_conical(const SkColor colors[], int count, const SkSize& size) {
36 SkPoint center = { size.width()/2, size.height()/2 };
37 return SkGradientShader::MakeTwoPointConical(center, size.width()/64, center, size.width()/2,
38 colors, nullptr, count, SkTileMode::kClamp);
39}
40
41static sk_sp<SkShader> shader_sweep(const SkColor colors[], int count, const SkSize& size) {
42 return SkGradientShader::MakeSweep(size.width()/2, size.height()/2, colors, nullptr, count);
43}
44
46public:
47 ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither)
48 : fProc(proc), fName(name), fDither(dither) {}
49
50private:
51 MakeShaderProc fProc;
52 const char* fName;
53 bool fDither;
54
55 SkString getName() const override {
56 return SkStringPrintf("shallow_gradient_%s%s", fName, fDither ? "" : "_nodither");
57 }
58
59 SkISize getISize() override { return {800, 800}; }
60
61 void onDraw(SkCanvas* canvas) override {
62 const SkColor colors[] = { 0xFF555555, 0xFF444444 };
63 const int colorCount = std::size(colors);
64
65 SkRect r = { 0, 0, this->width(), this->height() };
66 SkSize size = SkSize::Make(r.width(), r.height());
67
69 paint.setShader(fProc(colors, colorCount, size));
70 paint.setDither(fDither);
71 canvas->drawRect(r, paint);
72 }
73};
74
75///////////////////////////////////////////////////////////////////////////////
76
77#define M(PROC, DITHER) DEF_GM( return new ShallowGradientGM(shader_ ## PROC, #PROC, DITHER); )
78M(linear, true)
79M(radial, true)
80M(conical, true)
81M(sweep, true)
82
83M(linear, false)
84M(radial, false)
85M(conical, false)
86M(sweep, false)
87#undef M
const char * fName
int count
IsFiniteProc fProc
uint32_t SkColor
Definition SkColor.h:37
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
static SkScalar center(float pos0, float pos1)
SkString getName() const override
ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither)
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
void drawRect(const SkRect &rect, const SkPaint &paint)
static sk_sp< SkShader > MakeTwoPointConical(const SkPoint &start, SkScalar startRadius, const SkPoint &end, SkScalar endRadius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
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)
SkScalar width()
Definition gm.h:159
SkScalar height()
Definition gm.h:162
const Paint & paint
const char * name
Definition fuchsia.cc:50
static sk_sp< SkShader > shader_linear(const SkColor colors[], int count, const SkSize &size)
static sk_sp< SkShader > shader_conical(const SkColor colors[], int count, const SkSize &size)
#define M(PROC, DITHER)
static sk_sp< SkShader > shader_radial(const SkColor colors[], int count, const SkSize &size)
static sk_sp< SkShader > shader_sweep(const SkColor colors[], int count, const SkSize &size)
sk_sp< SkShader >(* MakeShaderProc)(const SkColor[], int count, const SkSize &)
constexpr float height() const
Definition SkRect.h:769
constexpr float width() const
Definition SkRect.h:762
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition SkSize.h:56
static sk_sp< SkShader > linear(sk_sp< SkShader > shader)