Debian Codegraph @

include/effects/SkGradient.h

raw
1 /*
2 * Copyright 2025 Google LLC.
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 SkGradient_DEFINED
9 #define SkGradient_DEFINED
10
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkShader.h"
15 #include "include/core/SkSpan.h"
16 #include "include/core/SkTileMode.h"
17 #include "include/private/base/SkAPI.h"
18
19 class SkMatrix;
20
21 class SkGradient {
22 public:
23 struct Interpolation {
24 enum class InPremul : bool { kNo = false, kYes = true };
25
26 enum class ColorSpace : uint8_t {
27 // Default Skia behavior: interpolate in the color space of the destination surface
28 kDestination,
29
30 // https://www.w3.org/TR/css-color-4/#interpolation-space
31 kSRGBLinear,
32 kLab,
33 kOKLab,
34 // This is the same as kOKLab, except it has a simplified version of the CSS gamut
35 // mapping algorithm (https://www.w3.org/TR/css-color-4/#css-gamut-mapping)
36 // into Rec2020 space applied to it.
37 // Warning: This space is experimental and should not be used in production.
38 kOKLabGamutMap,
39 kLCH,
40 kOKLCH,
41 // This is the same as kOKLCH, except it has the same gamut mapping applied to it
42 // as kOKLabGamutMap does.
43 // Warning: This space is experimental and should not be used in production.
44 kOKLCHGamutMap,
45 kSRGB,
46 kHSL,
47 kHWB,
48
49 kDisplayP3,
50 kRec2020,
51 kProphotoRGB,
52 kA98RGB,
53
54 kLastColorSpace = kA98RGB,
55 };
56 static constexpr int kColorSpaceCount = static_cast<int>(ColorSpace::kLastColorSpace) + 1;
57
58 enum class HueMethod : uint8_t {
59 // https://www.w3.org/TR/css-color-4/#hue-interpolation
60 kShorter,
61 kLonger,
62 kIncreasing,
63 kDecreasing,
64
65 kLastHueMethod = kDecreasing,
66 };
67 static constexpr int kHueMethodCount = static_cast<int>(HueMethod::kLastHueMethod) + 1;
68
69 InPremul fInPremul = InPremul::kNo;
70 ColorSpace fColorSpace = ColorSpace::kDestination;
71 HueMethod fHueMethod = HueMethod::kShorter; // Only relevant for LCH, OKLCH, HSL, or HWB
72
73 // legacy compatibility -- remove when we can
74 static Interpolation FromFlags(uint32_t flags) {
75 return {flags & 1 ? InPremul::kYes : InPremul::kNo,
76 ColorSpace::kDestination,
77 HueMethod::kShorter};
78 }
79 };
80
81 /**
82 * Specification for the colors in a gradient.
83 *
84 * @param colors The span of colors for the gradient.
85 * @param pos Relative positions of each color across the gradient. If empty,
86 * the the colors are distributed evenly. If this is not null, the values
87 * must lie between 0.0 and 1.0, and be strictly increasing. If the first
88 * value is not 0.0, then an additional color stop is added at position 0.0,
89 * with the same color as colors[0]. If the the last value is less than 1.0,
90 * then an additional color stop is added at position 1.0, with the same color
91 * as colors[count - 1].
92 * @param mode Tiling mode for the gradient.
93 * @param cs Optional colorspace associated with the span of colors. If this is null,
94 * the colors are treated as sRGB.
95 */
96 class Colors {
97 public:
98 Colors() {}
99 Colors(SkSpan<const SkColor4f> colors,
100 SkSpan<const float> pos,
101 SkTileMode mode,
102 sk_sp<SkColorSpace> cs = nullptr)
103 : fColors(colors), fPos(pos), fColorSpace(std::move(cs)), fTileMode(mode) {
104 SkASSERT(fPos.size() == 0 || fPos.size() == fColors.size());
105
106 // throw away inconsistent inputs
107 if (fPos.size() != fColors.size()) {
108 fPos = {};
109 }
110 }
111
112 Colors(SkSpan<const SkColor4f> colors, SkTileMode tm, sk_sp<SkColorSpace> cs = nullptr)
113 : Colors(colors, {}, tm, std::move(cs)) {}
114
115 SkSpan<const SkColor4f> colors() const { return fColors; }
116 SkSpan<const float> positions() const { return fPos; }
117 const sk_sp<SkColorSpace>& colorSpace() const { return fColorSpace; }
118 SkTileMode tileMode() const { return fTileMode; }
119
120 private:
121 SkSpan<const SkColor4f> fColors;
122 SkSpan<const float> fPos;
123 sk_sp<SkColorSpace> fColorSpace;
124 SkTileMode fTileMode = SkTileMode::kClamp;
125 };
126
127 SkGradient() {}
128 SkGradient(const Colors& colors, const Interpolation& interp)
129 : fColors(colors), fInterpolation(interp) {}
130
131 const Colors& colors() const { return fColors; }
132 const Interpolation& interpolation() const { return fInterpolation; }
133
134 private:
135 Colors fColors;
136 Interpolation fInterpolation;
137 };
138
139 namespace SkShaders {
140 /** Returns a shader that generates a linear gradient between the two specified points.
141 * If the inputs are invalid, this will return nullptr.
142 * @param points Array of 2 points, the end-points of the line segment
143 * @param grad Description of the colors and interpolation method
144 * @param lm Optional local matrix, may be null
145 */
146 SK_API sk_sp<SkShader> LinearGradient(const SkPoint pts[2],
147 const SkGradient&,
148 const SkMatrix* lm = nullptr);
149
150 /** Returns a shader that generates a radial gradient given the center and radius.
151 * @param center The center of the circle for this gradient
152 * @param radius Must be positive. The radius of the circle for this gradient
153 * @param grad Description of the colors and interpolation method
154 * @param lm Optional local matrix, may be null
155 */
156 SK_API sk_sp<SkShader> RadialGradient(SkPoint center,
157 float radius,
158 const SkGradient& grad,
159 const SkMatrix* lm = nullptr);
160
161 /** Returns a shader that generates a conical gradient given two circles, or
162 * returns null if the inputs are invalid. The gradient interprets the
163 * two circles according to the following HTML spec.
164 * http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient
165 * @param start The center of the circle for this gradient
166 * @param startRadius Must be positive. The radius of the circle for this gradient
167 * @param end The center of the circle for this gradient
168 * @param startRadius Must be positive. The radius of the circle for this gradient
169 * @param grad Description of the colors and interpolation method
170 * @param lm Optional local matrix, may be null
171 */
172 SK_API sk_sp<SkShader> TwoPointConicalGradient(SkPoint start,
173 float startRadius,
174 SkPoint end,
175 float endRadius,
176 const SkGradient& grad,
177 const SkMatrix* lm = nullptr);
178
179 /** Returns a shader that generates a sweep gradient given a center.
180 * The shader accepts negative angles and angles larger than 360, draws
181 * between 0 and 360 degrees, similar to the CSS conic-gradient
182 * semantics. 0 degrees means horizontal positive x axis. The start angle
183 * must be less than the end angle, otherwise a null pointer is
184 * returned. If color stops do not contain 0 and 1 but are within this
185 * range, the respective outer color stop is repeated for 0 and 1. Color
186 * stops less than 0 are clamped to 0, and greater than 1 are clamped to 1.
187 * @param center The center of the sweep
188 * @param startAngle Start of the angular range, corresponding to pos == 0.
189 * @param endAngle End of the angular range, corresponding to pos == 1.
190 * @param grad Description of the colors and interpolation method
191 * @param lm Optional local matrix, may be null
192 */
193 SK_API sk_sp<SkShader> SweepGradient(SkPoint center,
194 float startAngle,
195 float endAngle,
196 const SkGradient&,
197 const SkMatrix* lm = nullptr);
198 static inline sk_sp<SkShader> SweepGradient(SkPoint center,
199 const SkGradient& grad,
200 const SkMatrix* lm = nullptr) {
201 return SweepGradient(center, 0, 360, grad, lm);
202 }
203
204 } // namespace SkShaders
205
206 #endif