Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2016 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 "Resources.h"
     10 
     11 #include "SkImageDecoder.h"
     12 #include "SkAvoidXfermode.h"
     13 #include "SkStream.h"
     14 
     15 class AvoidXfermodeGM : public skiagm::GM {
     16 public:
     17     AvoidXfermodeGM() { }
     18 
     19 protected:
     20     SkString onShortName() override {
     21         return SkString("avoidxfermode");
     22     }
     23 
     24     SkISize onISize() override { return SkISize::Make(128, 128); }
     25 
     26     void onOnceBeforeDraw() override {
     27         SkImageDecoder* codec = nullptr;
     28         SkString resourcePath = GetResourcePath("color_wheel.png");
     29         SkFILEStream stream(resourcePath.c_str());
     30         if (stream.isValid()) {
     31             codec = SkImageDecoder::Factory(&stream);
     32         }
     33         if (codec) {
     34             stream.rewind();
     35             codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
     36             delete codec;
     37         } else {
     38             fBM.allocN32Pixels(1, 1);
     39             fBM.eraseARGB(255, 255, 0 , 0); // red == bad
     40         }
     41     }
     42 
     43     void onDraw(SkCanvas* canvas) override {
     44         canvas->drawBitmap(fBM, 0, 0);
     45 
     46         SkRect r = SkRect::MakeIWH(64, 64);
     47 
     48         // UL corner: replace white with black with a tight tolerance
     49         SkPaint p1;
     50         p1.setColor(SK_ColorBLACK);
     51         p1.setXfermode(SkAvoidXfermode::Create(SK_ColorWHITE,
     52                                                5,
     53                                                SkAvoidXfermode::kTargetColor_Mode))->unref();
     54 
     55         canvas->drawRect(r, p1);
     56 
     57         r.offsetTo(64, 0.0f);
     58 
     59         // UR corner: draw black everywhere except white with a tight tolerance
     60         SkPaint p2;
     61         p2.setColor(SK_ColorBLACK);
     62         p2.setXfermode(SkAvoidXfermode::Create(SK_ColorWHITE,
     63                                                250,
     64                                                SkAvoidXfermode::kAvoidColor_Mode))->unref();
     65 
     66         canvas->drawRect(r, p2);
     67 
     68         r.offsetTo(0.0f, 64);
     69 
     70         // LL corner: replace red with transparent blue with a loose tolerance
     71         SkPaint p3;
     72         p3.setColor(0x7F0000FF);
     73         p3.setXfermode(SkAvoidXfermode::Create(SK_ColorRED,
     74                                                250,
     75                                                SkAvoidXfermode::kTargetColor_Mode))->unref();
     76 
     77         canvas->drawRect(r, p3);
     78 
     79         r.offsetTo(64, 64);
     80 
     81         // LR corner: draw black everywhere except red with a loose tolerance
     82         SkPaint p4;
     83         p4.setColor(SK_ColorBLACK);
     84         p4.setXfermode(SkAvoidXfermode::Create(SK_ColorRED,
     85                                                5,
     86                                                SkAvoidXfermode::kAvoidColor_Mode))->unref();
     87 
     88         canvas->drawRect(r, p4);
     89     }
     90 
     91 private:
     92     SkBitmap fBM;
     93 
     94     typedef GM INHERITED;
     95 };
     96 
     97 //////////////////////////////////////////////////////////////////////////////
     98 
     99 DEF_GM(return new AvoidXfermodeGM;)
    100