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.
|
|
|
|
*/
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "samplecode/Sample.h"
|
2009-04-02 20:49:39 +00:00
|
|
|
|
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 } };
|
2019-04-03 14:27:45 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
|
2009-07-02 15:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_alphagradients(SkCanvas* canvas) {
|
|
|
|
SkRect r;
|
2019-08-24 23:39:13 +00:00
|
|
|
r.setLTRB(10, 10, 410, 30);
|
2009-07-02 15:01:02 +00:00
|
|
|
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 }
|
|
|
|
};
|
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkTileMode tm) {
|
2016-03-09 02:50:00 +00:00
|
|
|
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
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkTileMode 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
|
|
|
}
|
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkTileMode 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
|
|
|
}
|
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkTileMode 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,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode 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
|
|
|
}
|
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, SkTileMode 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
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class GradientsView : public Sample {
|
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:
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("Gradients"); }
|
2009-04-02 20:49:39 +00:00
|
|
|
|
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
|
|
|
|
2019-04-03 14:27:45 +00:00
|
|
|
for (int tm = 0; tm < kSkTileModeCount; ++tm) {
|
2009-04-02 20:49:39 +00:00
|
|
|
canvas->save();
|
2022-06-22 12:52:07 +00:00
|
|
|
for (size_t i = 0; i < std::size(gGradData); i++) {
|
2011-04-06 19:35:37 +00:00
|
|
|
canvas->save();
|
2022-06-22 12:52:07 +00:00
|
|
|
for (size_t j = 0; j < std::size(gGradMakers); j++) {
|
2019-04-03 14:27:45 +00:00
|
|
|
paint.setShader(gGradMakers[j](pts, gGradData[i], (SkTileMode)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();
|
2022-06-22 12:52:07 +00:00
|
|
|
canvas->translate(std::size(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));
|
2022-02-04 21:09:04 +00:00
|
|
|
if ((false)) { // avoid bit rot, suppress warning
|
2012-06-06 12:09:51 +00:00
|
|
|
test_alphagradients(canvas);
|
|
|
|
}
|
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:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Sample;
|
2009-04-02 20:49:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new GradientsView(); )
|