bgone 0.6.0

Ultra-fast CLI tool for removing solid background colors from images
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
875
876
877
878
879
880
881
882
883
884
885
886
mod common;

use assert_cmd::cargo;
use assert_cmd::prelude::*;
use bgone::color::ForegroundColorSpec;
use bgone::deduce::deduce_unknown_colors;
use bgone::unmix::{compute_result_color, unmix_colors};
use common::{
    calculate_psnr, calculate_similarity_percentage, ensure_output_dir, overlay_on_background,
    save_test_images,
};
use std::process::Command;
use tempfile::TempDir;

#[test]
fn test_color_deduction_single_unknown() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Run bgone with one known and one unknown color
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/square-glow.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "#ff0000",
        "auto",
        "--bg",
        "#000000",
    ]);

    let output = cmd.assert().success().get_output().stdout.clone();
    let output_str = String::from_utf8_lossy(&output);
    println!("Output:\n{}", output_str);

    // Should deduce purple
    assert!(output_str.contains("Deduced"));

    // Load and save images for inspection
    let original = image::open("tests/inputs/square-glow.png").unwrap();
    let processed = image::open(&output_path).unwrap();
    let reconstructed = overlay_on_background(&processed, [0, 0, 0]);

    save_test_images("color_deduction", "square_glow", &processed, &reconstructed);

    // Check reconstruction quality - should be perfect in strict mode
    let similarity = calculate_similarity_percentage(&original, &reconstructed);
    let psnr = calculate_psnr(&original, &reconstructed);
    println!(
        "Red with purple glow (strict) - Similarity: {:.2}%, PSNR: {:.2} dB",
        similarity, psnr
    );

    assert!(
        similarity > 99.0,
        "Strict mode should reconstruct the image with high quality: {:.4}%",
        similarity
    );
}

#[test]
fn test_color_deduction_all_unknown() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Create a custom test image with known colors
    let width = 100;
    let height = 100;
    let mut img = image::RgbImage::new(width, height);

    // Fill with white background
    for pixel in img.pixels_mut() {
        *pixel = image::Rgb([255, 255, 255]);
    }

    // Add red and green circles (50% opacity each)
    let red_mixed = [255, 128, 128]; // Red mixed with white
    let green_mixed = [128, 255, 128]; // Green mixed with white

    // Red circle (left)
    for y in 0..height {
        for x in 0..width / 2 {
            if ((x as i32 - 25).pow(2) + (y as i32 - 50).pow(2)) < 400 {
                img.put_pixel(x, y, image::Rgb(red_mixed));
            }
        }
    }

    // Green circle (right)
    for y in 0..height {
        for x in width / 2..width {
            if ((x as i32 - 75).pow(2) + (y as i32 - 50).pow(2)) < 400 {
                img.put_pixel(x, y, image::Rgb(green_mixed));
            }
        }
    }

    let test_image_path = temp_dir.path().join("test_input.png");
    img.save(&test_image_path).unwrap();

    // Run bgone with all unknown colors
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        test_image_path.to_str().unwrap(),
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "auto",
        "auto",
        "--bg",
        "#ffffff",
    ]);

    let output = cmd.assert().success().get_output().stdout.clone();
    let output_str = String::from_utf8_lossy(&output);
    println!("Output:\n{}", output_str);

    // Should deduce both colors
    assert!(output_str.contains("Deduced 2 unknown colors"));
}

#[test]
fn test_color_deduction_error_cases() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Test: Only 'auto' specified without any known colors - should work in strict mode
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/square.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "auto",
    ]);

    cmd.assert().success();
}

#[test]
fn test_mixed_known_and_unknown_colors() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Run bgone with mix of known and unknown colors
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/rectangles.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "#ff0000", // Red (known)
        "auto",    // Unknown
        "#ffff00", // Yellow (known)
        "--bg",
        "#0000ff",
    ]);

    cmd.assert()
        .success()
        .stdout(predicates::str::contains("Deduced 1 unknown color"));
}

