qr_code_styling/config/
gradient.rs1use super::Color;
4use crate::types::GradientType;
5
6#[derive(Debug, Clone, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct ColorStop {
10 pub offset: f64,
12 pub color: Color,
14}
15
16impl ColorStop {
17 pub fn new(offset: f64, color: Color) -> Self {
19 Self {
20 offset: offset.clamp(0.0, 1.0),
21 color,
22 }
23 }
24}
25
26#[derive(Debug, Clone, PartialEq)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29pub struct Gradient {
30 pub gradient_type: GradientType,
32 pub rotation: f64,
34 pub color_stops: Vec<ColorStop>,
36}
37
38impl Gradient {
39 pub fn linear(color_stops: Vec<ColorStop>) -> Self {
41 Self {
42 gradient_type: GradientType::Linear,
43 rotation: 0.0,
44 color_stops,
45 }
46 }
47
48 pub fn linear_rotated(rotation: f64, color_stops: Vec<ColorStop>) -> Self {
50 Self {
51 gradient_type: GradientType::Linear,
52 rotation,
53 color_stops,
54 }
55 }
56
57 pub fn radial(color_stops: Vec<ColorStop>) -> Self {
59 Self {
60 gradient_type: GradientType::Radial,
61 rotation: 0.0,
62 color_stops,
63 }
64 }
65
66 pub fn simple_linear(start: Color, end: Color) -> Self {
68 Self::linear(vec![
69 ColorStop::new(0.0, start),
70 ColorStop::new(1.0, end),
71 ])
72 }
73
74 pub fn simple_radial(center: Color, edge: Color) -> Self {
76 Self::radial(vec![
77 ColorStop::new(0.0, center),
78 ColorStop::new(1.0, edge),
79 ])
80 }
81}
82
83impl Default for Gradient {
84 fn default() -> Self {
85 Self::simple_linear(Color::BLACK, Color::BLACK)
86 }
87}