Expand description
A module for parsing simple grid data definitions and rendering them as PNG or SVG images. It supports mapping color codes (u8) to specific RGB values and defining rendering parameters.
use pixgrid::PixGrid;
use std::error::Error;
use std::fs;
use std::path::Path;
fn main() -> Result<(), Box<dyn Error>> {
let input_file_path = "instances/all_diff.pg";
// Modify here to choose the output extension and format.
let output_file_name = "nested_squares.svg";
let output_path = Path::new(output_file_name);
// 1. Reading the file
println!("Reading file: {}", input_file_path);
let input_data = fs::read_to_string(input_file_path).map_err(|e| {
format!(
"Error reading file {}: {}",
input_file_path, e
)
})?;
// 2. Parsing colors and the grid (PixGrid contains everything)
// Using the static method PixGrid::parse
let pg = PixGrid::parse(&input_data)?;
println!(
"Defined colors: {:?}",
pg.color_map.keys().collect::<Vec<_>>()
);
println!(
"Grid read: {} rows x {} columns",
pg.grid_data.len(),
pg.grid_data.first().map_or(0, |r| r.len())
);
// 3. Image generation (selection logic)
let extension = output_path
.extension()
.and_then(std::ffi::OsStr::to_str)
.unwrap_or_default();
match extension {
"png" => {
println!("Generating in PNG format...");
// Using the instance method
pg.export_png(output_path)?;
}
"svg" => {
println!("Generating in SVG format...");
// Using the instance method
pg.export_svg(output_path)?;
}
_ => {
return Err(format!(
"Unsupported file extension: '{}'. Use '.png' or '.svg'.",
extension
)
.into());
}
}
println!("Generation finished successfully!");
Ok(())
}Structsยง
- Cell
Position - PixGrid
- Represents a pixel grid configuration, containing color mappings, the grid layout, cell size, and grid line visibility settings.