epaint/
stroke.rs

1#![allow(clippy::derived_hash_with_manual_eq)] // We need to impl Hash for f32, but we don't implement Eq, which is fine
2
3use std::{fmt::Debug, sync::Arc};
4
5use super::{emath, Color32, ColorMode, Pos2, Rect};
6
7/// Describes the width and color of a line.
8///
9/// The default stroke is the same as [`Stroke::NONE`].
10#[derive(Clone, Copy, Debug, Default, PartialEq)]
11#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
12pub struct Stroke {
13    pub width: f32,
14    pub color: Color32,
15}
16
17impl Stroke {
18    /// Same as [`Stroke::default`].
19    pub const NONE: Self = Self {
20        width: 0.0,
21        color: Color32::TRANSPARENT,
22    };
23
24    #[inline]
25    pub fn new(width: impl Into<f32>, color: impl Into<Color32>) -> Self {
26        Self {
27            width: width.into(),
28            color: color.into(),
29        }
30    }
31
32    /// True if width is zero or color is transparent
33    #[inline]
34    pub fn is_empty(&self) -> bool {
35        self.width <= 0.0 || self.color == Color32::TRANSPARENT
36    }
37}
38
39impl<Color> From<(f32, Color)> for Stroke
40where
41    Color: Into<Color32>,
42{
43    #[inline(always)]
44    fn from((width, color): (f32, Color)) -> Self {
45        Self::new(width, color)
46    }
47}
48
49impl std::hash::Hash for Stroke {
50    #[inline(always)]
51    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
52        let Self { width, color } = *self;
53        emath::OrderedFloat(width).hash(state);
54        color.hash(state);
55    }
56}
57
58/// Describes how the stroke of a shape should be painted.
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
61pub enum StrokeKind {
62    /// The stroke should be painted entirely inside of the shape
63    Inside,
64
65    /// The stroke should be painted right on the edge of the shape, half inside and half outside.
66    Middle,
67
68    /// The stroke should be painted entirely outside of the shape
69    Outside,
70}
71
72/// Describes the width and color of paths. The color can either be solid or provided by a callback. For more information, see [`ColorMode`]
73///
74/// The default stroke is the same as [`Stroke::NONE`].
75#[derive(Clone, Debug, PartialEq)]
76#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
77pub struct PathStroke {
78    pub width: f32,
79    pub color: ColorMode,
80    pub kind: StrokeKind,
81}
82
83impl Default for PathStroke {
84    #[inline]
85    fn default() -> Self {
86        Self::NONE
87    }
88}
89
90impl PathStroke {
91    /// Same as [`PathStroke::default`].
92    pub const NONE: Self = Self {
93        width: 0.0,
94        color: ColorMode::TRANSPARENT,
95        kind: StrokeKind::Middle,
96    };
97
98    #[inline]
99    pub fn new(width: impl Into<f32>, color: impl Into<Color32>) -> Self {
100        Self {
101            width: width.into(),
102            color: ColorMode::Solid(color.into()),
103            kind: StrokeKind::Middle,
104        }
105    }
106
107    /// Create a new `PathStroke` with a UV function
108    ///
109    /// The bounding box passed to the callback will have a margin of [`TessellationOptions::feathering_size_in_pixels`](`crate::tessellator::TessellationOptions::feathering_size_in_pixels`)
110    #[inline]
111    pub fn new_uv(
112        width: impl Into<f32>,
113        callback: impl Fn(Rect, Pos2) -> Color32 + Send + Sync + 'static,
114    ) -> Self {
115        Self {
116            width: width.into(),
117            color: ColorMode::UV(Arc::new(callback)),
118            kind: StrokeKind::Middle,
119        }
120    }
121
122    #[inline]
123    pub fn with_kind(self, kind: StrokeKind) -> Self {
124        Self { kind, ..self }
125    }
126
127    /// Set the stroke to be painted right on the edge of the shape, half inside and half outside.
128    #[inline]
129    pub fn middle(self) -> Self {
130        Self {
131            kind: StrokeKind::Middle,
132            ..self
133        }
134    }
135
136    /// Set the stroke to be painted entirely outside of the shape
137    #[inline]
138    pub fn outside(self) -> Self {
139        Self {
140            kind: StrokeKind::Outside,
141            ..self
142        }
143    }
144
145    /// Set the stroke to be painted entirely inside of the shape
146    #[inline]
147    pub fn inside(self) -> Self {
148        Self {
149            kind: StrokeKind::Inside,
150            ..self
151        }
152    }
153
154    /// True if width is zero or color is solid and transparent
155    #[inline]
156    pub fn is_empty(&self) -> bool {
157        self.width <= 0.0 || self.color == ColorMode::TRANSPARENT
158    }
159}
160
161impl<Color> From<(f32, Color)> for PathStroke
162where
163    Color: Into<Color32>,
164{
165    #[inline(always)]
166    fn from((width, color): (f32, Color)) -> Self {
167        Self::new(width, color)
168    }
169}
170
171impl From<Stroke> for PathStroke {
172    fn from(value: Stroke) -> Self {
173        if value.is_empty() {
174            // Important, since we use the stroke color when doing feathering of the fill!
175            Self::NONE
176        } else {
177            Self {
178                width: value.width,
179                color: ColorMode::Solid(value.color),
180                kind: StrokeKind::Middle,
181            }
182        }
183    }
184}