#[test]
fn test_multiple_unknown_colors_convergence() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Test that multiple unknowns don't all converge to black
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/circle-gradients.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "auto",
        "auto",
        "auto",
        "--bg",
        "#ffffff",
    ]);

    let output = cmd
        .assert()
        .success()
        .stdout(predicates::str::contains("Deduced 3 unknown colors"))
        .get_output()
        .stdout
        .clone();

    let output_str = String::from_utf8_lossy(&output);
    println!("Output:\n{}", output_str);

    // Extract deduced colors from output
    let mut deduced_colors = Vec::new();
    for line in output_str.lines() {
        if line.contains("Deduced 3 unknown colors:") {
            // Parse colors from format: "✓ Deduced 3 unknown colors: #ff1b1b #1bff1b #1b1bff"
            if let Some(colors_part) = line.split("colors:").nth(1) {
                deduced_colors = colors_part
                    .split_whitespace()
                    .map(|s| s.to_string())
                    .collect();
                break;
            }
        }
    }

    println!("Deduced colors: {:?}", deduced_colors);
    assert_eq!(
        deduced_colors.len(),
        3,
        "Should have deduced exactly 3 colors"
    );

    // Parse the deduced colors
    let mut parsed_colors = Vec::new();
    for color in &deduced_colors {
        if color.starts_with("#") && color.len() == 7 {
            let r = u8::from_str_radix(&color[1..3], 16).unwrap();
            let g = u8::from_str_radix(&color[3..5], 16).unwrap();
            let b = u8::from_str_radix(&color[5..7], 16).unwrap();
            parsed_colors.push((r, g, b));
        }
    }

    // Check that we have valid colors
    assert_eq!(parsed_colors.len(), 3, "All colors should be valid");

    // The optimal colors should be pure red, green, and blue (furthest from white background)
    let has_pure_red = parsed_colors
        .iter()
        .any(|&(r, g, b)| r == 255 && g == 0 && b == 0);
    let has_pure_green = parsed_colors
        .iter()
        .any(|&(r, g, b)| r == 0 && g == 255 && b == 0);
    let has_pure_blue = parsed_colors
        .iter()
        .any(|&(r, g, b)| r == 0 && g == 0 && b == 255);

    // The algorithm should now find the optimal pure colors
    assert!(
        has_pure_red && has_pure_green && has_pure_blue,
        "Expected pure colors #ff0000, #00ff00, #0000ff but got {:?}",
        deduced_colors
    );

    // Load and save images for inspection
    let original = image::open("tests/inputs/circle-gradients.png").unwrap();
    let processed = image::open(&output_path).unwrap();
    let reconstructed = overlay_on_background(&processed, [255, 255, 255]);

    save_test_images(
        "color_deduction",
        "circle_gradients_all_auto",
        &processed,
        &reconstructed,
    );

    // Also check reconstruction quality
    let similarity = calculate_similarity_percentage(&original, &reconstructed);
    println!("Three gradients deduction - Similarity: {:.2}%", similarity);
}

