oxidize-pdf 2.5.0

A pure Rust PDF generation and manipulation library with zero external dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/// Represents a color in PDF documents.
///
/// Supports RGB, Grayscale, and CMYK color spaces.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Color {
    /// RGB color (red, green, blue) with values from 0.0 to 1.0
    Rgb(f64, f64, f64),
    /// Grayscale color with value from 0.0 (black) to 1.0 (white)
    Gray(f64),
    /// CMYK color (cyan, magenta, yellow, key/black) with values from 0.0 to 1.0
    Cmyk(f64, f64, f64, f64),
}

impl Color {
    /// Creates an RGB color with values clamped to 0.0-1.0.
    pub fn rgb(r: f64, g: f64, b: f64) -> Self {
        Color::Rgb(r.clamp(0.0, 1.0), g.clamp(0.0, 1.0), b.clamp(0.0, 1.0))
    }

    /// Create a color from a hex string like "#RRGGBB"
    pub fn hex(hex_str: &str) -> Self {
        let hex = hex_str.trim_start_matches('#');
        if hex.len() != 6 {
            return Color::black(); // Default fallback
        }

        let r = u8::from_str_radix(&hex[0..2], 16).unwrap_or(0) as f64 / 255.0;
        let g = u8::from_str_radix(&hex[2..4], 16).unwrap_or(0) as f64 / 255.0;
        let b = u8::from_str_radix(&hex[4..6], 16).unwrap_or(0) as f64 / 255.0;

        Color::rgb(r, g, b)
    }

    /// Creates a grayscale color with value clamped to 0.0-1.0.
    pub fn gray(value: f64) -> Self {
        Color::Gray(value.clamp(0.0, 1.0))
    }

    /// Creates a CMYK color with values clamped to 0.0-1.0.
    pub fn cmyk(c: f64, m: f64, y: f64, k: f64) -> Self {
        Color::Cmyk(
            c.clamp(0.0, 1.0),
            m.clamp(0.0, 1.0),
            y.clamp(0.0, 1.0),
            k.clamp(0.0, 1.0),
        )
    }

    /// Black color (gray 0.0).
    pub fn black() -> Self {
        Color::Gray(0.0)
    }

    /// White color (gray 1.0).
    pub fn white() -> Self {
        Color::Gray(1.0)
    }

    /// Red color (RGB 1,0,0).
    pub fn red() -> Self {
        Color::Rgb(1.0, 0.0, 0.0)
    }

    /// Green color (RGB 0,1,0).
    pub fn green() -> Self {
        Color::Rgb(0.0, 1.0, 0.0)
    }

    /// Blue color (RGB 0,0,1).
    pub fn blue() -> Self {
        Color::Rgb(0.0, 0.0, 1.0)
    }

    pub fn yellow() -> Self {
        Color::Rgb(1.0, 1.0, 0.0)
    }

    pub fn cyan() -> Self {
        Color::Rgb(0.0, 1.0, 1.0)
    }

    pub fn magenta() -> Self {
        Color::Rgb(1.0, 0.0, 1.0)
    }

    /// Pure cyan color in CMYK space (100% cyan, 0% magenta, 0% yellow, 0% black)
    pub fn cmyk_cyan() -> Self {
        Color::Cmyk(1.0, 0.0, 0.0, 0.0)
    }

    /// Pure magenta color in CMYK space (0% cyan, 100% magenta, 0% yellow, 0% black)
    pub fn cmyk_magenta() -> Self {
        Color::Cmyk(0.0, 1.0, 0.0, 0.0)
    }

    /// Pure yellow color in CMYK space (0% cyan, 0% magenta, 100% yellow, 0% black)
    pub fn cmyk_yellow() -> Self {
        Color::Cmyk(0.0, 0.0, 1.0, 0.0)
    }

    /// Pure black color in CMYK space (0% cyan, 0% magenta, 0% yellow, 100% black)
    pub fn cmyk_black() -> Self {
        Color::Cmyk(0.0, 0.0, 0.0, 1.0)
    }

    /// Get red component (converts other color spaces to RGB approximation)
    pub fn r(&self) -> f64 {
        match self {
            Color::Rgb(r, _, _) => *r,
            Color::Gray(g) => *g,
            Color::Cmyk(c, _, _, k) => (1.0 - c) * (1.0 - k),
        }
    }

