Kevin Lubick | 4765da4 | 2020-03-18 13:21:18 -0400 | [diff] [blame] | 1 | // Copyright 2020 Google LLC. |
| 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 | #include "tools/fiddle/examples.h" |
| 4 | REG_FIDDLE(GradientShader_MakeLinear, 201, 201, false, 0) { |
| 5 | void draw(SkCanvas* canvas) { |
| 6 | // This fiddle draws 4 instances of a LinearGradient, demonstrating |
| 7 | // how the local matrix affects the gradient as well as the flag |
| 8 | // which controls unpremul vs premul color interpolation. |
| 9 | |
| 10 | SkPaint strokePaint; |
| 11 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 12 | strokePaint.setColor(SK_ColorBLACK); |
| 13 | |
| 14 | SkPaint p; |
| 15 | p.setStyle(SkPaint::kFill_Style); |
| 16 | |
| 17 | SkColor transparentGreen = SkColorSetARGB(0, 0, 255, 255); |
| 18 | SkColor colors[] = { transparentGreen, SK_ColorBLUE, SK_ColorRED }; |
| 19 | SkScalar positions[] = { 0.0, 0.65, 1.0 }; |
| 20 | |
| 21 | for (int i = 0; i < 4; i++) { |
Kevin Lubick | 6f60a85 | 2020-03-23 09:20:38 -0400 | [diff] [blame] | 22 | SkScalar blockX = (i % 2) * 100; |
| 23 | SkScalar blockY = (i / 2) * 100; |
Kevin Lubick | 4765da4 | 2020-03-18 13:21:18 -0400 | [diff] [blame] | 24 | SkPoint pts[] = { {blockX, blockY}, {blockX + 50, blockY + 100} }; |
| 25 | |
| 26 | int flags = 0; // interpolate colors in unpremul |
| 27 | if (i % 2 == 1) { |
| 28 | // right column will have premul |
| 29 | flags = SkGradientShader::Flags::kInterpolateColorsInPremul_Flag; |
| 30 | } |
| 31 | |
Kevin Lubick | 6f60a85 | 2020-03-23 09:20:38 -0400 | [diff] [blame] | 32 | SkMatrix matr = SkMatrix::I(); |
Kevin Lubick | 4765da4 | 2020-03-18 13:21:18 -0400 | [diff] [blame] | 33 | if (i / 2 == 1) { |
| 34 | // bottom row will be rotated 45 degrees. |
Kevin Lubick | 6f60a85 | 2020-03-23 09:20:38 -0400 | [diff] [blame] | 35 | matr.setRotate(45, blockX, blockY); |
Kevin Lubick | 4765da4 | 2020-03-18 13:21:18 -0400 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | auto lgs = SkGradientShader::MakeLinear( |
Kevin Lubick | 6f60a85 | 2020-03-23 09:20:38 -0400 | [diff] [blame] | 39 | pts, colors, positions, 3, SkTileMode::kMirror, |
| 40 | flags, &matr); |
Kevin Lubick | 4765da4 | 2020-03-18 13:21:18 -0400 | [diff] [blame] | 41 | |
| 42 | p.setShader(lgs); |
| 43 | auto r = SkRect::MakeLTRB(blockX, blockY, blockX + 100, blockY + 100); |
| 44 | canvas->drawRect(r, p); |
| 45 | canvas->drawRect(r, strokePaint); |
| 46 | } |
| 47 | } |
| 48 | } // END FIDDLE |