oxidize_pdf/graphics/
color.rs

1/// Represents a color in PDF documents.
2/// 
3/// Supports RGB, Grayscale, and CMYK color spaces.
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub enum Color {
6    /// RGB color (red, green, blue) with values from 0.0 to 1.0
7    Rgb(f64, f64, f64),
8    /// Grayscale color with value from 0.0 (black) to 1.0 (white)
9    Gray(f64),
10    /// CMYK color (cyan, magenta, yellow, key/black) with values from 0.0 to 1.0
11    Cmyk(f64, f64, f64, f64),
12}
13
14impl Color {
15    /// Creates an RGB color with values clamped to 0.0-1.0.
16    pub fn rgb(r: f64, g: f64, b: f64) -> Self {
17        Color::Rgb(
18            r.clamp(0.0, 1.0),
19            g.clamp(0.0, 1.0),
20            b.clamp(0.0, 1.0),
21        )
22    }
23    
24    /// Creates a grayscale color with value clamped to 0.0-1.0.
25    pub fn gray(value: f64) -> Self {
26        Color::Gray(value.clamp(0.0, 1.0))
27    }
28    
29    /// Creates a CMYK color with values clamped to 0.0-1.0.
30    pub fn cmyk(c: f64, m: f64, y: f64, k: f64) -> Self {
31        Color::Cmyk(
32            c.clamp(0.0, 1.0),
33            m.clamp(0.0, 1.0),
34            y.clamp(0.0, 1.0),
35            k.clamp(0.0, 1.0),
36        )
37    }
38    
39    /// Black color (gray 0.0).
40    pub fn black() -> Self {
41        Color::Gray(0.0)
42    }
43    
44    /// White color (gray 1.0).
45    pub fn white() -> Self {
46        Color::Gray(1.0)
47    }
48    
49    /// Red color (RGB 1,0,0).
50    pub fn red() -> Self {
51        Color::Rgb(1.0, 0.0, 0.0)
52    }
53    
54    /// Green color (RGB 0,1,0).
55    pub fn green() -> Self {
56        Color::Rgb(0.0, 1.0, 0.0)
57    }
58    
59    /// Blue color (RGB 0,0,1).
60    pub fn blue() -> Self {
61        Color::Rgb(0.0, 0.0, 1.0)
62    }
63    
64    pub fn yellow() -> Self {
65        Color::Rgb(1.0, 1.0, 0.0)
66    }
67    
68    pub fn cyan() -> Self {
69        Color::Rgb(0.0, 1.0, 1.0)
70    }
71    
72    pub fn magenta() -> Self {
73        Color::Rgb(1.0, 0.0, 1.0)
74    }
75}