1use core::hash::{Hash, Hasher};
4
5use crate::color::Color;
6
7#[derive(Clone, Copy, Debug, PartialEq)]
8pub enum Fill<'a> {
9 Solid(Color),
10 LinearGradient(LinearGradient<'a>),
11 ConicGradient(ConicGradient<'a>),
12}
13
14impl Fill<'_> {
15 pub fn normalized(self) -> Self {
17 let (stops, fill) = match self {
18 Self::Solid(_) => return self,
19 Self::LinearGradient(mut gradient) => {
20 gradient.angle = if gradient.angle.is_finite() {
21 let angle = gradient.angle % 360.0;
22 if angle < 0.0 { angle + 360.0 } else { angle }
23 } else {
24 0.0
25 };
26 (gradient.stops, Self::LinearGradient(gradient))
27 }
28 Self::ConicGradient(mut gradient) => {
29 gradient.angle = if gradient.angle.is_finite() {
30 let angle = gradient.angle % 360.0;
31 if angle < 0.0 { angle + 360.0 } else { angle }
32 } else {
33 0.0
34 };
35 gradient.center = gradient.center.map(|value| if value.is_finite() { value } else { 0.5 });
36 (gradient.stops, Self::ConicGradient(gradient))
37 }
38 };
39 if stops.len() >= 2
40 && stops
41 .iter()
42 .all(|stop| stop.position.is_finite() && (0.0..=1.0).contains(&stop.position))
43 && stops.windows(2).all(|stops| stops[0].position < stops[1].position)
44 {
45 fill
46 } else {
47 Self::Solid(stops.first().map_or(Color::TRANSPARENT, |stop| stop.color))
48 }
49 }
50}
51
52impl Hash for Fill<'_> {
53 fn hash<H: Hasher>(&self, state: &mut H) {
54 core::mem::discriminant(self).hash(state);
55 let (stops, angle) = match *self {
56 Fill::Solid(color) => return color.hash(state),
57 Fill::LinearGradient(gradient) => (gradient.stops, gradient.angle),
58 Fill::ConicGradient(gradient) => {
59 gradient
60 .center
61 .map(|value| if value == 0.0 { 0 } else { value.to_bits() })
62 .hash(state);
63 (gradient.stops, gradient.angle)
64 }
65 };
66 state.write_u32(if angle == 0.0 { 0 } else { angle.to_bits() });
67 stops.len().hash(state);
68 for stop in stops {
69 let position = stop.position;
70 state.write_u32(if position == 0.0 { 0 } else { position.to_bits() });
71 stop.color.hash(state);
72 }
73 }
74}
75
76impl Default for Fill<'_> {
77 fn default() -> Self {
78 Self::Solid(Color::TRANSPARENT)
79 }
80}
81
82impl From<Color> for Fill<'_> {
83 fn from(color: Color) -> Self {
84 Self::Solid(color)
85 }
86}
87
88impl<'a> From<LinearGradient<'a>> for Fill<'a> {
89 fn from(gradient: LinearGradient<'a>) -> Self {
90 Self::LinearGradient(gradient)
91 }
92}
93
94impl<'a> From<ConicGradient<'a>> for Fill<'a> {
95 fn from(gradient: ConicGradient<'a>) -> Self {
96 Self::ConicGradient(gradient)
97 }
98}
99
100#[derive(Clone, Copy, Debug, Default, PartialEq)]
101pub struct Border<'a> {
102 pub width: f32,
103 pub fill: Fill<'a>,
104}
105
106impl<'a> Border<'a> {
107 pub const fn solid(width: f32, color: Color) -> Self {
108 Self {
109 width,
110 fill: Fill::Solid(color),
111 }
112 }
113
114 pub fn gradient(width: f32, gradient: impl Into<Fill<'a>>) -> Self {
115 Self {
116 width,
117 fill: gradient.into(),
118 }
119 }
120}
121
122pub type BorderRadius = blit::Corners<f32>;
123
124blit::builder! {
125 #[derive(Clone, Copy, Debug, PartialEq)]
126 pub struct LinearGradient<'a> {
127 new(stops: &'a [GradientStop]),
128 angle: f32 = 0.0,
129 }
130}
131
132blit::builder! {
133 #[derive(Clone, Copy, Debug, PartialEq)]
136 pub struct ConicGradient<'a> {
137 new(stops: &'a [GradientStop]),
138 center: [f32; 2] = [0.5, 0.5],
139 angle: f32 = 0.0,
140 }
141}
142
143#[derive(Clone, Copy, Debug, PartialEq)]
144pub struct GradientStop {
145 pub position: f32,
146 pub color: Color,
147}
148
149impl GradientStop {
150 pub const fn new(position: f32, color: Color) -> Self {
151 Self { position, color }
152 }
153}