floem_peniko/
brush.rs

1// Copyright 2022 The peniko authors.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4use super::{Color, Gradient, Image};
5
6/// Describes the color content of a filled or stroked shape.
7#[derive(Clone, PartialEq, Debug)]
8pub enum Brush {
9    /// Solid color brush.
10    Solid(Color),
11    /// Gradient brush.
12    Gradient(Gradient),
13    /// Image brush.
14    Image(Image),
15}
16
17impl From<Color> for Brush {
18    fn from(c: Color) -> Self {
19        Self::Solid(c)
20    }
21}
22
23impl From<Gradient> for Brush {
24    fn from(g: Gradient) -> Self {
25        Self::Gradient(g)
26    }
27}
28
29/// Reference to a brush.
30///
31/// This is useful for methods that would like to accept brushes by reference. Defining
32/// the type as `impl<Into<BrushRef>>` allows accepting types like `&LinearGradient`
33/// directly without cloning or allocating.
34#[derive(Clone, PartialEq, Debug)]
35pub enum BrushRef<'a> {
36    /// Solid color brush.
37    Solid(Color),
38    /// Gradient brush.
39    Gradient(&'a Gradient),
40    /// Image brush.
41    Image(&'a Image),
42}
43
44impl<'a> BrushRef<'a> {
45    /// Converts the reference to an owned brush.
46    pub fn to_owned(&self) -> Brush {
47        match self {
48            Self::Solid(color) => Brush::Solid(*color),
49            Self::Gradient(gradient) => Brush::Gradient((*gradient).clone()),
50            Self::Image(image) => Brush::Image((*image).clone()),
51        }
52    }
53}
54
55impl From<Color> for BrushRef<'_> {
56    fn from(color: Color) -> Self {
57        Self::Solid(color)
58    }
59}
60
61impl<'a> From<&'a Color> for BrushRef<'_> {
62    fn from(color: &'a Color) -> Self {
63        Self::Solid(*color)
64    }
65}
66
67impl<'a> From<&'a Gradient> for BrushRef<'a> {
68    fn from(gradient: &'a Gradient) -> Self {
69        Self::Gradient(gradient)
70    }
71}
72
73impl<'a> From<&'a Image> for BrushRef<'a> {
74    fn from(image: &'a Image) -> Self {
75        Self::Image(image)
76    }
77}
78
79impl<'a> From<&'a Brush> for BrushRef<'a> {
80    fn from(brush: &'a Brush) -> Self {
81        match brush {
82            Brush::Solid(color) => Self::Solid(*color),
83            Brush::Gradient(gradient) => Self::Gradient(gradient),
84            Brush::Image(image) => Self::Image(image),
85        }
86    }
87}
88
89/// Defines how a brush is extended when the content does not
90/// fill a shape.
91#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
92pub enum Extend {
93    /// Extends the image by repeating the edge color of the brush.
94    #[default]
95    Pad,
96    /// Extends the image by repeating the brush.
97    Repeat,
98    /// Extends the image by reflecting the brush.
99    Reflect,
100}