• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 #include <stdio.h>
30 #include <string.h>
31 
32 using InvertStyle = SkHighContrastConfig::InvertStyle;
33 
34 static SkScalar kSize   = 200;
35 static SkColor  kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
36 static SkColor  kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
37 
draw_label(SkCanvas * canvas,const SkHighContrastConfig & config)38 static void draw_label(SkCanvas* canvas, const SkHighContrastConfig& config) {
39     char labelBuffer[256];
40     const char* invertStr =
41         (config.fInvertStyle == InvertStyle::kInvertBrightness ?
42              "InvBright" :
43             (config.fInvertStyle == InvertStyle::kInvertLightness ?
44                  "InvLight" : "NoInvert"));
45 
46     snprintf(labelBuffer, sizeof(labelBuffer), "%s%s contrast=%.1f",
47              config.fGrayscale ? "Gray " : "",
48              invertStr,
49              config.fContrast);
50 
51     SkFont font;
52     font.setTypeface(ToolUtils::create_portable_typeface());
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;
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         SK_ARRAY_COUNT(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         SK_ARRAY_COUNT(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, SK_ARRAY_COUNT(g1Colors),
119             SkTileMode::kClamp);
120         fGr2 = SkGradientShader::MakeLinear(
121             g2Points, g2Colors, pos, SK_ARRAY_COUNT(g2Colors),
122             SkTileMode::kClamp);
123     }
124 
onShortName()125     SkString onShortName() override {
126         return SkString("highcontrastfilter");
127     }
128 
onISize()129     SkISize onISize() override {
130         return SkISize::Make(800, 420);
131     }
132 
onDraw(SkCanvas * canvas)133     void onDraw(SkCanvas* canvas) override {
134         SkHighContrastConfig configs[] = {
135             { false, InvertStyle::kNoInvert, 0.0f },
136             { false, InvertStyle::kInvertBrightness, 0.0f },
137             { false, InvertStyle::kInvertLightness, 0.0f },
138             { false, InvertStyle::kInvertLightness, 0.2f },
139             { true, InvertStyle::kNoInvert, 0.0f },
140             { true, InvertStyle::kInvertBrightness, 0.0f },
141             { true, InvertStyle::kInvertLightness, 0.0f },
142             { true, InvertStyle::kInvertLightness, 0.2f },
143         };
144 
145         for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
146             SkScalar x = kSize * (i % 4);
147             SkScalar y = kSize * (i / 4);
148             canvas->save();
149             canvas->translate(x, y);
150             canvas->scale(kSize, kSize);
151             draw_scene(canvas, configs[i]);
152             draw_label(canvas, configs[i]);
153             canvas->restore();
154         }
155     }
156 
157 private:
158     sk_sp<SkColorFilter>    fFilter;
159     sk_sp<SkShader>         fGr1, fGr2;
160 
161     using INHERITED = skiagm::GM;
162 };
163 
164 DEF_GM(return new HighContrastFilterGM;)
165