#[test]
fn test_auto_deduction_finds_optimal_colors() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // This test checks that auto deduction finds the most saturated colors
    // (furthest from the background), not just any valid colors
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/circle-gradients.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "auto",
        "auto",
        "auto",
        "--bg",
        "#ffffff",
    ]);

    let output = cmd
        .assert()
        .success()
        .stdout(predicates::str::contains("Deduced 3 unknown colors"))
        .get_output()
        .clone();

    let output_str = String::from_utf8_lossy(&output.stdout);

    // Extract deduced colors
    let mut deduced_colors: Vec<String> = Vec::new();
    for line in output_str.lines() {
        if line.contains("Deduced unknown color") {
            if let Some(color_part) = line.split(':').nth(1) {
                let colors: Vec<String> = color_part
                    .split_whitespace()
                    .map(|s| s.to_string())
                    .collect();
                deduced_colors.extend(colors);
            }
        } else if line.contains("Deduced 3 unknown colors:")
            && let Some(color_part) = line.split(':').nth(1)
        {
            deduced_colors = color_part
                .split_whitespace()
                .map(|s| s.to_string())
                .collect();
            break;
        }
    }

    println!("Deduced colors: {:?}", deduced_colors);

    // Parse colors
    let mut parsed_colors = Vec::new();
    for color in &deduced_colors {
        if color.starts_with("#") && color.len() == 7 {
            let r = u8::from_str_radix(&color[1..3], 16).unwrap();
            let g = u8::from_str_radix(&color[3..5], 16).unwrap();
            let b = u8::from_str_radix(&color[5..7], 16).unwrap();
            parsed_colors.push((r, g, b));
        }
    }

    // The optimal colors for white background should be pure RGB
    let has_pure_red = parsed_colors
        .iter()
        .any(|&(r, g, b)| r == 255 && g == 0 && b == 0);
    let has_pure_green = parsed_colors
        .iter()
        .any(|&(r, g, b)| r == 0 && g == 255 && b == 0);
    let has_pure_blue = parsed_colors
        .iter()
        .any(|&(r, g, b)| r == 0 && g == 0 && b == 255);

    assert!(
        has_pure_red && has_pure_green && has_pure_blue,
        "Expected pure colors #ff0000, #00ff00, #0000ff but got {:?}",
        deduced_colors
    );
}

#[test]
fn test_circle_gradients_with_known_red() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Test with red as known color - should deduce green and blue
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/circle-gradients.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "#ff0000",
        "auto",
        "auto",
        "--bg",
        "#ffffff",
    ]);

    let output = cmd
        .assert()
        .success()
        .stdout(predicates::str::contains("Deduced 2 unknown colors"))
        .get_output()
        .stdout
        .clone();

    let output_str = String::from_utf8_lossy(&output);

    // Extract deduced colors from output
    let mut colors = vec!["#ff0000".to_string()]; // Red is known
    for line in output_str.lines() {
        if line.contains("Deduced 2 unknown colors:") {
            // Parse colors from format: "✓ Deduced 2 unknown colors: #1bff1b #1b1bff"
            if let Some(colors_part) = line.split("colors:").nth(1) {
                let deduced: Vec<String> = colors_part
                    .split_whitespace()
                    .map(|s| s.to_string())
                    .collect();
                colors.extend(deduced);
                break;
            }
        }
    }

    println!("Final colors with red known: {:?}", colors);

    // Should have red plus two other bright colors
    assert_eq!(colors.len(), 3);
    assert!(colors.contains(&"#ff0000".to_string()));

    // Check that we deduced green and blue (approximately)
    let mut found_green = false;
    let mut found_blue = false;

    for color in &colors {
        if color != "#ff0000" && color.starts_with("#") && color.len() == 7 {
            let r = u8::from_str_radix(&color[1..3], 16).unwrap();
            let g = u8::from_str_radix(&color[3..5], 16).unwrap();
            let b = u8::from_str_radix(&color[5..7], 16).unwrap();

            if g > 200 && r < 50 && b < 50 {
                found_green = true;
            } else if b > 200 && r < 50 && g < 50 {
                found_blue = true;
            }
        }
    }

    assert!(
        found_green,
        "Should deduce green when red is known, got: {:?}",
        colors
    );
    assert!(
        found_blue,
        "Should deduce blue when red is known, got: {:?}",
        colors
    );

    // Load and save images for inspection
    let processed = image::open(&output_path).unwrap();
    let reconstructed = overlay_on_background(&processed, [255, 255, 255]);

    save_test_images(
        "color_deduction",
        "circle_gradients_known_red",
        &processed,
        &reconstructed,
    );
}

