Skip to main content

qr_code_styling/config/
background_options.rs

1//! Background styling options.
2
3use super::{Color, Gradient};
4
5/// Options for styling QR code background.
6#[derive(Debug, Clone, PartialEq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct BackgroundOptions {
9    /// Solid color for background (ignored if gradient is set).
10    pub color: Color,
11    /// Optional gradient for background.
12    pub gradient: Option<Gradient>,
13    /// Corner radius ratio (0.0 to 1.0, where 0.5 = fully rounded).
14    pub round: f64,
15}
16
17impl Default for BackgroundOptions {
18    fn default() -> Self {
19        Self {
20            color: Color::WHITE,
21            gradient: None,
22            round: 0.0,
23        }
24    }
25}
26
27impl BackgroundOptions {
28    /// Create new background options with a specific color.
29    pub fn new(color: Color) -> Self {
30        Self {
31            color,
32            ..Default::default()
33        }
34    }
35
36    /// Create transparent background.
37    pub fn transparent() -> Self {
38        Self {
39            color: Color::TRANSPARENT,
40            gradient: None,
41            round: 0.0,
42        }
43    }
44
45    /// Set the color.
46    pub fn with_color(mut self, color: Color) -> Self {
47        self.color = color;
48        self
49    }
50
51    /// Set the gradient.
52    pub fn with_gradient(mut self, gradient: Gradient) -> Self {
53        self.gradient = Some(gradient);
54        self
55    }
56
57    /// Set the corner radius ratio.
58    pub fn with_round(mut self, round: f64) -> Self {
59        self.round = round.clamp(0.0, 0.5);
60        self
61    }
62}