2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2009-04-02 20:49:39 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> setgrad(const SkRect& r, SkColor c0, SkColor c1) {
|
2009-07-02 15:01:02 +00:00
|
|
|
SkColor colors[] = { c0, c1 };
|
|
|
|
SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
|
2016-03-09 02:50:00 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2009-07-02 15:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_alphagradients(SkCanvas* canvas) {
|
|
|
|
SkRect r;
|
|
|
|
r.set(SkIntToScalar(10), SkIntToScalar(10),
|
|
|
|
SkIntToScalar(410), SkIntToScalar(30));
|
|
|
|
SkPaint p, p2;
|
|
|
|
p2.setStyle(SkPaint::kStroke_Style);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
p.setShader(setgrad(r, 0xFF00FF00, 0x0000FF00));
|
2009-07-02 15:01:02 +00:00
|
|
|
canvas->drawRect(r, p);
|
|
|
|
canvas->drawRect(r, p2);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-07-02 15:01:02 +00:00
|
|
|
r.offset(0, r.height() + SkIntToScalar(4));
|
2016-03-09 02:50:00 +00:00
|
|
|
p.setShader(setgrad(r, 0xFF00FF00, 0x00000000));
|
2009-07-02 15:01:02 +00:00
|
|
|
canvas->drawRect(r, p);
|
|
|
|
canvas->drawRect(r, p2);
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-07-02 15:01:02 +00:00
|
|
|
r.offset(0, r.height() + SkIntToScalar(4));
|
2016-03-09 02:50:00 +00:00
|
|
|
p.setShader(setgrad(r, 0xFF00FF00, 0x00FF0000));
|
2009-07-02 15:01:02 +00:00
|
|
|
canvas->drawRect(r, p);
|
|
|
|
canvas->drawRect(r, p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-04-02 20:49:39 +00:00
|
|
|
struct GradData {
|
|
|
|
int fCount;
|
|
|
|
const SkColor* fColors;
|
|
|
|
const SkScalar* fPos;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const SkColor gColors[] = {
|
|
|
|
SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK
|
|
|
|
};
|
|
|
|
static const SkScalar gPos0[] = { 0, SK_Scalar1 };
|
|
|
|
static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
|
|
|
|
static const SkScalar gPos2[] = {
|
|
|
|
0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1
|
|
|
|
};
|
|
|
|
|
|
|
|
static const GradData gGradData[] = {
|
2015-08-27 14:41:13 +00:00
|
|
|
{ 2, gColors, nullptr },
|
2009-04-02 20:49:39 +00:00
|
|
|
{ 2, gColors, gPos0 },
|
|
|
|
{ 2, gColors, gPos1 },
|
2015-08-27 14:41:13 +00:00
|
|
|
{ 5, gColors, nullptr },
|
2009-04-02 20:49:39 +00:00
|
|
|
{ 5, gColors, gPos2 }
|
|
|
|
};
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
|
|
|
|
return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
|
2009-04-02 20:49:39 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
|
2009-04-02 20:49:39 +00:00
|
|
|
SkPoint center;
|
|
|
|
center.set(SkScalarAve(pts[0].fX, pts[1].fX),
|
|
|
|
SkScalarAve(pts[0].fY, pts[1].fY));
|
2016-03-09 02:50:00 +00:00
|
|
|
return SkGradientShader::MakeRadial(center, center.fX, data.fColors,
|
|
|
|
data.fPos, data.fCount, tm);
|
2009-04-02 20:49:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
|
2009-04-02 20:49:39 +00:00
|
|
|
SkPoint center;
|
|
|
|
center.set(SkScalarAve(pts[0].fX, pts[1].fX),
|
|
|
|
SkScalarAve(pts[0].fY, pts[1].fY));
|
2016-03-09 02:50:00 +00:00
|
|
|
return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
|
2009-04-02 20:49:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
|
2009-09-22 20:14:42 +00:00
|
|
|
SkPoint center0, center1;
|
|
|
|
center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
|
|
|
|
SkScalarAve(pts[0].fY, pts[1].fY));
|
|
|
|
center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
|
|
|
|
SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
|
2016-03-09 02:50:00 +00:00
|
|
|
return SkGradientShader::MakeTwoPointConical(
|
2009-09-22 20:14:42 +00:00
|
|
|
center1, (pts[1].fX - pts[0].fX) / 7,
|
|
|
|
center0, (pts[1].fX - pts[0].fX) / 2,
|
2014-05-22 12:27:41 +00:00
|
|
|
data.fColors, data.fPos, data.fCount, tm);
|
2009-09-22 20:14:42 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> Make2ConicalConcentric(const SkPoint pts[2], const GradData& data,
|
2014-05-22 12:27:41 +00:00
|
|
|
SkShader::TileMode tm) {
|
2011-04-06 19:35:37 +00:00
|
|
|
SkPoint center;
|
|
|
|
center.set(SkScalarAve(pts[0].fX, pts[1].fX),
|
|
|
|
SkScalarAve(pts[0].fY, pts[1].fY));
|
2016-03-09 02:50:00 +00:00
|
|
|
return SkGradientShader::MakeTwoPointConical(
|
2011-04-06 19:35:37 +00:00
|
|
|
center, (pts[1].fX - pts[0].fX) / 7,
|
|
|
|
center, (pts[1].fX - pts[0].fX) / 2,
|
2014-05-22 12:27:41 +00:00
|
|
|
data.fColors, data.fPos, data.fCount, tm);
|
2011-04-06 19:35:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
|
2014-05-22 12:27:41 +00:00
|
|
|
|
2009-04-02 20:49:39 +00:00
|
|
|
static const GradMaker gGradMakers[] = {
|
2015-05-04 15:32:51 +00:00
|
|
|
MakeLinear, MakeRadial, MakeSweep, Make2Conical, Make2ConicalConcentric
|
2009-04-02 20:49:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-04-22 14:10:48 +00:00
|
|
|
class GradientsView : public SampleView {
|
2009-04-02 20:49:39 +00:00
|
|
|
public:
|
2012-08-23 18:19:56 +00:00
|
|
|
GradientsView() {
|
2011-04-22 14:10:48 +00:00
|
|
|
this->setBGColor(0xFFDDDDDD);
|
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-04-02 20:49:39 +00:00
|
|
|
protected:
|
2016-03-09 02:50:00 +00:00
|
|
|
bool onQuery(SkEvent* evt) override {
|
2009-04-02 20:49:39 +00:00
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Gradients");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2009-04-02 20:49:39 +00:00
|
|
|
SkPoint pts[2] = {
|
|
|
|
{ 0, 0 },
|
|
|
|
{ SkIntToScalar(100), SkIntToScalar(100) }
|
|
|
|
};
|
|
|
|
SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
|
|
|
|
SkPaint paint;
|
2009-07-10 03:33:52 +00:00
|
|
|
paint.setDither(true);
|
2009-07-02 15:01:02 +00:00
|
|
|
|
|
|
|
canvas->save();
|
2009-09-22 20:14:42 +00:00
|
|
|
canvas->translate(SkIntToScalar(20), SkIntToScalar(10));
|
2011-04-06 19:35:37 +00:00
|
|
|
|
|
|
|
for (int tm = 0; tm < SkShader::kTileModeCount; ++tm) {
|
2009-04-02 20:49:39 +00:00
|
|
|
canvas->save();
|
2011-04-06 19:35:37 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
|
|
|
|
canvas->save();
|
|
|
|
for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
|
2016-03-09 02:50:00 +00:00
|
|
|
paint.setShader(gGradMakers[j](pts, gGradData[i], (SkShader::TileMode)tm));
|
2011-04-06 19:35:37 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->translate(0, SkIntToScalar(120));
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(SkIntToScalar(120), 0);
|
2009-04-02 20:49:39 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2011-04-06 19:35:37 +00:00
|
|
|
canvas->translate(SK_ARRAY_COUNT(gGradData)*SkIntToScalar(120), 0);
|
2009-04-02 20:49:39 +00:00
|
|
|
}
|
2009-07-02 15:01:02 +00:00
|
|
|
canvas->restore();
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-07-02 15:01:02 +00:00
|
|
|
canvas->translate(0, SkIntToScalar(370));
|
2012-06-06 12:09:51 +00:00
|
|
|
if (false) { // avoid bit rot, suppress warning
|
|
|
|
test_alphagradients(canvas);
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
this->inval(nullptr);
|
2009-04-02 20:49:39 +00:00
|
|
|
}
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2009-04-02 20:49:39 +00:00
|
|
|
private:
|
2011-04-22 14:10:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2009-04-02 20:49:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new GradientsView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|