#[test]
fn test_circle_gradients_with_known_green() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Test with green as known color - should deduce red and blue
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/circle-gradients.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "#00ff00",
        "auto",
        "auto",
        "--bg",
        "#ffffff",
    ]);

    let output = cmd
        .assert()
        .success()
        .stdout(predicates::str::contains("Deduced 2 unknown colors"))
        .get_output()
        .stdout
        .clone();

    let output_str = String::from_utf8_lossy(&output);

    // Extract deduced colors from output
    let mut colors = vec!["#00ff00".to_string()]; // Green is known
    for line in output_str.lines() {
        if line.contains("Deduced 2 unknown colors:") {
            // Parse colors from format: "✓ Deduced 2 unknown colors: #ff1b1b #1b1bff"
            if let Some(colors_part) = line.split("colors:").nth(1) {
                let deduced: Vec<String> = colors_part
                    .split_whitespace()
                    .map(|s| s.to_string())
                    .collect();
                colors.extend(deduced);
                break;
            }
        }
    }

    println!("Final colors with green known: {:?}", colors);

    // Should have green plus two other bright colors
    assert_eq!(colors.len(), 3);
    assert!(colors.contains(&"#00ff00".to_string()));

    // Check that we deduced red and blue (approximately)
    let mut found_red = false;
    let mut found_blue = false;

    for color in &colors {
        if color != "#00ff00" && color.starts_with("#") && color.len() == 7 {
            let r = u8::from_str_radix(&color[1..3], 16).unwrap();
            let g = u8::from_str_radix(&color[3..5], 16).unwrap();
            let b = u8::from_str_radix(&color[5..7], 16).unwrap();

            if r > 200 && g < 50 && b < 50 {
                found_red = true;
            } else if b > 200 && r < 50 && g < 50 {
                found_blue = true;
            }
        }
    }

    assert!(
        found_red,
        "Should deduce red when green is known, got: {:?}",
        colors
    );
    assert!(
        found_blue,
        "Should deduce blue when green is known, got: {:?}",
        colors
    );

    // Load and save images for inspection
    let processed = image::open(&output_path).unwrap();
    let reconstructed = overlay_on_background(&processed, [255, 255, 255]);

    save_test_images(
        "color_deduction",
        "circle_gradients_known_green",
        &processed,
        &reconstructed,
    );
}

#[test]
fn test_circle_gradients_with_known_blue() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Test with blue as known color - should deduce red and green
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/circle-gradients.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "#0000ff",
        "auto",
        "auto",
        "--bg",
        "#ffffff",
    ]);

    let output = cmd
        .assert()
        .success()
        .stdout(predicates::str::contains("Deduced 2 unknown colors"))
        .get_output()
        .stdout
        .clone();

    let output_str = String::from_utf8_lossy(&output);

    // Extract deduced colors from output
    let mut colors = vec!["#0000ff".to_string()]; // Blue is known
    for line in output_str.lines() {
        if line.contains("Deduced 2 unknown colors:") {
            // Parse colors from format: "✓ Deduced 2 unknown colors: #ff1b1b #1bff1b"
            if let Some(colors_part) = line.split("colors:").nth(1) {
                let deduced: Vec<String> = colors_part
                    .split_whitespace()
                    .map(|s| s.to_string())
                    .collect();
                colors.extend(deduced);
                break;
            }
        }
    }

    let mut found_red = false;
    let mut found_green = false;

    for color in &colors {
        if color.starts_with("#") && color.len() == 7 {
            let r = u8::from_str_radix(&color[1..3], 16).unwrap();
            let g = u8::from_str_radix(&color[3..5], 16).unwrap();
            let b = u8::from_str_radix(&color[5..7], 16).unwrap();

            if r > 200 && g < 50 && b < 50 {
                found_red = true;
            } else if g > 200 && r < 50 && b < 50 {
                found_green = true;
            }
        }
    }

    assert!(
        found_red,
        "Should deduce red when blue is known, got: {:?}",
        colors
    );
    assert!(
        found_green,
        "Should deduce green when blue is known, got: {:?}",
        colors
    );

    // Load and save images for inspection
    let processed = image::open(&output_path).unwrap();
    let reconstructed = overlay_on_background(&processed, [255, 255, 255]);

    save_test_images(
        "color_deduction",
        "circle_gradients_known_blue",
        &processed,
        &reconstructed,
    );
}