    /// Get green component (converts other color spaces to RGB approximation)
    pub fn g(&self) -> f64 {
        match self {
            Color::Rgb(_, g, _) => *g,
            Color::Gray(g) => *g,
            Color::Cmyk(_, m, _, k) => (1.0 - m) * (1.0 - k),
        }
    }

    /// Get blue component (converts other color spaces to RGB approximation)
    pub fn b(&self) -> f64 {
        match self {
            Color::Rgb(_, _, b) => *b,
            Color::Gray(g) => *g,
            Color::Cmyk(_, _, y, k) => (1.0 - y) * (1.0 - k),
        }
    }

    /// Get CMYK components (for CMYK colors, or conversion for others)
    pub fn cmyk_components(&self) -> (f64, f64, f64, f64) {
        match self {
            Color::Cmyk(c, m, y, k) => (*c, *m, *y, *k),
            Color::Rgb(r, g, b) => {
                // Convert RGB to CMYK using standard formula
                let k = 1.0 - r.max(*g).max(*b);
                if k >= 1.0 {
                    (0.0, 0.0, 0.0, 1.0)
                } else {
                    let c = (1.0 - r - k) / (1.0 - k);
                    let m = (1.0 - g - k) / (1.0 - k);
                    let y = (1.0 - b - k) / (1.0 - k);
                    (c, m, y, k)
                }
            }
            Color::Gray(g) => {
                // Convert grayscale to CMYK (K channel only)
                let k = 1.0 - g;
                (0.0, 0.0, 0.0, k)
            }
        }
    }

    /// Convert to RGB color space
    pub fn to_rgb(&self) -> Color {
        match self {
            Color::Rgb(_, _, _) => *self,
            Color::Gray(g) => Color::Rgb(*g, *g, *g),
            Color::Cmyk(c, m, y, k) => {
                // Standard CMYK to RGB conversion
                let r = (1.0 - c) * (1.0 - k);
                let g = (1.0 - m) * (1.0 - k);
                let b = (1.0 - y) * (1.0 - k);
                Color::Rgb(r.clamp(0.0, 1.0), g.clamp(0.0, 1.0), b.clamp(0.0, 1.0))
            }
        }
    }

    /// Convert to CMYK color space
    pub fn to_cmyk(&self) -> Color {
        match self {
            Color::Cmyk(_, _, _, _) => *self,
            _ => {
                let (c, m, y, k) = self.cmyk_components();
                Color::Cmyk(c, m, y, k)
            }
        }
    }

