blob: 9fdcfee6835f63a554168fbe968c2fd7a3b1ddac [file] [log] [blame]
Jim Van Verth744cbb32017-12-19 13:02:38 -05001/*
2* Copyright 2017 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.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11#include "SkShadowUtils.h"
12
13enum BackgroundMode {
14 kLightBackground,
15 kDarkBackground
16};
17
18static constexpr int kW = 1225;
19static constexpr int kH = 550;
20
21void draw_content(SkCanvas* canvas, BackgroundMode mode) {
22 const SkScalar kLightWidth = 600;
23 const SkScalar kAmbientAlpha = 0.03f;
24 const SkScalar kSpotAlpha = 0.25f;
25
26 const SkColor kColors[30] = {
27 // purples
28 0xFF3A0072, 0xFF5D0099, 0xFF7F12B2, 0xFFA02AD1, 0xFFC245E5,
29 0xFFE95AF9, 0xFFFC79F0, 0xFFFDA6F0, 0xFFFFCCF8, 0xFFFFE1F9,
30 // oranges
31 0xFFEA3200, 0xFFFF4E00, 0xFFFF7300, 0xFFFF9100, 0xFFFFB000,
32 0xFFFFCE00, 0xFFFFE000, 0xFFFFF64D, 0xFFFFF98F, 0xFFFFFBCC,
33 // teals
34 0xFF004D51, 0xFF066266, 0xFF057F7F, 0xFF009999, 0xFF00B2B2,
35 0xFF15CCBE, 0xFF25E5CE, 0xFF2CFFE0, 0xFF80FFEA, 0xFFB3FFF0
36 };
37
38 SkPaint paint;
39 paint.setAntiAlias(true);
40 if (mode == kDarkBackground) {
41 canvas->drawColor(0xFF111111);
42 } else {
43 canvas->drawColor(0xFFEAEAEA);
44 }
45
46 SkPath path;
47 path.addRect(SkRect::MakeXYWH(-50, -50, 100, 100));
48
49 SkPoint3 lightPos = { 75, -400, 600 };
50 SkPoint3 zPlaneParams = SkPoint3::Make(0, 0, 16);
51 SkScalar yPos = 75;
52
53 for (int row = 0; row < 3; ++row) {
54 lightPos.fX = 75;
55 SkScalar xPos = 75;
56 for (int col = 0; col < 10; ++col) {
Jim Van Vertha5566842018-02-22 10:58:34 -050057 SkColor color = kColors[10 * row + col];
58 paint.setColor(color);
Jim Van Verth744cbb32017-12-19 13:02:38 -050059
60 canvas->save();
61 canvas->translate(xPos, yPos);
Jim Van Vertha5566842018-02-22 10:58:34 -050062
63 SkColor baseAmbient = SkColorSetARGB(kAmbientAlpha*SkColorGetA(color),
64 SkColorGetR(color), SkColorGetG(color),
65 SkColorGetB(color));
66 SkColor baseSpot = SkColorSetARGB(kSpotAlpha*SkColorGetA(color),
67 SkColorGetR(color), SkColorGetG(color),
68 SkColorGetB(color));
69 SkColor tonalAmbient, tonalSpot;
70 SkShadowUtils::ComputeTonalColors(baseAmbient, baseSpot, &tonalAmbient, &tonalSpot);
Jim Van Verth744cbb32017-12-19 13:02:38 -050071 SkShadowUtils::DrawShadow(canvas, path, zPlaneParams,
Jim Van Vertha5566842018-02-22 10:58:34 -050072 lightPos, kLightWidth, tonalAmbient, tonalSpot);
Jim Van Verth744cbb32017-12-19 13:02:38 -050073 canvas->drawPath(path, paint);
74 canvas->restore();
75
76 lightPos.fX += 120;
77 xPos += 120;
78 }
79
80 lightPos.fY += 200;
81 yPos += 200;
82 }
83}
84
85DEF_SIMPLE_GM(tonalshadows_light, canvas, kW, kH) {
86 draw_content(canvas, kLightBackground);
87}
88
89DEF_SIMPLE_GM(tonalshadows_dark, canvas, kW, kH) {
90 draw_content(canvas, kDarkBackground);
91}
92
93
94