#[test]
fn test_square_gradient_auto_deduction() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Test with auto color deduction - should maximize opacity
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/square-gradient.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "auto",
        "auto",
        "--bg",
        "#14191e",
    ]);

    let output = cmd
        .assert()
        .success()
        .stdout(predicates::str::contains("Deduced 2 unknown colors"))
        .get_output()
        .stdout
        .clone();

    let output_str = String::from_utf8_lossy(&output);
    println!("Square gradient auto deduction output:\n{}", output_str);

    // Verify cyan and magenta were deduced
    let lines: Vec<&str> = output_str.lines().collect();
    for line in lines {
        if line.contains("Deduced 2 unknown colors") {
            println!("Success: {}", line);
            break;
        }
    }

    // Load and save images for inspection
    let original = image::open("tests/inputs/square-gradient.png").unwrap();
    let processed = image::open(&output_path).unwrap();
    let reconstructed = overlay_on_background(&processed, [20, 25, 30]);

    save_test_images(
        "color_deduction",
        "square_gradient_auto",
        &processed,
        &reconstructed,
    );

    // Check reconstruction quality
    let similarity = calculate_similarity_percentage(&original, &reconstructed);
    println!(
        "Square gradient (auto deduction) - Similarity: {:.2}%",
        similarity
    );

    // Should have good reconstruction quality
    assert!(
        similarity > 98.0,
        "Reconstruction quality too low: {:.2}%",
        similarity
    );
}

#[test]
fn test_square_gradient_with_known_magenta() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Test with known magenta (slightly different) and auto cyan
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/square-gradient.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "#fa08fa", // Slightly different from #f805fe that it would normally deduce
        "auto",
        "--bg",
        "#14191e",
    ]);

    let output = cmd
        .assert()
        .success()
        .stdout(predicates::str::contains("Deduced 1 unknown color"))
        .get_output()
        .stdout
        .clone();

    let output_str = String::from_utf8_lossy(&output);
    println!("Output with known magenta:\n{}", output_str);

    // Check similarity is still acceptable
    let original = image::open("tests/inputs/square-gradient.png").unwrap();
    let processed = image::open(&output_path).unwrap();
    let reconstructed = overlay_on_background(&processed, [20, 25, 30]);

    save_test_images(
        "color_deduction",
        "square_gradient_known_magenta",
        &processed,
        &reconstructed,
    );

    let similarity = calculate_similarity_percentage(&original, &reconstructed);
    println!(
        "Square gradient (known magenta) - Similarity: {:.2}%",
        similarity
    );

    // With different magenta, similarity might be lower
    assert!(
        similarity > 98.0,
        "Reconstruction quality too low: {:.2}%",
        similarity
    );
}

