Skia
2D Graphics Library
SkGradientShader.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006 The Android Open Source Project
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 #ifndef SkGradientShader_DEFINED
9 #define SkGradientShader_DEFINED
10 
11 #include "include/core/SkColor.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkShader.h" // IWYU pragma: keep
18 #include "include/private/base/SkAPI.h"
19 
20 #include <cstdint>
21 #include <utility>
22 
23 class SkMatrix;
24 
59 class SK_API SkGradientShader {
60 public:
61  enum Flags {
68  kInterpolateColorsInPremul_Flag = 1 << 0,
69  };
70 
71  struct Interpolation {
72  enum class InPremul : bool { kNo = false, kYes = true };
73 
74  enum class ColorSpace : uint8_t {
75  // Default Skia behavior: interpolate in the color space of the destination surface
76  kDestination,
77 
78  // https://www.w3.org/TR/css-color-4/#interpolation-space
79  kSRGBLinear,
80  kLab,
81  kOKLab,
82  kLCH,
83  kOKLCH,
84  kSRGB,
85  kHSL,
86  kHWB,
87 
88  kLastColorSpace = kHWB,
89  };
90  static constexpr int kColorSpaceCount = static_cast<int>(ColorSpace::kLastColorSpace) + 1;
91 
92  enum class HueMethod : uint8_t {
93  // https://www.w3.org/TR/css-color-4/#hue-interpolation
94  kShorter,
95  kLonger,
96  kIncreasing,
97  kDecreasing,
98 
99  kLastHueMethod = kDecreasing,
100  };
101  static constexpr int kHueMethodCount = static_cast<int>(HueMethod::kLastHueMethod) + 1;
102 
103  InPremul fInPremul = InPremul::kNo;
104  ColorSpace fColorSpace = ColorSpace::kDestination;
105  HueMethod fHueMethod = HueMethod::kShorter; // Only relevant for LCH, OKLCH, HSL, or HWB
106 
107  static Interpolation FromFlags(uint32_t flags) {
108  return {flags & kInterpolateColorsInPremul_Flag ? InPremul::kYes : InPremul::kNo,
109  ColorSpace::kDestination,
110  HueMethod::kShorter};
111  }
112  };
113 
131  static sk_sp<SkShader> MakeLinear(const SkPoint pts[2],
132  const SkColor colors[], const SkScalar pos[], int count,
133  SkTileMode mode,
134  uint32_t flags = 0, const SkMatrix* localMatrix = nullptr);
135 
153  static sk_sp<SkShader> MakeLinear(const SkPoint pts[2],
154  const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
155  const SkScalar pos[], int count, SkTileMode mode,
156  const Interpolation& interpolation,
157  const SkMatrix* localMatrix);
158  static sk_sp<SkShader> MakeLinear(const SkPoint pts[2],
159  const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
160  const SkScalar pos[], int count, SkTileMode mode,
161  uint32_t flags = 0, const SkMatrix* localMatrix = nullptr) {
162  return MakeLinear(pts, colors, std::move(colorSpace), pos, count, mode,
163  Interpolation::FromFlags(flags), localMatrix);
164  }
165 
182  static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius,
183  const SkColor colors[], const SkScalar pos[], int count,
184  SkTileMode mode,
185  uint32_t flags = 0, const SkMatrix* localMatrix = nullptr);
186 
203  static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius,
204  const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
205  const SkScalar pos[], int count, SkTileMode mode,
206  const Interpolation& interpolation,
207  const SkMatrix* localMatrix);
208  static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius,
209  const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
210  const SkScalar pos[], int count, SkTileMode mode,
211  uint32_t flags = 0, const SkMatrix* localMatrix = nullptr) {
212  return MakeRadial(center, radius, colors, std::move(colorSpace), pos, count, mode,
213  Interpolation::FromFlags(flags), localMatrix);
214  }
215 
222  static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius,
223  const SkPoint& end, SkScalar endRadius,
224  const SkColor colors[], const SkScalar pos[],
225  int count, SkTileMode mode,
226  uint32_t flags = 0,
227  const SkMatrix* localMatrix = nullptr);
228 
235  static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius,
236  const SkPoint& end, SkScalar endRadius,
237  const SkColor4f colors[],
238  sk_sp<SkColorSpace> colorSpace, const SkScalar pos[],
239  int count, SkTileMode mode,
240  const Interpolation& interpolation,
241  const SkMatrix* localMatrix);
242  static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius,
243  const SkPoint& end, SkScalar endRadius,
244  const SkColor4f colors[],
245  sk_sp<SkColorSpace> colorSpace, const SkScalar pos[],
246  int count, SkTileMode mode,
247  uint32_t flags = 0,
248  const SkMatrix* localMatrix = nullptr) {
249  return MakeTwoPointConical(start, startRadius, end, endRadius, colors,
250  std::move(colorSpace), pos, count, mode,
251  Interpolation::FromFlags(flags), localMatrix);
252  }
253 
282  const SkColor colors[], const SkScalar pos[], int count,
283  SkTileMode mode,
284  SkScalar startAngle, SkScalar endAngle,
285  uint32_t flags, const SkMatrix* localMatrix);
287  const SkColor colors[], const SkScalar pos[], int count,
288  uint32_t flags = 0, const SkMatrix* localMatrix = nullptr) {
289  return MakeSweep(cx, cy, colors, pos, count, SkTileMode::kClamp, 0, 360, flags,
290  localMatrix);
291  }
292 
321  const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
322  const SkScalar pos[], int count,
323  SkTileMode mode,
324  SkScalar startAngle, SkScalar endAngle,
325  const Interpolation& interpolation,
326  const SkMatrix* localMatrix);
328  const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
329  const SkScalar pos[], int count,
330  SkTileMode mode,
331  SkScalar startAngle, SkScalar endAngle,
332  uint32_t flags, const SkMatrix* localMatrix) {
333  return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count, mode, startAngle,
334  endAngle, Interpolation::FromFlags(flags), localMatrix);
335  }
337  const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
338  const SkScalar pos[], int count,
339  uint32_t flags = 0, const SkMatrix* localMatrix = nullptr) {
340  return MakeSweep(cx, cy, colors, std::move(colorSpace), pos, count, SkTileMode::kClamp,
341  0, 360, flags, localMatrix);
342  }
343 };
344 
345 #endif
Types, consts, functions, and macros for colors.
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:37
@ kYes
Do pre-clip the geometry before applying the (perspective) matrix.
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
float SkScalar
Definition: SkScalar.h:14
SkTileMode
Definition: SkTileMode.h:13
@ kClamp
Replicate the edge color if the shader draws outside of its original bounds.
SkGradientShader hosts factories for creating subclasses of SkShader that render linear and radial gr...
Definition: SkGradientShader.h:59
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor4f colors[], sk_sp< SkColorSpace > colorSpace, const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
Definition: SkGradientShader.h:208
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
Returns a shader that generates a linear gradient between the two specified points.
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
Returns a shader that generates a radial gradient given the center and radius.
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor4f colors[], sk_sp< SkColorSpace > colorSpace, const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
Definition: SkGradientShader.h:327
static sk_sp< SkShader > MakeTwoPointConical(const SkPoint &start, SkScalar startRadius, const SkPoint &end, SkScalar endRadius, const SkColor4f colors[], sk_sp< SkColorSpace > colorSpace, const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
Definition: SkGradientShader.h:242
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor4f colors[], sk_sp< SkColorSpace > colorSpace, const SkScalar pos[], int count, SkTileMode mode, const Interpolation &interpolation, const SkMatrix *localMatrix)
Returns a shader that generates a radial gradient given the center and radius.
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor4f colors[], sk_sp< SkColorSpace > colorSpace, const SkScalar pos[], int count, SkTileMode mode, const Interpolation &interpolation, const SkMatrix *localMatrix)
Returns a shader that generates a linear gradient between the two specified points.
static sk_sp< SkShader > MakeTwoPointConical(const SkPoint &start, SkScalar startRadius, const SkPoint &end, SkScalar endRadius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
Returns a shader that generates a conical gradient given two circles, or returns NULL if the inputs a...
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor4f colors[], sk_sp< SkColorSpace > colorSpace, const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, const Interpolation &interpolation, const SkMatrix *localMatrix)
Returns a shader that generates a sweep gradient given a center.
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
Returns a shader that generates a sweep gradient given a center.
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor4f colors[], sk_sp< SkColorSpace > colorSpace, const SkScalar pos[], int count, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
Definition: SkGradientShader.h:336
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor4f colors[], sk_sp< SkColorSpace > colorSpace, const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
Definition: SkGradientShader.h:158
static sk_sp< SkShader > MakeTwoPointConical(const SkPoint &start, SkScalar startRadius, const SkPoint &end, SkScalar endRadius, const SkColor4f colors[], sk_sp< SkColorSpace > colorSpace, const SkScalar pos[], int count, SkTileMode mode, const Interpolation &interpolation, const SkMatrix *localMatrix)
Returns a shader that generates a conical gradient given two circles, or returns NULL if the inputs a...
Flags
Definition: SkGradientShader.h:61
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
Definition: SkGradientShader.h:286
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
RGBA color value, holding four floating point components.
Definition: SkGradientShader.h:71
InPremul
Definition: SkGradientShader.h:72
HueMethod
Definition: SkGradientShader.h:92
static Interpolation FromFlags(uint32_t flags)
Definition: SkGradientShader.h:107
ColorSpace
Definition: SkGradientShader.h:74