1/* 2 * Copyright 2014 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 "SkCanvas.h" 10#include "SkColorMatrixFilter.h" 11#include "SkGradientShader.h" 12 13static sk_sp<SkShader> make_shader(const SkRect& bounds) { 14 const SkPoint pts[] = { 15 { bounds.left(), bounds.top() }, 16 { bounds.right(), bounds.bottom() }, 17 }; 18 const SkColor colors[] = { 19 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorBLACK, 20 SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW, 21 }; 22 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), 23 SkShader::kClamp_TileMode); 24} 25 26typedef void (*InstallPaint)(SkPaint*, uint32_t, uint32_t); 27 28static void install_nothing(SkPaint* paint, uint32_t, uint32_t) { 29 paint->setColorFilter(nullptr); 30} 31 32static void install_lighting(SkPaint* paint, uint32_t mul, uint32_t add) { 33 paint->setColorFilter(SkColorMatrixFilter::MakeLightingFilter(mul, add)); 34} 35 36class ColorFiltersGM : public skiagm::GM { 37public: 38 ColorFiltersGM() { 39 fName.set("lightingcolorfilter"); 40 } 41 42protected: 43 virtual SkString onShortName() override { 44 return fName; 45 } 46 47 virtual SkISize onISize() override { 48 return SkISize::Make(620, 430); 49 } 50 51 void onDraw(SkCanvas* canvas) override { 52 SkPaint paint; 53 SkRect r; 54 r.setWH(600, 50); 55 paint.setShader(make_shader(r)); 56 57 const struct { 58 InstallPaint fProc; 59 uint32_t fData0, fData1; 60 } rec[] = { 61 { install_nothing, 0, 0 }, 62 { install_lighting, 0xFF0000, 0 }, 63 { install_lighting, 0x00FF00, 0 }, 64 { install_lighting, 0x0000FF, 0 }, 65 { install_lighting, 0x000000, 0xFF0000 }, 66 { install_lighting, 0x000000, 0x00FF00 }, 67 { install_lighting, 0x000000, 0x0000FF }, 68 }; 69 70 canvas->translate(10, 10); 71 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { 72 rec[i].fProc(&paint, rec[i].fData0, rec[i].fData1); 73 canvas->drawRect(r, paint); 74 canvas->translate(0, r.height() + 10); 75 } 76 } 77 78private: 79 SkString fName; 80 typedef GM INHERITED; 81}; 82 83 84////////////////////////////////////////////////////////////////////////////// 85 86DEF_GM(return new ColorFiltersGM;) 87