xref: /aosp_15_r20/external/skia/gm/highcontrastfilter.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkGradientShader.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 SkColor  kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
37 static SkColor  kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
38 
draw_label(SkCanvas * canvas,const SkHighContrastConfig & config)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 
draw_scene(SkCanvas * canvas,const SkHighContrastConfig & config)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     SkColor     colors[] = { SK_ColorWHITE, SK_ColorBLACK };
90     SkScalar    pos[] = { 0.2f, 0.8f };
91     paint.setShader(SkGradientShader::MakeLinear(
92         pts, colors, pos,
93         std::size(colors), SkTileMode::kClamp));
94     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,
100         std::size(colors2), SkTileMode::kClamp));
101     canvas->drawRect(bounds, paint);
102 
103     canvas->restore();
104 }
105 
106 class HighContrastFilterGM : public skiagm::GM {
107 protected:
onOnceBeforeDraw()108     void onOnceBeforeDraw() override {
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, std::size(g1Colors),
119             SkTileMode::kClamp);
120         fGr2 = SkGradientShader::MakeLinear(
121             g2Points, g2Colors, pos, std::size(g2Colors),
122             SkTileMode::kClamp);
123     }
124 
getName() const125     SkString getName() const override { return SkString("highcontrastfilter"); }
126 
getISize()127     SkISize getISize() override { return SkISize::Make(800, 420); }
128 
onDraw(SkCanvas * canvas)129     void onDraw(SkCanvas* canvas) override {
130         SkHighContrastConfig configs[] = {
131             { false, InvertStyle::kNoInvert, 0.0f },
132             { false, InvertStyle::kInvertBrightness, 0.0f },
133             { false, InvertStyle::kInvertLightness, 0.0f },
134             { false, InvertStyle::kInvertLightness, 0.2f },
135             { true, InvertStyle::kNoInvert, 0.0f },
136             { true, InvertStyle::kInvertBrightness, 0.0f },
137             { true, InvertStyle::kInvertLightness, 0.0f },
138             { true, InvertStyle::kInvertLightness, 0.2f },
139         };
140 
141         for (size_t i = 0; i < std::size(configs); ++i) {
142             SkScalar x = kSize * (i % 4);
143             SkScalar y = kSize * (i / 4);
144             canvas->save();
145             canvas->translate(x, y);
146             canvas->scale(kSize, kSize);
147             draw_scene(canvas, configs[i]);
148             draw_label(canvas, configs[i]);
149             canvas->restore();
150         }
151     }
152 
153 private:
154     sk_sp<SkColorFilter>    fFilter;
155     sk_sp<SkShader>         fGr1, fGr2;
156 
157     using INHERITED = skiagm::GM;
158 };
159 
160 DEF_GM(return new HighContrastFilterGM;)
161