blob: 49e342d0022e358413f7564bca23467d1dbb5662 [file] [log] [blame]
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +00001/*
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
13static 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();
junov@google.comdbfac8a2012-12-06 21:47:40 +000018 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000019
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000020 SkCanvas canvas(*bm);
21 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000022 SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} };
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000023 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}
32static 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();
junov@google.comdbfac8a2012-12-06 21:47:40 +000037 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000038
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000039 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
55static 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}
61static void make_table1(uint8_t table[]) {
62 for (int i = 0; i < 256; ++i) {
63 table[i] = i * i / 255;
64 }
65}
66static void make_table2(uint8_t table[]) {
67 for (int i = 0; i < 256; ++i) {
68 float fi = i / 255.0f;
robertphillips@google.com6853e802012-04-16 15:50:18 +000069 table[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000070 }
71}
72
73static SkColorFilter* make_cf0() {
74 uint8_t table[256]; make_table0(table);
75 return SkTableColorFilter::Create(table);
76}
77static SkColorFilter* make_cf1() {
78 uint8_t table[256]; make_table1(table);
79 return SkTableColorFilter::Create(table);
80}
81static SkColorFilter* make_cf2() {
82 uint8_t table[256]; make_table2(table);
83 return SkTableColorFilter::Create(table);
84}
85static 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
92class TableColorFilterGM : public skiagm::GM {
93public:
94 TableColorFilterGM() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +000095
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000096protected:
97 virtual SkString onShortName() {
98 return SkString("tablecolorfilter");
99 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000100
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000101 virtual SkISize onISize() {
reed@google.com6b2f7ea2012-03-14 20:00:42 +0000102 return SkISize::Make(700, 300);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000103 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000104
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000105 virtual void onDraw(SkCanvas* canvas) {
106 canvas->drawColor(0xFFDDDDDD);
107 canvas->translate(20, 20);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000109 SkScalar x = 0, y = 0;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000111 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);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000116 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);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000127
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000128 y += bm.height() * 9 / 8;
129 }
130 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000131
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000132private:
133 typedef GM INHERITED;
134};
135
136//////////////////////////////////////////////////////////////////////////////
137
138static skiagm::GM* MyFactory(void*) { return new TableColorFilterGM; }
139static skiagm::GMRegistry reg(MyFactory);