primitives/foundation/
style.rs1use std::cell::RefCell;
2
3use crate::foundation::colorspace::Color;
4
5#[derive(Default, Copy, Clone, Debug)]
7pub struct RadialGradient {
8 pub x0: f64,
10 pub y0: f64,
12 pub r0: f64,
14 pub x1: f64,
16 pub y1: f64,
18 pub r1: f64,
20}
21
22impl RadialGradient {
23 pub fn new(x0: f64, y0: f64, r0: f64, x1: f64, y1: f64, r1: f64) -> Self {
25 Self {
26 x0,
27 y0,
28 r0,
29 x1,
30 y1,
31 r1,
32 }
33 }
34}
35
36#[derive(Default, Copy, Clone, Debug)]
39pub struct LinearGradient {
40 pub x0: f64,
42 pub y0: f64,
44 pub x1: f64,
46 pub y1: f64,
48}
49
50impl LinearGradient {
51 pub fn new(x0: f64, y0: f64, x1: f64, y1: f64) -> Self {
53 Self { x0, y0, x1, y1 }
54 }
55}
56
57
58#[derive(Debug, Copy, Clone)]
60pub struct ColorStop {
61 pub offset: f64,
63 pub color: Color,
65}
66
67impl ColorStop {
68 pub fn new(offset: f64, color: Color) -> Self {
70 Self { offset, color }
71 }
72}
73
74#[derive(Debug, Copy, Clone)]
76pub enum GradientType {
77 Linear(LinearGradient),
79 Radial(RadialGradient),
81}
82
83impl Default for GradientType {
84 fn default() -> Self {
85 GradientType::Linear(Default::default())
86 }
87}
88
89#[derive(Debug, Clone)]
91pub struct Gradient {
92 pub kind: GradientType,
94 pub stops: RefCell<Vec<ColorStop>>,
96}
97
98impl Gradient {
99 pub fn new(kind: GradientType) -> Self {
101 Self {
102 kind,
103 stops: Default::default(),
104 }
105 }
106
107 pub fn add_color_stop(&self, stop: ColorStop) {
109 let mut stops = self.stops.borrow_mut();
110 stops.push(stop)
111 }
112
113 pub fn color_count(&self) -> usize {
115 let stops = self.stops.borrow();
116 stops.len()
117 }
118
119 pub fn get_color_stop(&self, index: usize) -> Option<ColorStop> {
121 let stops = self.stops.borrow();
122 stops.get(index).copied()
123 }
124}
125
126impl Default for Gradient {
127 fn default() -> Self {
128 Self {
129 kind: Default::default(),
130 stops: Default::default(),
131 }
132 }
133}