primitives/foundation/
style.rs

1use std::cell::RefCell;
2
3use crate::foundation::colorspace::Color;
4
5/// Represents radial gradient specified by six parameters
6#[derive(Default, Copy, Clone, Debug)]
7pub struct RadialGradient {
8    /// Represents first point x position of gradient
9    pub x0: f64,
10    /// Represents first point y position of gradient
11    pub y0: f64,
12    /// Represents first point radius of gradient
13    pub r0: f64,
14    /// Represents second point x position of gradient
15    pub x1: f64,
16    /// Represents second point y position of gradient
17    pub y1: f64,
18    /// Represents second point radius of gradient
19    pub r1: f64,
20}
21
22impl RadialGradient {
23    /// Create new radial gradient with params
24    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/// Represents radial gradient specified by four parameters
37
38#[derive(Default, Copy, Clone, Debug)]
39pub struct LinearGradient {
40    /// Represents first point x position of gradient
41    pub x0: f64,
42    /// Represents first point y position of gradient
43    pub y0: f64,
44    /// Represents second point x position of gradient
45    pub x1: f64,
46    /// Represents second point y position of gradient
47    pub y1: f64,
48}
49
50impl LinearGradient {
51    /// Create new linear gradient with params
52    pub fn new(x0: f64, y0: f64, x1: f64, y1: f64) -> Self {
53        Self { x0, y0, x1, y1 }
54    }
55}
56
57
58/// Define the an offset and a color, to a given canvas gradient. 
59#[derive(Debug, Copy, Clone)]
60pub struct ColorStop {
61    /// Reperesent the offset of color stop
62    pub offset: f64,
63    /// Reperesent the color
64    pub color: Color,
65}
66
67impl ColorStop {
68    /// Create new color stop with params
69    pub fn new(offset: f64, color: Color) -> Self {
70        Self { offset, color }
71    }
72}
73
74/// Define the gradient type with parameters
75#[derive(Debug, Copy, Clone)]
76pub enum GradientType {
77    /// Linear gradient
78    Linear(LinearGradient),
79    /// Radial gradient
80    Radial(RadialGradient),
81}
82
83impl Default for GradientType {
84    fn default() -> Self {
85        GradientType::Linear(Default::default())
86    }
87}
88
89/// Represents an opaque object describing a gradient.
90#[derive(Debug, Clone)]
91pub struct Gradient {
92    /// Kind of gradient
93    pub kind: GradientType,
94    /// Color stop store
95    pub stops: RefCell<Vec<ColorStop>>,
96}
97
98impl Gradient {
99    /// Create new gradient with type
100    pub fn new(kind: GradientType) -> Self {
101        Self {
102            kind,
103            stops: Default::default(),
104        }
105    }
106
107    /// Create color stop to gradient
108    pub fn add_color_stop(&self, stop: ColorStop) {
109        let mut stops = self.stops.borrow_mut();
110        stops.push(stop)
111    }
112
113    /// Retrieve count of color stop's
114    pub fn color_count(&self) -> usize {
115        let stops = self.stops.borrow();
116        stops.len()
117    }
118
119    /// Retrieve color stop by index
120    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}