2018-01-12 17:25:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkSGGradient_DEFINED
|
|
|
|
#define SkSGGradient_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "modules/sksg/include/SkSGRenderEffect.h"
|
2018-01-12 17:25:09 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
2018-01-12 17:25:09 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace sksg {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gradient base class.
|
|
|
|
*/
|
2019-04-01 17:40:58 +00:00
|
|
|
class Gradient : public Shader {
|
2018-01-12 17:25:09 +00:00
|
|
|
public:
|
|
|
|
struct ColorStop {
|
2019-11-05 14:22:39 +00:00
|
|
|
SkScalar fPosition;
|
|
|
|
SkColor4f fColor;
|
2018-01-12 17:25:09 +00:00
|
|
|
|
|
|
|
bool operator==(const ColorStop& other) const {
|
|
|
|
return fPosition == other.fPosition && fColor == other.fColor;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SG_ATTRIBUTE(ColorStops, std::vector<ColorStop>, fColorStops)
|
2019-04-03 14:27:45 +00:00
|
|
|
SG_ATTRIBUTE(TileMode , SkTileMode , fTileMode )
|
2018-01-12 17:25:09 +00:00
|
|
|
|
|
|
|
protected:
|
2019-04-01 17:40:58 +00:00
|
|
|
sk_sp<SkShader> onRevalidateShader() final;
|
2018-01-12 17:25:09 +00:00
|
|
|
|
2019-11-05 14:22:39 +00:00
|
|
|
virtual sk_sp<SkShader> onMakeShader(const std::vector<SkColor4f>& colors,
|
|
|
|
const std::vector<SkScalar >& positions) const = 0;
|
2018-01-12 17:25:09 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
Gradient() = default;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<ColorStop> fColorStops;
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode fTileMode = SkTileMode::kClamp;
|
2018-01-12 17:25:09 +00:00
|
|
|
|
2019-04-01 17:40:58 +00:00
|
|
|
using INHERITED = Shader;
|
2018-01-12 17:25:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class LinearGradient final : public Gradient {
|
|
|
|
public:
|
|
|
|
static sk_sp<LinearGradient> Make() {
|
|
|
|
return sk_sp<LinearGradient>(new LinearGradient());
|
|
|
|
}
|
|
|
|
|
|
|
|
SG_ATTRIBUTE(StartPoint, SkPoint, fStartPoint)
|
|
|
|
SG_ATTRIBUTE(EndPoint , SkPoint, fEndPoint )
|
|
|
|
|
|
|
|
protected:
|
2019-11-05 14:22:39 +00:00
|
|
|
sk_sp<SkShader> onMakeShader(const std::vector<SkColor4f>&,
|
|
|
|
const std::vector<SkScalar >&) const override;
|
2018-01-12 17:25:09 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
LinearGradient() = default;
|
|
|
|
|
|
|
|
SkPoint fStartPoint = SkPoint::Make(0, 0),
|
|
|
|
fEndPoint = SkPoint::Make(0, 0);
|
|
|
|
|
|
|
|
using INHERITED = Gradient;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RadialGradient final : public Gradient {
|
|
|
|
public:
|
|
|
|
static sk_sp<RadialGradient> Make() {
|
|
|
|
return sk_sp<RadialGradient>(new RadialGradient());
|
|
|
|
}
|
|
|
|
|
|
|
|
SG_ATTRIBUTE(StartCenter, SkPoint , fStartCenter)
|
|
|
|
SG_ATTRIBUTE(EndCenter , SkPoint , fEndCenter )
|
|
|
|
SG_ATTRIBUTE(StartRadius, SkScalar, fStartRadius)
|
|
|
|
SG_ATTRIBUTE(EndRadius , SkScalar, fEndRadius )
|
|
|
|
|
|
|
|
protected:
|
2019-11-05 14:22:39 +00:00
|
|
|
sk_sp<SkShader> onMakeShader(const std::vector<SkColor4f>&,
|
|
|
|
const std::vector<SkScalar >&) const override;
|
2018-01-12 17:25:09 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
RadialGradient() = default;
|
|
|
|
|
|
|
|
SkPoint fStartCenter = SkPoint::Make(0, 0),
|
|
|
|
fEndCenter = SkPoint::Make(0, 0);
|
|
|
|
SkScalar fStartRadius = 0,
|
|
|
|
fEndRadius = 0;
|
|
|
|
|
|
|
|
using INHERITED = Gradient;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sksg
|
|
|
|
|
|
|
|
#endif // SkSGGradient_DEFINED
|