1 /* 2 * Copyright 2011 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 "SkGradientShader.h" 11 #include "SkTableColorFilter.h" 12 13 static void make_bm0(SkBitmap* bm) { 14 int W = 120; 15 int H = 120; 16 bm->setConfig(SkBitmap::kARGB_8888_Config, W, H); 17 bm->allocPixels(); 18 bm->eraseColor(SK_ColorTRANSPARENT); 19 20 SkCanvas canvas(*bm); 21 SkPaint paint; 22 SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} }; 23 SkColor colors[] = { 24 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN, 25 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE 26 }; 27 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), 28 SkShader::kClamp_TileMode); 29 paint.setShader(s)->unref(); 30 canvas.drawPaint(paint); 31 } 32 static void make_bm1(SkBitmap* bm) { 33 int W = 120; 34 int H = 120; 35 bm->setConfig(SkBitmap::kARGB_8888_Config, W, H); 36 bm->allocPixels(); 37 bm->eraseColor(SK_ColorTRANSPARENT); 38 39 SkCanvas canvas(*bm); 40 SkPaint paint; 41 SkScalar cx = SkIntToScalar(W)/2; 42 SkScalar cy = SkIntToScalar(H)/2; 43 SkColor colors[] = { 44 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, 45 }; 46 SkShader* s = SkGradientShader::CreateRadial(SkPoint::Make(SkIntToScalar(W)/2, 47 SkIntToScalar(H)/2), 48 SkIntToScalar(W)/2, colors, NULL, SK_ARRAY_COUNT(colors), 49 SkShader::kClamp_TileMode); 50 paint.setShader(s)->unref(); 51 paint.setAntiAlias(true); 52 canvas.drawCircle(cx, cy, cx, paint); 53 } 54 55 static void make_table0(uint8_t table[]) { 56 for (int i = 0; i < 256; ++i) { 57 int n = i >> 5; 58 table[i] = (n << 5) | (n << 2) | (n >> 1); 59 } 60 } 61 static void make_table1(uint8_t table[]) { 62 for (int i = 0; i < 256; ++i) { 63 table[i] = i * i / 255; 64 } 65 } 66 static void make_table2(uint8_t table[]) { 67 for (int i = 0; i < 256; ++i) { 68 float fi = i / 255.0f; 69 table[i] = static_cast<uint8_t>(sqrtf(fi) * 255); 70 } 71 } 72 73 static SkColorFilter* make_cf0() { 74 uint8_t table[256]; make_table0(table); 75 return SkTableColorFilter::Create(table); 76 } 77 static SkColorFilter* make_cf1() { 78 uint8_t table[256]; make_table1(table); 79 return SkTableColorFilter::Create(table); 80 } 81 static SkColorFilter* make_cf2() { 82 uint8_t table[256]; make_table2(table); 83 return SkTableColorFilter::Create(table); 84 } 85 static SkColorFilter* make_cf3() { 86 uint8_t table0[256]; make_table0(table0); 87 uint8_t table1[256]; make_table1(table1); 88 uint8_t table2[256]; make_table2(table2); 89 return SkTableColorFilter::CreateARGB(NULL, table0, table1, table2); 90 } 91 92 class TableColorFilterGM : public skiagm::GM { 93 public: 94 TableColorFilterGM() {} 95 96 protected: 97 virtual SkString onShortName() { 98 return SkString("tablecolorfilter"); 99 } 100 101 virtual SkISize onISize() { 102 return SkISize::Make(700, 300); 103 } 104 105 virtual void onDraw(SkCanvas* canvas) { 106 canvas->drawColor(0xFFDDDDDD); 107 canvas->translate(20, 20); 108 109 SkScalar x = 0, y = 0; 110 111 static void (*gMakers[])(SkBitmap*) = { make_bm0, make_bm1 }; 112 for (size_t maker = 0; maker < SK_ARRAY_COUNT(gMakers); ++maker) { 113 SkBitmap bm; 114 gMakers[maker](&bm); 115 116 SkPaint paint; 117 x = 0; 118 canvas->drawBitmap(bm, x, y, &paint); 119 paint.setColorFilter(make_cf0())->unref(); x += bm.width() * 9 / 8; 120 canvas->drawBitmap(bm, x, y, &paint); 121 paint.setColorFilter(make_cf1())->unref(); x += bm.width() * 9 / 8; 122 canvas->drawBitmap(bm, x, y, &paint); 123 paint.setColorFilter(make_cf2())->unref(); x += bm.width() * 9 / 8; 124 canvas->drawBitmap(bm, x, y, &paint); 125 paint.setColorFilter(make_cf3())->unref(); x += bm.width() * 9 / 8; 126 canvas->drawBitmap(bm, x, y, &paint); 127 128 y += bm.height() * 9 / 8; 129 } 130 } 131 132 private: 133 typedef GM INHERITED; 134 }; 135 136 ////////////////////////////////////////////////////////////////////////////// 137 138 static skiagm::GM* MyFactory(void*) { return new TableColorFilterGM; } 139 static skiagm::GMRegistry reg(MyFactory); 140