1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Brush definitions for painting (solid colors, gradients, etc.)
use crate::{
color::Color,
geometry::{Point, Rect},
render_effect::TileMode,
};
/// Describes how a shape, path or text is filled: a flat color or a gradient. Compose's `Brush`.
#[derive(Clone, Debug, PartialEq)]
pub enum Brush {
/// A single flat color. Compose's `SolidColor`.
Solid(Color),
/// Colors blend along the line from `start` to `end`. Compose's `Brush.linearGradient`.
LinearGradient {
/// Colors to blend between, in order, from `start` to `end`.
colors: Vec<Color>,
/// Positions of `colors` along the line from `start` to `end`, from 0.0 to 1.0; `None`
/// spaces the colors evenly.
stops: Option<Vec<f32>>,
/// Where the first color is reached, in logical pixels relative to the top-left corner of
/// the painted area. An infinite coordinate means the far edge of that area.
start: Point,
/// Where the last color is reached, in logical pixels relative to the top-left corner of
/// the painted area. An infinite coordinate means the far edge of that area.
end: Point,
/// How the gradient continues beyond `start` and `end`.
tile_mode: TileMode,
},
/// Colors blend with distance from `center`. Compose's `Brush.radialGradient`.
RadialGradient {
/// Colors to blend between, in order, from `center` outward.
colors: Vec<Color>,
/// Positions of `colors` from 0.0 to 1.0; `None` spaces the colors evenly.
stops: Option<Vec<f32>>,
/// Gradient center in logical pixels, relative to the top-left corner of the painted area.
center: Point,
/// Distance in logical pixels from `center` at which the last color is reached.
radius: f32,
/// How the gradient continues beyond `radius`.
tile_mode: TileMode,
},
/// Colors blend by angle around `center`, the first at 3 o'clock and the
/// rest clockwise from it. Compose's `Brush.sweepGradient`.
SweepGradient {
/// Colors to blend between, in order.
colors: Vec<Color>,
/// Positions of `colors` from 0.0 to 1.0; `None` spaces the colors evenly.
stops: Option<Vec<f32>>,
/// Gradient center in logical pixels, relative to the top-left corner of the painted area.
center: Point,
},
}
fn split_color_stops(color_stops: Vec<(f32, Color)>) -> (Vec<Color>, Vec<f32>) {
let mut colors = Vec::with_capacity(color_stops.len());
let mut stops = Vec::with_capacity(color_stops.len());
for (stop, color) in color_stops {
colors.push(color);
stops.push(stop);
}
(colors, stops)
}
/// A colour paints as a solid brush wherever a brush is taken, as Compose
/// takes a `Color` beside each `Brush` overload.
impl From<Color> for Brush {
fn from(color: Color) -> Self {
Brush::Solid(color)
}
}
impl Brush {
/// Creates a brush that paints one flat color.
pub fn solid(color: Color) -> Self {
Brush::Solid(color)
}
/// Creates a linear gradient that defaults to Compose semantics:
/// start at `(0,0)`, end at `(+inf,+inf)`, and `TileMode::Clamp`.
pub fn linear_gradient(colors: Vec<Color>) -> Self {
Self::linear_gradient_with_tile_mode(
colors,
Point { x: 0.0, y: 0.0 },
Point {
x: f32::INFINITY,
y: f32::INFINITY,
},
TileMode::Clamp,
)
}
/// Creates a linear gradient that spreads `colors` evenly from `start` to `end`, clamping
/// outside that line.
pub fn linear_gradient_range(colors: Vec<Color>, start: Point, end: Point) -> Self {
Self::linear_gradient_with_tile_mode(colors, start, end, TileMode::Clamp)
}
/// Creates a linear gradient that spreads `colors` evenly from `start` to `end`, repeating or
/// clamping outside that line as `tile_mode` says.
pub fn linear_gradient_with_tile_mode(
colors: Vec<Color>,
start: Point,
end: Point,
tile_mode: TileMode,
) -> Self {
Brush::LinearGradient {
colors,
stops: None,
start,
end,
tile_mode,
}
}
/// Creates a linear gradient from `(position, color)` pairs, with positions from 0.0 at `start`
/// to 1.0 at `end`.
pub fn linear_gradient_stops(
color_stops: Vec<(f32, Color)>,
start: Point,
end: Point,
tile_mode: TileMode,
) -> Self {
let (colors, stops) = split_color_stops(color_stops);
Brush::LinearGradient {
colors,
stops: Some(stops),
start,
end,
tile_mode,
}
}
/// Creates a vertical gradient that spreads `colors` evenly from `start_y` to `end_y`, clamping
/// outside that range.
pub fn vertical_gradient(colors: Vec<Color>, start_y: f32, end_y: f32) -> Self {
Self::vertical_gradient_tiled(colors, start_y, end_y, TileMode::Clamp)
}
/// Creates a vertical gradient that spreads `colors` evenly from `start_y` to `end_y`,
/// repeating or clamping outside that range as `tile_mode` says.
pub fn vertical_gradient_tiled(
colors: Vec<Color>,
start_y: f32,
end_y: f32,
tile_mode: TileMode,
) -> Self {
Self::linear_gradient_with_tile_mode(
colors,
Point { x: 0.0, y: start_y },
Point { x: 0.0, y: end_y },
tile_mode,
)
}
/// Creates a vertical gradient across the full height of the painted area, clamping at both
/// ends.
pub fn vertical_gradient_default(colors: Vec<Color>) -> Self {
Self::vertical_gradient_tiled(colors, 0.0, f32::INFINITY, TileMode::Clamp)
}
/// Creates a vertical gradient from `(position, color)` pairs, with positions from 0.0 at
/// `start_y` to 1.0 at `end_y`.
pub fn vertical_gradient_stops(
color_stops: Vec<(f32, Color)>,
start_y: f32,
end_y: f32,
tile_mode: TileMode,
) -> Self {
Self::linear_gradient_stops(
color_stops,
Point { x: 0.0, y: start_y },
Point { x: 0.0, y: end_y },
tile_mode,
)
}
/// Creates a horizontal gradient that spreads `colors` evenly from `start_x` to `end_x`,
/// clamping outside that range.
pub fn horizontal_gradient(colors: Vec<Color>, start_x: f32, end_x: f32) -> Self {
Self::horizontal_gradient_tiled(colors, start_x, end_x, TileMode::Clamp)
}
/// Creates a horizontal gradient that spreads `colors` evenly from `start_x` to `end_x`,
/// repeating or clamping outside that range as `tile_mode` says.
pub fn horizontal_gradient_tiled(
colors: Vec<Color>,
start_x: f32,
end_x: f32,
tile_mode: TileMode,
) -> Self {
Self::linear_gradient_with_tile_mode(
colors,
Point { x: start_x, y: 0.0 },
Point { x: end_x, y: 0.0 },
tile_mode,
)
}
/// Creates a horizontal gradient across the full width of the painted area, clamping at both
/// ends.
pub fn horizontal_gradient_default(colors: Vec<Color>) -> Self {
Self::horizontal_gradient_tiled(colors, 0.0, f32::INFINITY, TileMode::Clamp)
}
/// Creates a horizontal gradient from `(position, color)` pairs, with positions from 0.0 at
/// `start_x` to 1.0 at `end_x`.
pub fn horizontal_gradient_stops(
color_stops: Vec<(f32, Color)>,
start_x: f32,
end_x: f32,
tile_mode: TileMode,
) -> Self {
Self::linear_gradient_stops(
color_stops,
Point { x: start_x, y: 0.0 },
Point { x: end_x, y: 0.0 },
tile_mode,
)
}
/// Creates a radial gradient that spreads `colors` evenly from `center` out to `radius`,
/// clamping beyond it.
pub fn radial_gradient(colors: Vec<Color>, center: Point, radius: f32) -> Self {
Self::radial_gradient_tiled(colors, center, radius, TileMode::Clamp)
}
/// Creates a radial gradient that spreads `colors` evenly from `center` out to `radius`,
/// repeating or clamping beyond it as `tile_mode` says.
pub fn radial_gradient_tiled(
colors: Vec<Color>,
center: Point,
radius: f32,
tile_mode: TileMode,
) -> Self {
Brush::RadialGradient {
colors,
stops: None,
center,
radius,
tile_mode,
}
}
/// Creates a radial gradient from `(position, color)` pairs, with positions from 0.0 at the
/// center to 1.0 at `radius`.
pub fn radial_gradient_stops(
color_stops: Vec<(f32, Color)>,
center: Point,
radius: f32,
tile_mode: TileMode,
) -> Self {
let (colors, stops) = split_color_stops(color_stops);
Brush::RadialGradient {
colors,
stops: Some(stops),
center,
radius,
tile_mode,
}
}
/// Creates a sweep gradient that spreads `colors` evenly around `center`.
pub fn sweep_gradient(colors: Vec<Color>, center: Point) -> Self {
Brush::SweepGradient {
colors,
stops: None,
center,
}
}
/// Creates a sweep gradient from `(position, color)` pairs, with positions from 0.0 to 1.0
/// around `center`.
pub fn sweep_gradient_stops(color_stops: Vec<(f32, Color)>, center: Point) -> Self {
let (colors, stops) = split_color_stops(color_stops);
Brush::SweepGradient {
colors,
stops: Some(stops),
center,
}
}
}
impl Brush {
/// The color this brush paints at `point` when it fills `rect`, before
/// any dithering. Gradient coordinates are offsets from `rect`'s
/// top-left corner, and an infinite one is `rect`'s far edge. Where a
/// [`TileMode::Decal`] gradient paints nothing the color is transparent.
pub fn color_at(&self, rect: Rect, point: Point) -> Color {
let (x, y) = (point.x, point.y);
let (colors, stops, t, tile_mode) = match self {
Brush::Solid(color) => return *color,
Brush::LinearGradient {
colors,
stops,
start,
end,
tile_mode,
} => {
let sx = resolve_gradient_point(rect.x, rect.width, start.x);
let sy = resolve_gradient_point(rect.y, rect.height, start.y);
let ex = resolve_gradient_point(rect.x, rect.width, end.x);
let ey = resolve_gradient_point(rect.y, rect.height, end.y);
let (dx, dy) = (ex - sx, ey - sy);
let denom = (dx * dx + dy * dy).max(f32::EPSILON);
let t = ((x - sx) * dx + (y - sy) * dy) / denom;
(colors, stops, t, *tile_mode)
}
Brush::RadialGradient {
colors,
stops,
center,
radius,
tile_mode,
} => {
let dx = x - (rect.x + center.x);
let dy = y - (rect.y + center.y);
let t = (dx * dx + dy * dy).sqrt() / radius.max(f32::EPSILON);
(colors, stops, t, *tile_mode)
}
Brush::SweepGradient {
colors,
stops,
center,
} => {
let angle = (y - (rect.y + center.y)).atan2(x - (rect.x + center.x));
// From 3 o'clock, clockwise, as Compose's sweep.
let t = (angle / std::f32::consts::TAU).rem_euclid(1.0);
(colors, stops, t, TileMode::Clamp)
}
};
match tiled_gradient_t(t, tile_mode) {
Some(t) => interpolate_colors(colors, stops.as_deref(), t),
None => TRANSPARENT,
}
}
}
const TRANSPARENT: Color = Color(0.0, 0.0, 0.0, 0.0);
fn resolve_gradient_point(origin: f32, extent: f32, value: f32) -> f32 {
if value.is_finite() {
origin + value
} else if value.is_sign_positive() {
origin + extent
} else {
origin
}
}
/// Where `t` lands in the gradient's one span under `tile_mode`; `None`
/// past a decal gradient's ends.
fn tiled_gradient_t(t: f32, tile_mode: TileMode) -> Option<f32> {
match tile_mode {
TileMode::Clamp => Some(t.clamp(0.0, 1.0)),
TileMode::Decal => (0.0..=1.0).contains(&t).then_some(t),
TileMode::Repeated => Some(t.rem_euclid(1.0)),
TileMode::Mirror => {
let wrapped = t.rem_euclid(2.0);
Some(if wrapped <= 1.0 {
wrapped
} else {
2.0 - wrapped
})
}
}
}
fn interpolate_colors(colors: &[Color], stops: Option<&[f32]>, t: f32) -> Color {
let (Some(&first), Some(&last)) = (colors.first(), colors.last()) else {
return TRANSPARENT;
};
if colors.len() == 1 {
return first;
}
let clamped = t.clamp(0.0, 1.0);
if let Some(stops) = stops.filter(|stops| stops.len() == colors.len()) {
if stops.first().is_some_and(|&start| clamped <= start) {
return first;
}
for (pair, span) in colors.windows(2).zip(stops.windows(2)) {
if clamped <= span[1] {
let width = (span[1] - span[0]).max(f32::EPSILON);
return lerp_color(
pair[0],
pair[1],
((clamped - span[0]) / width).clamp(0.0, 1.0),
);
}
}
return last;
}
let scaled = clamped * (colors.len() - 1) as f32;
let index = scaled.floor() as usize;
match colors.get(index..index + 2) {
Some(&[from, to]) => lerp_color(from, to, scaled - index as f32),
_ => last,
}
}
fn lerp_color(a: Color, b: Color, t: f32) -> Color {
let lerp = |start: f32, end: f32| start + (end - start) * t;
Color(
lerp(a.0, b.0),
lerp(a.1, b.1),
lerp(a.2, b.2),
lerp(a.3, b.3),
)
}
#[cfg(test)]
#[path = "tests/brush_tests.rs"]
mod tests;