#[test]
fn test_square_gradient_with_known_cyan() {
    ensure_output_dir();
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.png");

    // Test with known cyan (slightly different) and auto magenta
    let mut cmd = Command::new(cargo::cargo_bin!("bgone"));
    cmd.args([
        "tests/inputs/square-gradient.png",
        output_path.to_str().unwrap(),
        "--strict",
        "--fg",
        "#109098", // Slightly different from #0e929a that it would normally deduce
        "auto",
        "--bg",
        "#14191e",
    ]);

    let output = cmd
        .assert()
        .success()
        .stdout(predicates::str::contains("Deduced 1 unknown color"))
        .get_output()
        .stdout
        .clone();

    let output_str = String::from_utf8_lossy(&output);
    println!("Output with known cyan:\n{}", output_str);

    // Check similarity is still acceptable
    let original = image::open("tests/inputs/square-gradient.png").unwrap();
    let processed = image::open(&output_path).unwrap();
    let reconstructed = overlay_on_background(&processed, [20, 25, 30]);

    save_test_images(
        "color_deduction",
        "square_gradient_known_cyan",
        &processed,
        &reconstructed,
    );

    let similarity = calculate_similarity_percentage(&original, &reconstructed);
    println!(
        "Square gradient (known cyan) - Similarity: {:.2}%",
        similarity
    );

    // With different cyan, similarity might be lower
    assert!(
        similarity > 98.0,
        "Reconstruction quality too low: {:.2}%",
        similarity
    );
}

#[test]
fn test_pure_color_deduction() {
    let temp_dir = TempDir::new().unwrap();

    // Create test image: pure green on black
    let width = 100;
    let height = 100;
    let mut img = image::RgbImage::new(width, height);

    // Fill with black background
    for pixel in img.pixels_mut() {
        *pixel = image::Rgb([0, 0, 0]);
    }

    // Add pure green rectangle
    for y in 25..75 {
        for x in 25..75 {
            img.put_pixel(x, y, image::Rgb([0, 255, 0]));
        }
    }

    let test_image_path = temp_dir.path().join("pure_green.png");
    img.save(&test_image_path).unwrap();

    // Test color deduction
    let img = image::open(&test_image_path).unwrap();
    let specs = vec![ForegroundColorSpec::Unknown];
    let background = [0, 0, 0];

    let result = deduce_unknown_colors(&img, &specs, background, 0.05).unwrap();

    assert_eq!(result.len(), 1);
    let deduced_color = result[0];

    // Should deduce pure green
    assert_eq!(deduced_color[0], 0); // R
    assert_eq!(deduced_color[1], 255); // G
    assert_eq!(deduced_color[2], 0); // B

    println!("Successfully deduced pure green: {:?}", deduced_color);
}

#[test]
fn test_opacity_optimization() {
    // Test that unmixing optimizes for opacity
    let observed = [128, 0, 0]; // 50% red on black
    let foregrounds = vec![[1.0, 0.0, 0.0]]; // Pure red
    let background = [0.0, 0.0, 0.0]; // Black

    let result = unmix_colors(observed, &foregrounds, background);
    let (color, alpha) = compute_result_color(&result, &foregrounds);

    println!("Unmixed color: {:?}, alpha: {}", color, alpha);

    // Should get pure red with ~50% opacity
    assert!((color[0] - 1.0).abs() < 0.01);
    assert!((color[1] - 0.0).abs() < 0.01);
    assert!((color[2] - 0.0).abs() < 0.01);
    assert!((alpha - 0.502).abs() < 0.01); // ~50%

    // Test with multiple colors - should still optimize for opacity
    let observed = [255, 255, 0]; // Yellow
    let foregrounds = vec![[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]; // R,G,B
    let result = unmix_colors(observed, &foregrounds, background);

    println!("Yellow unmix result: {:?}", result);

    // Should use red and green at full opacity, not blue
    assert!(result.weights[0] > 0.4); // Red weight
    assert!(result.weights[1] > 0.4); // Green weight
    assert!(result.weights[2] < 0.1); // Blue weight (should be near 0)
    assert!(result.alpha > 0.95); // Should achieve high opacity

    // Test gradient case
    let observed = [200, 100, 100]; // Reddish color
    let foregrounds = vec![[1.0, 0.0, 0.0], [0.5, 0.5, 0.5]]; // Red and gray
    let background = [1.0, 1.0, 1.0]; // White

    let result = unmix_colors(observed, &foregrounds, background);
    println!("Gradient unmix result: {:?}", result);

    // Should achieve good opacity
    assert!(result.alpha > 0.6);
}