Home | History | Annotate | Download | only in gm
      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.h"
      9 #include "sk_tool_utils.h"
     10 #include "SkCanvas.h"
     11 #include "SkFont.h"
     12 #include "SkGradientShader.h"
     13 #include "SkHighContrastFilter.h"
     14 
     15 using InvertStyle = SkHighContrastConfig::InvertStyle;
     16 
     17 static SkScalar kSize   = 200;
     18 static SkColor  kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
     19 static SkColor  kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
     20 
     21 static void draw_label(SkCanvas* canvas, const SkHighContrastConfig& config) {
     22     char labelBuffer[256];
     23     const char* invertStr =
     24         (config.fInvertStyle == InvertStyle::kInvertBrightness ?
     25              "InvBrightness" :
     26             (config.fInvertStyle == InvertStyle::kInvertLightness ?
     27                  "InvLightness" : "NoInvert"));
     28 
     29     snprintf(labelBuffer, sizeof(labelBuffer), "%s%s contrast=%.1f",
     30              config.fGrayscale ? "Gray " : "",
     31              invertStr,
     32              config.fContrast);
     33 
     34     SkFont font;
     35     font.setTypeface(sk_tool_utils::create_portable_typeface());
     36     font.setSize(0.05f);
     37     font.setEdging(SkFont::Edging::kAlias);
     38 
     39     size_t len = strlen(labelBuffer);
     40 
     41     SkScalar width = font.measureText(labelBuffer, len, kUTF8_SkTextEncoding);
     42     canvas->drawSimpleText(labelBuffer, len, kUTF8_SkTextEncoding, 0.5f - width / 2, 0.16f, font, SkPaint());
     43 }
     44 
     45 static void draw_scene(SkCanvas* canvas, const SkHighContrastConfig& config) {
     46     SkRect bounds = SkRect::MakeLTRB(0.0f, 0.0f, 1.0f, 1.0f);
     47     SkPaint xferPaint;
     48     xferPaint.setColorFilter(SkHighContrastFilter::Make(config));
     49     canvas->saveLayer(&bounds, &xferPaint);
     50 
     51     SkPaint paint;
     52     bounds = SkRect::MakeLTRB(0.1f, 0.2f, 0.9f, 0.4f);
     53     paint.setARGB(0xff, 0x66, 0x11, 0x11);
     54     canvas->drawRect(bounds, paint);
     55 
     56     SkFont font;
     57     font.setSize(0.15f);
     58     font.setEdging(SkFont::Edging::kAlias);
     59 
     60     paint.setARGB(0xff, 0xbb, 0x77, 0x77);
     61     canvas->drawString("A", 0.15f, 0.35f, font, paint);
     62 
     63     bounds = SkRect::MakeLTRB(0.1f, 0.8f, 0.9f, 1.0f);
     64     paint.setARGB(0xff, 0xcc, 0xcc, 0xff);
     65     canvas->drawRect(bounds, paint);
     66 
     67     paint.setARGB(0xff, 0x88, 0x88, 0xbb);
     68     canvas->drawString("Z", 0.75f, 0.95f, font, paint);
     69 
     70     bounds = SkRect::MakeLTRB(0.1f, 0.4f, 0.9f, 0.6f);
     71     SkPoint     pts[] = { { 0, 0 }, { 1, 0 } };
     72     SkColor     colors[] = { SK_ColorWHITE, SK_ColorBLACK };
     73     SkScalar    pos[] = { 0.2f, 0.8f };
     74     paint.setShader(SkGradientShader::MakeLinear(
     75         pts, colors, pos,
     76         SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode));
     77     canvas->drawRect(bounds, paint);
     78 
     79     bounds = SkRect::MakeLTRB(0.1f, 0.6f, 0.9f, 0.8f);
     80     SkColor colors2[] = { SK_ColorGREEN, SK_ColorWHITE };
     81     paint.setShader(SkGradientShader::MakeLinear(
     82         pts, colors2, pos,
     83         SK_ARRAY_COUNT(colors2), SkShader::kClamp_TileMode));
     84     canvas->drawRect(bounds, paint);
     85 
     86     canvas->restore();
     87 }
     88 
     89 class HighContrastFilterGM : public skiagm::GM {
     90 public:
     91     HighContrastFilterGM() {
     92         SkColor  g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
     93         SkColor  g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
     94         SkPoint  g1Points[] = { { 0, 0 }, { 0,     100 } };
     95         SkPoint  g2Points[] = { { 0, 0 }, { kSize, 0   } };
     96         SkScalar pos[] = { 0.2f, 1.0f };
     97 
     98         SkHighContrastConfig fConfig;
     99         fFilter = SkHighContrastFilter::Make(fConfig);
    100         fGr1 = SkGradientShader::MakeLinear(
    101             g1Points, g1Colors, pos, SK_ARRAY_COUNT(g1Colors),
    102             SkShader::kClamp_TileMode);
    103         fGr2 = SkGradientShader::MakeLinear(
    104             g2Points, g2Colors, pos, SK_ARRAY_COUNT(g2Colors),
    105             SkShader::kClamp_TileMode);
    106     }
    107 
    108 protected:
    109 
    110     SkString onShortName() override {
    111         return SkString("highcontrastfilter");
    112     }
    113 
    114     SkISize onISize() override {
    115         return SkISize::Make(600, 420);
    116     }
    117 
    118     void onDraw(SkCanvas* canvas) override {
    119         SkHighContrastConfig configs[] = {
    120             { false, InvertStyle::kNoInvert, 0.0f },
    121             { false, InvertStyle::kInvertBrightness, 0.0f },
    122             { false, InvertStyle::kInvertLightness, 0.0f },
    123             { false, InvertStyle::kInvertLightness, 0.2f },
    124             { true, InvertStyle::kNoInvert, 0.0f },
    125             { true, InvertStyle::kInvertBrightness, 0.0f },
    126             { true, InvertStyle::kInvertLightness, 0.0f },
    127             { true, InvertStyle::kInvertLightness, 0.2f },
    128         };
    129 
    130         for (size_t i = 0; i < SK_ARRAY_COUNT(configs); ++i) {
    131             SkScalar x = kSize * (i % 4);
    132             SkScalar y = kSize * (i / 4);
    133             canvas->save();
    134             canvas->translate(x, y);
    135             canvas->scale(kSize, kSize);
    136             draw_scene(canvas, configs[i]);
    137             draw_label(canvas, configs[i]);
    138             canvas->restore();
    139         }
    140     }
    141 
    142 private:
    143     sk_sp<SkColorFilter>    fFilter;
    144     sk_sp<SkShader>         fGr1, fGr2;
    145 
    146     typedef skiagm::GM INHERITED;
    147 };
    148 
    149 DEF_GM(return new HighContrastFilterGM;)
    150