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