Skip to main content

palette_bin/
lib.rs

1pub mod args;
2pub mod palette;
3pub mod resize;
4
5#[derive(Debug, PartialEq, clap::ValueEnum, Clone)]
6pub enum RatioMode {
7    Maintain,
8    Stretch,
9    Crop,
10}
11
12#[cfg(test)]
13pub mod test_utils {
14    use image::{ImageBuffer, Rgb};
15    use std::fs::File;
16    use std::{io, io::Write, path::PathBuf};
17
18    /// Creates a pattern for testing. The top left is always pure black, bottom right pure white.
19    pub fn create_test_image(width: u32, height: u32) -> image::RgbImage {
20        let mut img = ImageBuffer::new(width, height);
21        for (x, y, pixel) in img.enumerate_pixels_mut() {
22            let r = (x * 255 / (width - 1)) as u8;
23            let g = (y * 255 / (height - 1)) as u8;
24            let b = ((x + y) * 255 / (width + height - 2)) as u8;
25            *pixel = Rgb([r, g, b]);
26        }
27        img
28    }
29
30    // Helper function to create a simple ACT palette for testing with 5 basic colors
31    pub fn create_test_act_file(path: &PathBuf) -> Result<(), io::Error> {
32        let mut file = File::create(path)?;
33
34        // Define our 5 colors: black, white, red, green, blue
35        let colors = [
36            [0, 0, 0],       // Black
37            [255, 255, 255], // White
38            [255, 0, 0],     // Red
39            [0, 255, 0],     // Green
40            [0, 0, 255],     // Blue
41        ];
42
43        // Write the color data
44        let mut palette_data = Vec::new();
45
46        // First write all the defined colors
47        for color in &colors {
48            palette_data.push(color[0]); // R
49            palette_data.push(color[1]); // G
50            palette_data.push(color[2]); // B
51        }
52
53        // Pad the rest of the 256-color palette with zeros
54        // Each color takes 3 bytes, we have 5 colors, so need 251 more colors worth of padding
55        // That's 251 * 3 = 753 bytes of padding
56        palette_data.extend(vec![0; 251 * 3]);
57
58        // Write the number of actual colors at offset 768 (2 bytes)
59        palette_data.push(0); // High byte of 5 (0x0005)
60        palette_data.push(5); // Low byte of 5
61
62        // Write the transparent color index as -1 (0xFFFF) meaning no transparent color
63        palette_data.push(0xFF);
64        palette_data.push(0xFF);
65
66        // Write all the data to the file
67        file.write_all(&palette_data)?;
68        Ok(())
69    }
70}