blob: dc5a0390a57ecda2f08aef9d6968fd3857042707 [file] [log] [blame]
Dominic Mazzoni394d4142017-02-14 11:15:31 -08001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkColorFilter.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkFontTypes.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkPoint.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkShader.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTileMode.h"
23#include "include/core/SkTypeface.h"
24#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/effects/SkGradientShader.h"
26#include "include/effects/SkHighContrastFilter.h"
27#include "tools/ToolUtils.h"
Dominic Mazzoni394d4142017-02-14 11:15:31 -080028
Ben Wagner7fde8e12019-05-01 17:28:53 -040029#include <stdio.h>
30#include <string.h>
31
Dominic Mazzoni394d4142017-02-14 11:15:31 -080032using InvertStyle = SkHighContrastConfig::InvertStyle;
33
34static SkScalar kSize = 200;
35static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
36static SkColor kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
37
38static void draw_label(SkCanvas* canvas, const SkHighContrastConfig& config) {
39 char labelBuffer[256];
40 const char* invertStr =
41 (config.fInvertStyle == InvertStyle::kInvertBrightness ?
42 "InvBrightness" :
43 (config.fInvertStyle == InvertStyle::kInvertLightness ?
44 "InvLightness" : "NoInvert"));
45
46 snprintf(labelBuffer, sizeof(labelBuffer), "%s%s contrast=%.1f",
47 config.fGrayscale ? "Gray " : "",
48 invertStr,
49 config.fContrast);
50
Mike Reeddf3d2252018-12-20 17:10:27 -050051 SkFont font;
Mike Kleinea3f0142019-03-20 11:12:10 -050052 font.setTypeface(ToolUtils::create_portable_typeface());
Mike Reeddf3d2252018-12-20 17:10:27 -050053 font.setSize(0.05f);
54 font.setEdging(SkFont::Edging::kAlias);
55
Dominic Mazzoni394d4142017-02-14 11:15:31 -080056 size_t len = strlen(labelBuffer);
57
Ben Wagner51e15a62019-05-07 15:38:46 -040058 SkScalar width = font.measureText(labelBuffer, len, SkTextEncoding::kUTF8);
59 canvas->drawSimpleText(labelBuffer, len, SkTextEncoding::kUTF8, 0.5f - width / 2, 0.16f, font, SkPaint());
Dominic Mazzoni394d4142017-02-14 11:15:31 -080060}
61
62static void draw_scene(SkCanvas* canvas, const SkHighContrastConfig& config) {
63 SkRect bounds = SkRect::MakeLTRB(0.0f, 0.0f, 1.0f, 1.0f);
64 SkPaint xferPaint;
65 xferPaint.setColorFilter(SkHighContrastFilter::Make(config));
66 canvas->saveLayer(&bounds, &xferPaint);
67
68 SkPaint paint;
69 bounds = SkRect::MakeLTRB(0.1f, 0.2f, 0.9f, 0.4f);
70 paint.setARGB(0xff, 0x66, 0x11, 0x11);
71 canvas->drawRect(bounds, paint);
72
Mike Reed1af9b482019-01-07 11:01:57 -050073 SkFont font;
74 font.setSize(0.15f);
75 font.setEdging(SkFont::Edging::kAlias);
76
Dominic Mazzoni394d4142017-02-14 11:15:31 -080077 paint.setARGB(0xff, 0xbb, 0x77, 0x77);
Mike Reed1af9b482019-01-07 11:01:57 -050078 canvas->drawString("A", 0.15f, 0.35f, font, paint);
Dominic Mazzoni394d4142017-02-14 11:15:31 -080079
80 bounds = SkRect::MakeLTRB(0.1f, 0.8f, 0.9f, 1.0f);
81 paint.setARGB(0xff, 0xcc, 0xcc, 0xff);
82 canvas->drawRect(bounds, paint);
83
84 paint.setARGB(0xff, 0x88, 0x88, 0xbb);
Mike Reed1af9b482019-01-07 11:01:57 -050085 canvas->drawString("Z", 0.75f, 0.95f, font, paint);
Dominic Mazzoni394d4142017-02-14 11:15:31 -080086
87 bounds = SkRect::MakeLTRB(0.1f, 0.4f, 0.9f, 0.6f);
88 SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
89 SkColor colors[] = { SK_ColorWHITE, SK_ColorBLACK };
90 SkScalar pos[] = { 0.2f, 0.8f };
91 paint.setShader(SkGradientShader::MakeLinear(
92 pts, colors, pos,
Mike Reedfae8fce2019-04-03 10:27:45 -040093 SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
Dominic Mazzoni394d4142017-02-14 11:15:31 -080094 canvas->drawRect(bounds, paint);
95
96 bounds = SkRect::MakeLTRB(0.1f, 0.6f, 0.9f, 0.8f);
97 SkColor colors2[] = { SK_ColorGREEN, SK_ColorWHITE };
98 paint.setShader(SkGradientShader::MakeLinear(
99 pts, colors2, pos,
Mike Reedfae8fce2019-04-03 10:27:45 -0400100 SK_ARRAY_COUNT(colors2), SkTileMode::kClamp));
Dominic Mazzoni394d4142017-02-14 11:15:31 -0800101 canvas->drawRect(bounds, paint);
102
103 canvas->restore();
104}
105
106class HighContrastFilterGM : public skiagm::GM {
107public:
108 HighContrastFilterGM() {
109 SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
110 SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
111 SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } };
112 SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } };
113 SkScalar pos[] = { 0.2f, 1.0f };
114
115 SkHighContrastConfig fConfig;
116 fFilter = SkHighContrastFilter::Make(fConfig);
117 fGr1 = SkGradientShader::MakeLinear(
118 g1Points, g1Colors, pos, SK_ARRAY_COUNT(g1Colors),
Mike Reedfae8fce2019-04-03 10:27:45 -0400119 SkTileMode::kClamp);
Dominic Mazzoni394d4142017-02-14 11:15:31 -0800120 fGr2 = SkGradientShader::MakeLinear(
121 g2Points, g2Colors, pos, SK_ARRAY_COUNT(g2Colors),
Mike Reedfae8fce2019-04-03 10:27:45 -0400122 SkTileMode::kClamp);
Dominic Mazzoni394d4142017-02-14 11:15:31 -0800123 }
124
125protected:
126
127 SkString onShortName() override {
128 return SkString("highcontrastfilter");
129 }
130
131 SkISize onISize() override {
132 return SkISize::Make(600, 420);
133 }
134
135 void onDraw(SkCanvas* canvas) override {
136 SkHighContrastConfig configs[] = {
137 { false, InvertStyle::kNoInvert, 0.0f },
138 { false, InvertStyle::kInvertBrightness, 0.0f },
139 { false, InvertStyle::kInvertLightness, 0.0f },
140 { false, InvertStyle::kInvertLightness, 0.2f },
141 { true, InvertStyle::kNoInvert, 0.0f },
142 { true, InvertStyle::kInvertBrightness, 0.0f },
143 { true, InvertStyle::kInvertLightness, 0.0f },
144 { true, InvertStyle::kInvertLightness, 0.2f },
145 };
146
147 for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
148 SkScalar x = kSize * (i % 4);
149 SkScalar y = kSize * (i / 4);
150 canvas->save();
151 canvas->translate(x, y);
152 canvas->scale(kSize, kSize);
153 draw_scene(canvas, configs[i]);
154 draw_label(canvas, configs[i]);
155 canvas->restore();
156 }
157 }
158
159private:
160 sk_sp<SkColorFilter> fFilter;
161 sk_sp<SkShader> fGr1, fGr2;
162
163 typedef skiagm::GM INHERITED;
164};
165
166DEF_GM(return new HighContrastFilterGM;)