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(r.clamp(0.0, 1.0), g.clamp(0.0, 1.0), b.clamp(0.0, 1.0))
18    }
19
20    /// Creates a grayscale color with value clamped to 0.0-1.0.
21    pub fn gray(value: f64) -> Self {
22        Color::Gray(value.clamp(0.0, 1.0))
23    }
24
25    /// Creates a CMYK color with values clamped to 0.0-1.0.
26    pub fn cmyk(c: f64, m: f64, y: f64, k: f64) -> Self {
27        Color::Cmyk(
28            c.clamp(0.0, 1.0),
29            m.clamp(0.0, 1.0),
30            y.clamp(0.0, 1.0),
31            k.clamp(0.0, 1.0),
32        )
33    }
34
35    /// Black color (gray 0.0).
36    pub fn black() -> Self {
37        Color::Gray(0.0)
38    }
39
40    /// White color (gray 1.0).
41    pub fn white() -> Self {
42        Color::Gray(1.0)
43    }
44
45    /// Red color (RGB 1,0,0).
46    pub fn red() -> Self {
47        Color::Rgb(1.0, 0.0, 0.0)
48    }
49
50    /// Green color (RGB 0,1,0).
51    pub fn green() -> Self {
52        Color::Rgb(0.0, 1.0, 0.0)
53    }
54
55    /// Blue color (RGB 0,0,1).
56    pub fn blue() -> Self {
57        Color::Rgb(0.0, 0.0, 1.0)
58    }
59
60    pub fn yellow() -> Self {
61        Color::Rgb(1.0, 1.0, 0.0)
62    }
63
64    pub fn cyan() -> Self {
65        Color::Rgb(0.0, 1.0, 1.0)
66    }
67
68    pub fn magenta() -> Self {
69        Color::Rgb(1.0, 0.0, 1.0)
70    }
71}