OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 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 /* | |
9 * This GM presents a variety of different gradients with different | |
10 * tile modes. Each entry in the table is a rectangle with a linear | |
11 * gradient that spans from its left edge to its right edge. The rows | |
12 * in the table represent different color/position configurations, | |
13 * while the columns in the table represent different tile modes. In | |
14 * order to highlight the differences between tile modes, the gradient | |
15 * starts and ends at 30 pixel offsets on either side of the rectangle. | |
tomhudson
2016/06/22 15:28:49
starts and ends 30 pixels inset from either end of
| |
16 * | |
17 * | Clamp Repeat Mirror | |
18 * _____________________|___________________________________________ | |
19 * 2-color | rect00 rect01 rect02 | |
20 * 3-color | rect10 rect11 rect12 | |
21 * 5-color hard stop | rect20 rect21 rect22 | |
22 * 5-color edge case 1 | rect30 rect31 rect32 | |
23 * 5-color edge case 2 | rect40 rect41 rect42 | |
24 * | |
25 * The last two rows are edge cases; the LAST two t-values in edge case 1 | |
tomhudson
2016/06/22 15:28:49
Nit: omit the first 7 words.
| |
26 * are the same, while the FIRST two t-values in edge case 2 are the same. | |
27 */ | |
28 | |
29 #include "gm.h" | |
30 | |
31 #include "SkGradientShader.h" | |
32 | |
33 const int WIDTH = 500; | |
34 const int HEIGHT = 500; | |
35 | |
36 const int NUM_ROWS = 5; | |
37 const int NUM_COLS = 3; | |
38 | |
39 const int CELL_WIDTH = WIDTH / NUM_COLS; | |
40 const int CELL_HEIGHT = HEIGHT / NUM_ROWS; | |
41 | |
42 const int PAD_WIDTH = 3; | |
43 const int PAD_HEIGHT = 3; | |
44 | |
45 const int RECT_WIDTH = CELL_WIDTH - (2 * PAD_WIDTH); | |
46 const int RECT_HEIGHT = CELL_HEIGHT - (2 * PAD_HEIGHT); | |
47 | |
48 static void shadeRect(SkCanvas* canvas, sk_sp<SkShader> shader, int cell_row, in t cell_col) { | |
49 SkPaint paint; | |
50 paint.setShader(shader); | |
51 | |
52 SkRect rect = SkRect::MakeXYWH(cell_col * CELL_WIDTH + PAD_WIDTH, | |
53 cell_row * CELL_HEIGHT + PAD_HEIGHT, | |
54 RECT_WIDTH, | |
55 RECT_HEIGHT); | |
56 | |
57 canvas->drawRect(rect, paint); | |
58 } | |
59 | |
60 static void getPoints(int cell_row, int cell_col, SkPoint points[2]) { | |
tomhudson
2016/06/22 15:28:49
Invest in better naming. Or maybe a comment.
| |
61 const int X_OFFSET = 30; | |
62 | |
63 int x0 = cell_col * CELL_WIDTH + PAD_WIDTH + X_OFFSET; | |
64 int x1 = (cell_col+1) * CELL_WIDTH - PAD_WIDTH - X_OFFSET; | |
65 int y = cell_row * CELL_HEIGHT + PAD_HEIGHT + RECT_HEIGHT/2; | |
66 | |
67 points[0] = SkPoint::Make(x0, y); | |
68 points[1] = SkPoint::Make(x1, y); | |
69 } | |
70 | |
71 static int getCount(int cell_row) { | |
tomhudson
2016/06/22 15:28:49
Count of what?
static int numGradientColors()?
| |
72 if (cell_row == 0) | |
73 return 2; | |
74 else if (cell_row == 1) | |
75 return 3; | |
76 else | |
77 return 5; | |
78 } | |
79 | |
80 static SkShader::TileMode getTileMode(int cell_col) { | |
tomhudson
2016/06/22 15:28:49
This could be an array? Or would that be uglier?
| |
81 if (cell_col == 0) | |
82 return SkShader::kClamp_TileMode; | |
83 else if (cell_col == 1) | |
84 return SkShader::kRepeat_TileMode; | |
85 else | |
86 return SkShader::kMirror_TileMode; | |
87 } | |
88 | |
89 class HardstopGradientShaderGM : public skiagm::GM { | |
90 public: | |
91 HardstopGradientShaderGM() { | |
92 | |
93 } | |
94 | |
95 protected: | |
96 SkString onShortName() override { | |
97 return SkString("hardstop_gradients"); | |
98 } | |
99 | |
100 SkISize onISize() override { | |
101 return SkISize::Make(512, 512); | |
102 } | |
103 | |
104 void onDraw(SkCanvas* canvas) { | |
105 SkPoint points[2]; | |
106 | |
107 SkColor colors[] = { | |
108 SK_ColorRED, | |
109 SK_ColorGREEN, | |
110 SK_ColorBLUE, | |
111 SK_ColorYELLOW, | |
112 SK_ColorMAGENTA, | |
113 }; | |
114 | |
115 SkScalar row3[] = {0.00f, 0.25f, 0.50f, 0.50f, 1.00f}; | |
116 SkScalar row4[] = {0.00f, 0.25f, 0.50f, 1.00f, 1.00f}; | |
117 SkScalar row5[] = {0.00f, 0.00f, 0.50f, 0.50f, 1.00f}; | |
118 | |
119 SkScalar* positions[] = { | |
120 nullptr, | |
121 nullptr, | |
122 row3, | |
123 row4, | |
124 row5, | |
125 }; | |
126 | |
127 for (int cell_row = 0; cell_row < NUM_ROWS; cell_row++) { | |
128 for (int cell_col = 0; cell_col < NUM_COLS; cell_col++) { | |
129 getPoints(cell_row, cell_col, points); | |
130 | |
131 auto shader = SkGradientShader::MakeLinear( | |
132 points, | |
133 colors, | |
134 positions[cell_row], | |
135 getCount(cell_row), | |
136 getTileMode(cell_col), | |
137 0, | |
138 nullptr); | |
139 | |
140 shadeRect(canvas, shader, cell_row, cell_col); | |
141 } | |
142 } | |
143 } | |
144 | |
145 private: | |
146 typedef skiagm::GM INHERITED; | |
147 }; | |
148 | |
149 DEF_GM(return new HardstopGradientShaderGM;) | |
OLD | NEW |