Debian Codegraph @

gm/highcontrastfilter.cpp

raw
1 /*
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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorFilter.h"
12 #include "include/core/SkFont.h"
13 #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"
25 #include "include/effects/SkGradient.h"
26 #include "include/effects/SkHighContrastFilter.h"
27 #include "tools/ToolUtils.h"
28 #include "tools/fonts/FontToolUtils.h"
29
30 #include <stdio.h>
31 #include <string.h>
32
33 using InvertStyle = SkHighContrastConfig::InvertStyle;
34
35 static SkScalar kSize = 200;
36 static SkColor4f kColor1 = SkColor4f::FromColor(SkColorSetARGB(0xff, 0xff, 0xff, 0));
37 static SkColor4f kColor2 = SkColor4f::FromColor(SkColorSetARGB(0xff, 0x82, 0xff, 0));
38
39 static void draw_label(SkCanvas* canvas, const SkHighContrastConfig& config) {
40 char labelBuffer[256];
41 const char* invertStr =
42 (config.fInvertStyle == InvertStyle::kInvertBrightness ?
43 "InvBright" :
44 (config.fInvertStyle == InvertStyle::kInvertLightness ?
45 "InvLight" : "NoInvert"));
46
47 snprintf(labelBuffer, sizeof(labelBuffer), "%s%s contrast=%.1f",
48 config.fGrayscale ? "Gray " : "",
49 invertStr,
50 config.fContrast);
51
52 SkFont font = ToolUtils::DefaultPortableFont();
53 font.setSize(0.075f);
54 font.setEdging(SkFont::Edging::kAntiAlias);
55
56 size_t len = strlen(labelBuffer);
57
58 SkScalar width = font.measureText(labelBuffer, len, SkTextEncoding::kUTF8);
59 canvas->drawSimpleText(labelBuffer, len, SkTextEncoding::kUTF8, 0.5f - width / 2, 0.16f, font, SkPaint());
60 }
61
62 static 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
73 SkFont font = ToolUtils::DefaultPortableFont();
74 font.setSize(0.15f);
75 font.setEdging(SkFont::Edging::kAlias);
76
77 paint.setARGB(0xff, 0xbb, 0x77, 0x77);
78 canvas->drawString("A", 0.15f, 0.35f, font, paint);
79
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);
85 canvas->drawString("Z", 0.75f, 0.95f, font, paint);
86
87 bounds = SkRect::MakeLTRB(0.1f, 0.4f, 0.9f, 0.6f);
88 SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
89 SkColor4f colors[] = { SkColors::kWhite, SkColors::kBlack };
90 SkScalar pos[] = { 0.2f, 0.8f };
91 paint.setShader(SkShaders::LinearGradient(pts, {{colors, pos, SkTileMode::kClamp}, {}}));
92 canvas->drawRect(bounds, paint);
93
94 bounds = SkRect::MakeLTRB(0.1f, 0.6f, 0.9f, 0.8f);
95 SkColor4f colors2[] = { SkColors::kGreen, SkColors::kWhite };
96 paint.setShader(SkShaders::LinearGradient(pts, {{colors2, pos, SkTileMode::kClamp}, {}}));
97 canvas->drawRect(bounds, paint);
98
99 canvas->restore();
100 }
101
102 class HighContrastFilterGM : public skiagm::GM {
103 protected:
104 void onOnceBeforeDraw() override {
105 SkColor4f g1Colors[] = { kColor1, kColor1.withAlphaByte(0x20) };
106 SkColor4f g2Colors[] = { kColor2, kColor2.withAlphaByte(0x20) };
107 SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } };
108 SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } };
109 SkScalar pos[] = { 0.2f, 1.0f };
110
111 SkHighContrastConfig fConfig;
112 fFilter = SkHighContrastFilter::Make(fConfig);
113 fGr1 = SkShaders::LinearGradient(
114 g1Points, {{g1Colors, pos, SkTileMode::kClamp}, {}});
115 fGr2 = SkShaders::LinearGradient(
116 g2Points, {{g2Colors, pos, SkTileMode::kClamp}, {}});
117 }
118
119 SkString getName() const override { return SkString("highcontrastfilter"); }
120
121 SkISize getISize() override { return SkISize::Make(800, 420); }
122
123 void onDraw(SkCanvas* canvas) override {
124 SkHighContrastConfig configs[] = {
125 { false, InvertStyle::kNoInvert, 0.0f },
126 { false, InvertStyle::kInvertBrightness, 0.0f },
127 { false, InvertStyle::kInvertLightness, 0.0f },
128 { false, InvertStyle::kInvertLightness, 0.2f },
129 { true, InvertStyle::kNoInvert, 0.0f },
130 { true, InvertStyle::kInvertBrightness, 0.0f },
131 { true, InvertStyle::kInvertLightness, 0.0f },
132 { true, InvertStyle::kInvertLightness, 0.2f },
133 };
134
135 for (size_t i = 0; i < std::size(configs); ++i) {
136 SkScalar x = kSize * (i % 4);
137 SkScalar y = kSize * (i / 4);
138 canvas->save();
139 canvas->translate(x, y);
140 canvas->scale(kSize, kSize);
141 draw_scene(canvas, configs[i]);
142 draw_label(canvas, configs[i]);
143 canvas->restore();
144 }
145 }
146
147 private:
148 sk_sp<SkColorFilter> fFilter;
149 sk_sp<SkShader> fGr1, fGr2;
150
151 using INHERITED = skiagm::GM;
152 };
153
154 DEF_GM(return new HighContrastFilterGM;)