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 inset from either side of the rectangle. | |
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 t-values in edge case 1 are the same, while the FIRST | |
26 * 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 createGradientPoints(int cell_row, int cell_col, SkPoint points[2]) { | |
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 class HardstopGradientShaderGM : public skiagm::GM { | |
72 public: | |
73 HardstopGradientShaderGM() { | |
74 | |
75 } | |
76 | |
77 protected: | |
78 SkString onShortName() override { | |
79 return SkString("hardstop_gradients"); | |
80 } | |
81 | |
82 SkISize onISize() override { | |
83 return SkISize::Make(512, 512); | |
84 } | |
85 | |
86 void onDraw(SkCanvas* canvas) { | |
87 SkPoint points[2]; | |
88 | |
89 SkColor colors[] = { | |
90 SK_ColorRED, | |
91 SK_ColorGREEN, | |
92 SK_ColorBLUE, | |
93 SK_ColorYELLOW, | |
94 SK_ColorMAGENTA, | |
95 }; | |
96 | |
97 SkScalar row3[] = {0.00f, 0.25f, 0.50f, 0.50f, 1.00f}; | |
98 SkScalar row4[] = {0.00f, 0.25f, 0.50f, 1.00f, 1.00f}; | |
99 SkScalar row5[] = {0.00f, 0.00f, 0.50f, 0.50f, 1.00f}; | |
100 | |
101 SkScalar* positions[] = { | |
102 nullptr, | |
103 nullptr, | |
104 row3, | |
105 row4, | |
106 row5, | |
107 }; | |
108 | |
109 int num_gradient_colors[] = { | |
110 2, | |
111 3, | |
112 5, | |
113 5, | |
114 5, | |
115 }; | |
116 | |
117 SkShader::TileMode tilemodes[] = { | |
118 SkShader::kClamp_TileMode, | |
119 SkShader::kRepeat_TileMode, | |
120 SkShader::kMirror_TileMode, | |
121 }; | |
122 | |
123 for (int cell_row = 0; cell_row < NUM_ROWS; cell_row++) { | |
tomhudson
2016/06/22 15:54:06
Nit: revisit this function after reading https://s
| |
124 for (int cell_col = 0; cell_col < NUM_COLS; cell_col++) { | |
125 createGradientPoints(cell_row, cell_col, points); | |
126 | |
127 auto shader = SkGradientShader::MakeLinear( | |
128 points, | |
129 colors, | |
130 positions[cell_row], | |
131 num_gradient_colors[cell_row], | |
132 tilemodes[cell_col], | |
133 0, | |
134 nullptr); | |
135 | |
136 shadeRect(canvas, shader, cell_row, cell_col); | |
137 } | |
138 } | |
139 } | |
140 | |
141 private: | |
142 typedef skiagm::GM INHERITED; | |
143 }; | |
144 | |
145 DEF_GM(return new HardstopGradientShaderGM;) | |
OLD | NEW |