    /// Get the color space name for PDF
    pub fn color_space_name(&self) -> &'static str {
        match self {
            Color::Gray(_) => "DeviceGray",
            Color::Rgb(_, _, _) => "DeviceRGB",
            Color::Cmyk(_, _, _, _) => "DeviceCMYK",
        }
    }

    /// Check if this color is in CMYK color space
    pub fn is_cmyk(&self) -> bool {
        matches!(self, Color::Cmyk(_, _, _, _))
    }

    /// Check if this color is in RGB color space
    pub fn is_rgb(&self) -> bool {
        matches!(self, Color::Rgb(_, _, _))
    }

    /// Check if this color is in grayscale color space
    pub fn is_gray(&self) -> bool {
        matches!(self, Color::Gray(_))
    }

    /// Convert to PDF array representation
    pub fn to_pdf_array(&self) -> crate::objects::Object {
        use crate::objects::Object;
        match self {
            Color::Gray(g) => Object::Array(vec![Object::Real(*g)]),
            Color::Rgb(r, g, b) => {
                Object::Array(vec![Object::Real(*r), Object::Real(*g), Object::Real(*b)])
            }
            Color::Cmyk(c, m, y, k) => Object::Array(vec![
                Object::Real(*c),
                Object::Real(*m),
                Object::Real(*y),
                Object::Real(*k),
            ]),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rgb_color_creation() {
        let color = Color::rgb(0.5, 0.7, 0.3);
        assert_eq!(color, Color::Rgb(0.5, 0.7, 0.3));
    }

    #[test]
    fn test_rgb_color_clamping() {
        let color = Color::rgb(1.5, -0.3, 0.5);
        assert_eq!(color, Color::Rgb(1.0, 0.0, 0.5));
    }

    #[test]
    fn test_gray_color_creation() {
        let color = Color::gray(0.5);
        assert_eq!(color, Color::Gray(0.5));
    }

    #[test]
    fn test_gray_color_clamping() {
        let color1 = Color::gray(1.5);
        assert_eq!(color1, Color::Gray(1.0));

        let color2 = Color::gray(-0.5);
        assert_eq!(color2, Color::Gray(0.0));
    }

    #[test]
    fn test_cmyk_color_creation() {
        let color = Color::cmyk(0.1, 0.2, 0.3, 0.4);
        assert_eq!(color, Color::Cmyk(0.1, 0.2, 0.3, 0.4));
    }

    #[test]
    fn test_cmyk_color_clamping() {
        let color = Color::cmyk(1.5, -0.2, 0.5, 2.0);
        assert_eq!(color, Color::Cmyk(1.0, 0.0, 0.5, 1.0));
    }

    #[test]
    fn test_predefined_colors() {
        assert_eq!(Color::black(), Color::Gray(0.0));
        assert_eq!(Color::white(), Color::Gray(1.0));
        assert_eq!(Color::red(), Color::Rgb(1.0, 0.0, 0.0));
        assert_eq!(Color::green(), Color::Rgb(0.0, 1.0, 0.0));
        assert_eq!(Color::blue(), Color::Rgb(0.0, 0.0, 1.0));
        assert_eq!(Color::yellow(), Color::Rgb(1.0, 1.0, 0.0));
        assert_eq!(Color::cyan(), Color::Rgb(0.0, 1.0, 1.0));
        assert_eq!(Color::magenta(), Color::Rgb(1.0, 0.0, 1.0));
    }

    #[test]
    fn test_color_equality() {
        let color1 = Color::rgb(0.5, 0.5, 0.5);
        let color2 = Color::rgb(0.5, 0.5, 0.5);
        let color3 = Color::rgb(0.5, 0.5, 0.6);

        assert_eq!(color1, color2);
        assert_ne!(color1, color3);

        let gray1 = Color::gray(0.5);
        let gray2 = Color::gray(0.5);
        assert_eq!(gray1, gray2);

        let cmyk1 = Color::cmyk(0.1, 0.2, 0.3, 0.4);
        let cmyk2 = Color::cmyk(0.1, 0.2, 0.3, 0.4);
        assert_eq!(cmyk1, cmyk2);
    }

    #[test]
    fn test_color_different_types_inequality() {
        let rgb = Color::rgb(0.5, 0.5, 0.5);
        let gray = Color::gray(0.5);
        let cmyk = Color::cmyk(0.5, 0.5, 0.5, 0.5);

        assert_ne!(rgb, gray);
        assert_ne!(rgb, cmyk);
        assert_ne!(gray, cmyk);
    }

    #[test]
    fn test_color_debug() {
        let rgb = Color::rgb(0.1, 0.2, 0.3);
        let debug_str = format!("{rgb:?}");
        assert!(debug_str.contains("Rgb"));
        assert!(debug_str.contains("0.1"));
        assert!(debug_str.contains("0.2"));
        assert!(debug_str.contains("0.3"));

        let gray = Color::gray(0.5);
        let gray_debug = format!("{gray:?}");
        assert!(gray_debug.contains("Gray"));
        assert!(gray_debug.contains("0.5"));

        let cmyk = Color::cmyk(0.1, 0.2, 0.3, 0.4);
        let cmyk_debug = format!("{cmyk:?}");
        assert!(cmyk_debug.contains("Cmyk"));
        assert!(cmyk_debug.contains("0.1"));
        assert!(cmyk_debug.contains("0.2"));
        assert!(cmyk_debug.contains("0.3"));
        assert!(cmyk_debug.contains("0.4"));
    }

    #[test]
    fn test_color_clone() {
        let rgb = Color::rgb(0.5, 0.6, 0.7);
        let rgb_clone = rgb;
        assert_eq!(rgb, rgb_clone);

        let gray = Color::gray(0.5);
        let gray_clone = gray;
        assert_eq!(gray, gray_clone);

        let cmyk = Color::cmyk(0.1, 0.2, 0.3, 0.4);
        let cmyk_clone = cmyk;
        assert_eq!(cmyk, cmyk_clone);
    }

    #[test]
    fn test_color_copy() {
        let rgb = Color::rgb(0.5, 0.6, 0.7);
        let rgb_copy = rgb; // Copy semantics
        assert_eq!(rgb, rgb_copy);

        // Both should still be usable
        assert_eq!(rgb, Color::Rgb(0.5, 0.6, 0.7));
        assert_eq!(rgb_copy, Color::Rgb(0.5, 0.6, 0.7));
    }

    #[test]
    fn test_edge_case_values() {
        // Test exact boundary values
        let color = Color::rgb(0.0, 0.5, 1.0);
        assert_eq!(color, Color::Rgb(0.0, 0.5, 1.0));

        let gray = Color::gray(0.0);
        assert_eq!(gray, Color::Gray(0.0));

        let gray_max = Color::gray(1.0);
        assert_eq!(gray_max, Color::Gray(1.0));

        let cmyk = Color::cmyk(0.0, 0.0, 0.0, 0.0);
        assert_eq!(cmyk, Color::Cmyk(0.0, 0.0, 0.0, 0.0));

        let cmyk_max = Color::cmyk(1.0, 1.0, 1.0, 1.0);
        assert_eq!(cmyk_max, Color::Cmyk(1.0, 1.0, 1.0, 1.0));
    }

    #[test]
    fn test_floating_point_precision() {
        let color = Color::rgb(0.333333333, 0.666666666, 0.999999999);
        match color {
            Color::Rgb(r, g, b) => {
                assert!((r - 0.333333333).abs() < 1e-9);
                assert!((g - 0.666666666).abs() < 1e-9);
                assert!((b - 0.999999999).abs() < 1e-9);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_rgb_clamping_infinity() {
        // Test infinity handling
        let inf_color = Color::rgb(f64::INFINITY, f64::NEG_INFINITY, 0.5);
        assert_eq!(inf_color, Color::Rgb(1.0, 0.0, 0.5));

        // Test large positive and negative values
        let large_color = Color::rgb(1000.0, -1000.0, 0.5);
        assert_eq!(large_color, Color::Rgb(1.0, 0.0, 0.5));
    }

    #[test]
    fn test_cmyk_all_components() {
        // Test that all CMYK components are properly stored
        let cmyk = Color::cmyk(0.1, 0.2, 0.3, 0.4);
        match cmyk {
            Color::Cmyk(c, m, y, k) => {
                assert_eq!(c, 0.1);
                assert_eq!(m, 0.2);
                assert_eq!(y, 0.3);
                assert_eq!(k, 0.4);
            }
            _ => panic!("Expected CMYK color"),
        }
    }

    #[test]
    fn test_pattern_matching() {
        let colors = vec![
            Color::rgb(0.5, 0.5, 0.5),
            Color::gray(0.5),
            Color::cmyk(0.1, 0.2, 0.3, 0.4),
        ];

        let mut rgb_count = 0;
        let mut gray_count = 0;
        let mut cmyk_count = 0;

        for color in colors {
            match color {
                Color::Rgb(_, _, _) => rgb_count += 1,
                Color::Gray(_) => gray_count += 1,
                Color::Cmyk(_, _, _, _) => cmyk_count += 1,
            }
        }

        assert_eq!(rgb_count, 1);
        assert_eq!(gray_count, 1);
        assert_eq!(cmyk_count, 1);
    }

    #[test]
    fn test_cmyk_pure_colors() {
        // Test pure CMYK colors
        assert_eq!(Color::cmyk_cyan(), Color::Cmyk(1.0, 0.0, 0.0, 0.0));
        assert_eq!(Color::cmyk_magenta(), Color::Cmyk(0.0, 1.0, 0.0, 0.0));
        assert_eq!(Color::cmyk_yellow(), Color::Cmyk(0.0, 0.0, 1.0, 0.0));
        assert_eq!(Color::cmyk_black(), Color::Cmyk(0.0, 0.0, 0.0, 1.0));
    }

    #[test]
    fn test_cmyk_to_rgb_conversion() {
        // Test CMYK to RGB conversion
        let pure_cyan = Color::cmyk_cyan().to_rgb();
        match pure_cyan {
            Color::Rgb(r, g, b) => {
                assert_eq!(r, 0.0);
                assert_eq!(g, 1.0);
                assert_eq!(b, 1.0);
            }
            _ => panic!("Expected RGB color"),
        }

        let pure_magenta = Color::cmyk_magenta().to_rgb();
        match pure_magenta {
            Color::Rgb(r, g, b) => {
                assert_eq!(r, 1.0);
                assert_eq!(g, 0.0);
                assert_eq!(b, 1.0);
            }
            _ => panic!("Expected RGB color"),
        }

        let pure_yellow = Color::cmyk_yellow().to_rgb();
        match pure_yellow {
            Color::Rgb(r, g, b) => {
                assert_eq!(r, 1.0);
                assert_eq!(g, 1.0);
                assert_eq!(b, 0.0);
            }
            _ => panic!("Expected RGB color"),
        }

        let pure_black = Color::cmyk_black().to_rgb();
        match pure_black {
            Color::Rgb(r, g, b) => {
                assert_eq!(r, 0.0);
                assert_eq!(g, 0.0);
                assert_eq!(b, 0.0);
            }
            _ => panic!("Expected RGB color"),
        }
    }

    #[test]
    fn test_rgb_to_cmyk_conversion() {
        // Test RGB to CMYK conversion
        let red = Color::red().to_cmyk();
        let (c, m, y, k) = red.cmyk_components();
        assert_eq!(c, 0.0);
        assert_eq!(m, 1.0);
        assert_eq!(y, 1.0);
        assert_eq!(k, 0.0);

        let green = Color::green().to_cmyk();
        let (c, m, y, k) = green.cmyk_components();
        assert_eq!(c, 1.0);
        assert_eq!(m, 0.0);
        assert_eq!(y, 1.0);
        assert_eq!(k, 0.0);

        let blue = Color::blue().to_cmyk();
        let (c, m, y, k) = blue.cmyk_components();
        assert_eq!(c, 1.0);
        assert_eq!(m, 1.0);
        assert_eq!(y, 0.0);
        assert_eq!(k, 0.0);

        let black = Color::black().to_cmyk();
        let (c, m, y, k) = black.cmyk_components();
        assert_eq!(c, 0.0);
        assert_eq!(m, 0.0);
        assert_eq!(y, 0.0);
        assert_eq!(k, 1.0);
    }

    #[test]
    fn test_color_space_detection() {
        assert!(Color::rgb(0.5, 0.5, 0.5).is_rgb());
        assert!(!Color::rgb(0.5, 0.5, 0.5).is_cmyk());
        assert!(!Color::rgb(0.5, 0.5, 0.5).is_gray());

        assert!(Color::gray(0.5).is_gray());
        assert!(!Color::gray(0.5).is_rgb());
        assert!(!Color::gray(0.5).is_cmyk());

        assert!(Color::cmyk(0.1, 0.2, 0.3, 0.4).is_cmyk());
        assert!(!Color::cmyk(0.1, 0.2, 0.3, 0.4).is_rgb());
        assert!(!Color::cmyk(0.1, 0.2, 0.3, 0.4).is_gray());
    }

    #[test]
    fn test_color_space_names() {
        assert_eq!(Color::rgb(0.5, 0.5, 0.5).color_space_name(), "DeviceRGB");
        assert_eq!(Color::gray(0.5).color_space_name(), "DeviceGray");
        assert_eq!(
            Color::cmyk(0.1, 0.2, 0.3, 0.4).color_space_name(),
            "DeviceCMYK"
        );
    }

    #[test]
    fn test_cmyk_components_extraction() {
        let cmyk_color = Color::cmyk(0.1, 0.2, 0.3, 0.4);
        let (c, m, y, k) = cmyk_color.cmyk_components();
        assert_eq!(c, 0.1);
        assert_eq!(m, 0.2);
        assert_eq!(y, 0.3);
        assert_eq!(k, 0.4);

        // Test RGB to CMYK component conversion
        let white = Color::white();
        let (c, m, y, k) = white.cmyk_components();
        assert_eq!(c, 0.0);
        assert_eq!(m, 0.0);
        assert_eq!(y, 0.0);
        assert_eq!(k, 0.0);
    }

    #[test]
    fn test_roundtrip_conversions() {
        // Test that conversion cycles preserve color reasonably well
        let original_rgb = Color::rgb(0.6, 0.3, 0.9);
        let converted_cmyk = original_rgb.to_cmyk();
        let back_to_rgb = converted_cmyk.to_rgb();

        let orig_components = (original_rgb.r(), original_rgb.g(), original_rgb.b());
        let final_components = (back_to_rgb.r(), back_to_rgb.g(), back_to_rgb.b());

        // Allow small tolerance for floating point conversion errors
        assert!((orig_components.0 - final_components.0).abs() < 0.001);
        assert!((orig_components.1 - final_components.1).abs() < 0.001);
        assert!((orig_components.2 - final_components.2).abs() < 0.001);
    }

    #[test]
    fn test_grayscale_to_cmyk_conversion() {
        let gray = Color::gray(0.7);
        let (c, m, y, k) = gray.cmyk_components();

        assert_eq!(c, 0.0);
        assert_eq!(m, 0.0);
        assert_eq!(y, 0.0);
        assert!((k - 0.3).abs() < 1e-10); // k = 1.0 - gray_value (with tolerance for floating point precision)

        let gray_as_cmyk = gray.to_cmyk();
        let cmyk_components = gray_as_cmyk.cmyk_components();
        assert_eq!(cmyk_components.0, 0.0);
        assert_eq!(cmyk_components.1, 0.0);
        assert_eq!(cmyk_components.2, 0.0);
        assert!((cmyk_components.3 - 0.3).abs() < 1e-10);
    }

    // =============================================================================
    // RIGOROUS TESTS FOR UNCOVERED LINES (Target: 100% coverage)
    // =============================================================================

    #[test]
    fn test_hex_color_invalid_length() {
        // Line 24: hex string with invalid length should return black
        let color1 = Color::hex("#FFF");
        assert_eq!(
            color1,
            Color::black(),
            "Short hex string should return black"
        );

        let color2 = Color::hex("#FFFFFFF");
        assert_eq!(
            color2,
            Color::black(),
            "Long hex string should return black"
        );

        let color3 = Color::hex("");
        assert_eq!(
            color3,
            Color::black(),
            "Empty hex string should return black"
        );
    }

    #[test]
    fn test_hex_color_valid() {
        let color = Color::hex("#FF0080");

        // Verify RGB components: FF = 255/255 = 1.0, 00 = 0/255 = 0.0, 80 = 128/255 ≈ 0.502
        assert!((color.r() - 1.0).abs() < 0.01, "Red should be 1.0");
        assert!((color.g() - 0.0).abs() < 0.01, "Green should be 0.0");
        assert!((color.b() - 0.502).abs() < 0.01, "Blue should be ~0.502");
    }

    #[test]
    fn test_hex_color_without_hash() {
        let color = Color::hex("00FF00");

        // Green color: 00 = 0, FF = 255, 00 = 0
        assert_eq!(color.r(), 0.0, "Red should be 0.0");
        assert_eq!(color.g(), 1.0, "Green should be 1.0");
        assert_eq!(color.b(), 0.0, "Blue should be 0.0");
    }

    #[test]
    fn test_cmyk_r_component() {
        // Lines 110-111: Test CMYK branch of r() method
        let cmyk_color = Color::cmyk(0.5, 0.2, 0.3, 0.1);
        let r = cmyk_color.r();

        // Formula: (1.0 - c) * (1.0 - k) = (1.0 - 0.5) * (1.0 - 0.1) = 0.5 * 0.9 = 0.45
        assert!((r - 0.45).abs() < 1e-10, "CMYK r() should be 0.45");
    }

    #[test]
    fn test_cmyk_g_component() {
        // Lines 119-120: Test CMYK branch of g() method
        let cmyk_color = Color::cmyk(0.2, 0.6, 0.3, 0.2);
        let g = cmyk_color.g();

        // Formula: (1.0 - m) * (1.0 - k) = (1.0 - 0.6) * (1.0 - 0.2) = 0.4 * 0.8 = 0.32
        assert!((g - 0.32).abs() < 1e-10, "CMYK g() should be 0.32");
    }

    #[test]
    fn test_cmyk_b_component() {
        // Lines 128-129: Test CMYK branch of b() method
        let cmyk_color = Color::cmyk(0.3, 0.2, 0.7, 0.15);
        let b = cmyk_color.b();

        // Formula: (1.0 - y) * (1.0 - k) = (1.0 - 0.7) * (1.0 - 0.15) = 0.3 * 0.85 = 0.255
        assert!((b - 0.255).abs() < 1e-10, "CMYK b() should be 0.255");
    }

    #[test]
    fn test_to_rgb_when_already_rgb() {
        // Line 141: to_rgb() when color is already RGB should return self
        let rgb_color = Color::rgb(0.5, 0.6, 0.7);
        let converted = rgb_color.to_rgb();

        assert_eq!(converted, rgb_color, "to_rgb() on RGB should return self");
        assert_eq!(converted.r(), 0.5);
        assert_eq!(converted.g(), 0.6);
        assert_eq!(converted.b(), 0.7);
    }

    #[test]
    fn test_to_cmyk_when_already_cmyk() {
        // Lines 160-161: to_cmyk() when color is already CMYK should return self
        let cmyk_color = Color::cmyk(0.1, 0.2, 0.3, 0.4);
        let converted = cmyk_color.to_cmyk();

        assert_eq!(
            converted, cmyk_color,
            "to_cmyk() on CMYK should return self"
        );
        let (c, m, y, k) = converted.cmyk_components();
        assert_eq!(c, 0.1);
        assert_eq!(m, 0.2);
        assert_eq!(y, 0.3);
        assert_eq!(k, 0.4);
    }

    #[test]
    fn test_to_pdf_array_gray() {
        // Line 175: to_pdf_array() for Gray color
        use crate::objects::Object;

        let gray = Color::gray(0.75);
        let array = gray.to_pdf_array();

        match array {
            Object::Array(vec) => {
                assert_eq!(vec.len(), 1, "Gray PDF array should have 1 element");
                match &vec[0] {
                    Object::Real(val) => assert_eq!(*val, 0.75, "Gray value should be 0.75"),
                    _ => panic!("Expected Real object"),
                }
            }
            _ => panic!("Expected Array object"),
        }
    }

    #[test]
    fn test_to_pdf_array_rgb() {
        // Lines 211, 215-219: to_pdf_array() for RGB color
        use crate::objects::Object;

        let rgb = Color::rgb(0.2, 0.5, 0.9);
        let array = rgb.to_pdf_array();

        match array {
            Object::Array(vec) => {
                assert_eq!(vec.len(), 3, "RGB PDF array should have 3 elements");
                match (&vec[0], &vec[1], &vec[2]) {
                    (Object::Real(r), Object::Real(g), Object::Real(b)) => {
                        assert_eq!(*r, 0.2, "Red should be 0.2");
                        assert_eq!(*g, 0.5, "Green should be 0.5");
                        assert_eq!(*b, 0.9, "Blue should be 0.9");
                    }
                    _ => panic!("Expected Real objects"),
                }
            }
            _ => panic!("Expected Array object"),
        }
    }

    #[test]
    fn test_to_pdf_array_cmyk() {
        // Lines 215-219: to_pdf_array() for CMYK color
        use crate::objects::Object;

        let cmyk = Color::cmyk(0.1, 0.3, 0.5, 0.7);
        let array = cmyk.to_pdf_array();

        match array {
            Object::Array(vec) => {
                assert_eq!(vec.len(), 4, "CMYK PDF array should have 4 elements");
                match (&vec[0], &vec[1], &vec[2], &vec[3]) {
                    (Object::Real(c), Object::Real(m), Object::Real(y), Object::Real(k)) => {
                        assert_eq!(*c, 0.1, "Cyan should be 0.1");
                        assert_eq!(*m, 0.3, "Magenta should be 0.3");
                        assert_eq!(*y, 0.5, "Yellow should be 0.5");
                        assert_eq!(*k, 0.7, "Black should be 0.7");
                    }
                    _ => panic!("Expected Real objects"),
                }
            }
            _ => panic!("Expected Array object"),
        }
    }

    #[test]
    fn test_cmyk_components_all_branches() {
        // Test all match branches in cmyk_components()

        // CMYK color (direct return)
        let cmyk = Color::cmyk(0.2, 0.4, 0.6, 0.8);
        let (c, m, y, k) = cmyk.cmyk_components();
        assert_eq!(c, 0.2);
        assert_eq!(m, 0.4);
        assert_eq!(y, 0.6);
        assert_eq!(k, 0.8);

        // RGB color (conversion)
        let rgb = Color::rgb(0.5, 0.25, 0.75);
        let (_c, _m, _y, k) = rgb.cmyk_components();
        // k = 1.0 - max(0.5, 0.25, 0.75) = 1.0 - 0.75 = 0.25
        assert!((k - 0.25).abs() < 1e-10, "K should be 0.25");

        // Gray color (K channel only)
        let gray = Color::gray(0.4);
        let (c, m, y, k) = gray.cmyk_components();
        assert_eq!(c, 0.0);
        assert_eq!(m, 0.0);
        assert_eq!(y, 0.0);
        assert!((k - 0.6).abs() < 1e-10, "K should be 0.6 (1.0 - 0.4)");
    }

    #[test]
    fn test_color_conversions_preserve_match_branches() {
        // Ensure all conversion branches are exercised

        // RGB → CMYK → RGB
        let rgb = Color::rgb(0.8, 0.4, 0.6);
        let as_cmyk = rgb.to_cmyk();
        let back_to_rgb = as_cmyk.to_rgb();

        // Verify conversion preserves color within tolerance
        assert!((rgb.r() - back_to_rgb.r()).abs() < 0.01);
        assert!((rgb.g() - back_to_rgb.g()).abs() < 0.01);
        assert!((rgb.b() - back_to_rgb.b()).abs() < 0.01);

        // Gray → CMYK
        let gray = Color::gray(0.5);
        let gray_as_cmyk = gray.to_cmyk();
        let (c, m, y, k) = gray_as_cmyk.cmyk_components();
        assert_eq!(c, 0.0);
        assert_eq!(m, 0.0);
        assert_eq!(y, 0.0);
        assert!((k - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_gray_r_g_b_components() {
        // Lines 110, 119, 128: Gray branch of r(), g(), b()
        let gray = Color::gray(0.6);

        let r = gray.r();
        let g = gray.g();
        let b = gray.b();

        // Gray returns same value for r, g, and b
        assert_eq!(r, 0.6, "Gray r() should return gray value");
        assert_eq!(g, 0.6, "Gray g() should return gray value");
        assert_eq!(b, 0.6, "Gray b() should return gray value");
    }

    #[test]
    fn test_to_rgb_gray_conversion() {
        // Line 161: Gray → RGB conversion
        let gray = Color::gray(0.4);
        let rgb = gray.to_rgb();

        match rgb {
            Color::Rgb(r, g, b) => {
                assert_eq!(r, 0.4, "Gray → RGB should set r = gray value");
                assert_eq!(g, 0.4, "Gray → RGB should set g = gray value");
                assert_eq!(b, 0.4, "Gray → RGB should set b = gray value");
            }
            _ => panic!("Expected RGB color from Gray.to_rgb()"),
        }
    }

    #[test]
    fn test_rgb_black_to_cmyk() {
        // Line 141: RGB(0,0,0) should convert to CMYK (0,0,0,1) - pure black
        let black_rgb = Color::rgb(0.0, 0.0, 0.0);
        let (c, m, y, k) = black_rgb.cmyk_components();

        // When all RGB are 0, k = 1.0 - max(0,0,0) = 1.0, so k >= 1.0 branch activates
        assert_eq!(c, 0.0, "Cyan should be 0 for pure black");
        assert_eq!(m, 0.0, "Magenta should be 0 for pure black");
        assert_eq!(y, 0.0, "Yellow should be 0 for pure black");
        assert_eq!(k, 1.0, "K should be 1.0 for pure black");
    }
}