Index: gm/hardstop_gradients.cpp |
diff --git a/gm/hardstop_gradients.cpp b/gm/hardstop_gradients.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f2c11d2bdb4622ddc9e87d3c12a3a9a732c67d7b |
--- /dev/null |
+++ b/gm/hardstop_gradients.cpp |
@@ -0,0 +1,149 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+/* |
+ * This GM presents a variety of different gradients with different |
+ * tile modes. Each entry in the table is a rectangle with a linear |
+ * gradient that spans from its left edge to its right edge. The rows |
+ * in the table represent different color/position configurations, |
+ * while the columns in the table represent different tile modes. In |
+ * order to highlight the differences between tile modes, the gradient |
+ * 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
|
+ * |
+ * | Clamp Repeat Mirror |
+ * _____________________|___________________________________________ |
+ * 2-color | rect00 rect01 rect02 |
+ * 3-color | rect10 rect11 rect12 |
+ * 5-color hard stop | rect20 rect21 rect22 |
+ * 5-color edge case 1 | rect30 rect31 rect32 |
+ * 5-color edge case 2 | rect40 rect41 rect42 |
+ * |
+ * 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.
|
+ * are the same, while the FIRST two t-values in edge case 2 are the same. |
+ */ |
+ |
+#include "gm.h" |
+ |
+#include "SkGradientShader.h" |
+ |
+const int WIDTH = 500; |
+const int HEIGHT = 500; |
+ |
+const int NUM_ROWS = 5; |
+const int NUM_COLS = 3; |
+ |
+const int CELL_WIDTH = WIDTH / NUM_COLS; |
+const int CELL_HEIGHT = HEIGHT / NUM_ROWS; |
+ |
+const int PAD_WIDTH = 3; |
+const int PAD_HEIGHT = 3; |
+ |
+const int RECT_WIDTH = CELL_WIDTH - (2 * PAD_WIDTH); |
+const int RECT_HEIGHT = CELL_HEIGHT - (2 * PAD_HEIGHT); |
+ |
+static void shadeRect(SkCanvas* canvas, sk_sp<SkShader> shader, int cell_row, int cell_col) { |
+ SkPaint paint; |
+ paint.setShader(shader); |
+ |
+ SkRect rect = SkRect::MakeXYWH(cell_col * CELL_WIDTH + PAD_WIDTH, |
+ cell_row * CELL_HEIGHT + PAD_HEIGHT, |
+ RECT_WIDTH, |
+ RECT_HEIGHT); |
+ |
+ canvas->drawRect(rect, paint); |
+} |
+ |
+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.
|
+ const int X_OFFSET = 30; |
+ |
+ int x0 = cell_col * CELL_WIDTH + PAD_WIDTH + X_OFFSET; |
+ int x1 = (cell_col+1) * CELL_WIDTH - PAD_WIDTH - X_OFFSET; |
+ int y = cell_row * CELL_HEIGHT + PAD_HEIGHT + RECT_HEIGHT/2; |
+ |
+ points[0] = SkPoint::Make(x0, y); |
+ points[1] = SkPoint::Make(x1, y); |
+} |
+ |
+static int getCount(int cell_row) { |
tomhudson
2016/06/22 15:28:49
Count of what?
static int numGradientColors()?
|
+ if (cell_row == 0) |
+ return 2; |
+ else if (cell_row == 1) |
+ return 3; |
+ else |
+ return 5; |
+} |
+ |
+static SkShader::TileMode getTileMode(int cell_col) { |
tomhudson
2016/06/22 15:28:49
This could be an array? Or would that be uglier?
|
+ if (cell_col == 0) |
+ return SkShader::kClamp_TileMode; |
+ else if (cell_col == 1) |
+ return SkShader::kRepeat_TileMode; |
+ else |
+ return SkShader::kMirror_TileMode; |
+} |
+ |
+class HardstopGradientShaderGM : public skiagm::GM { |
+public: |
+ HardstopGradientShaderGM() { |
+ |
+ } |
+ |
+protected: |
+ SkString onShortName() override { |
+ return SkString("hardstop_gradients"); |
+ } |
+ |
+ SkISize onISize() override { |
+ return SkISize::Make(512, 512); |
+ } |
+ |
+ void onDraw(SkCanvas* canvas) { |
+ SkPoint points[2]; |
+ |
+ SkColor colors[] = { |
+ SK_ColorRED, |
+ SK_ColorGREEN, |
+ SK_ColorBLUE, |
+ SK_ColorYELLOW, |
+ SK_ColorMAGENTA, |
+ }; |
+ |
+ SkScalar row3[] = {0.00f, 0.25f, 0.50f, 0.50f, 1.00f}; |
+ SkScalar row4[] = {0.00f, 0.25f, 0.50f, 1.00f, 1.00f}; |
+ SkScalar row5[] = {0.00f, 0.00f, 0.50f, 0.50f, 1.00f}; |
+ |
+ SkScalar* positions[] = { |
+ nullptr, |
+ nullptr, |
+ row3, |
+ row4, |
+ row5, |
+ }; |
+ |
+ for (int cell_row = 0; cell_row < NUM_ROWS; cell_row++) { |
+ for (int cell_col = 0; cell_col < NUM_COLS; cell_col++) { |
+ getPoints(cell_row, cell_col, points); |
+ |
+ auto shader = SkGradientShader::MakeLinear( |
+ points, |
+ colors, |
+ positions[cell_row], |
+ getCount(cell_row), |
+ getTileMode(cell_col), |
+ 0, |
+ nullptr); |
+ |
+ shadeRect(canvas, shader, cell_row, cell_col); |
+ } |
+ } |
+ } |
+ |
+private: |
+ typedef skiagm::GM INHERITED; |
+}; |
+ |
+DEF_GM(return new HardstopGradientShaderGM;) |