pub struct PixGrid {
pub color_map: HashMap<u8, Rgb<u8>>,
pub grid_data: Vec<Vec<u8>>,
pub cell_size: u32,
pub grid_color: Option<Rgb<u8>>,
}Expand description
Represents a pixel grid configuration, containing color mappings, the grid layout, cell size, and grid line visibility settings.
Fields§
§color_map: HashMap<u8, Rgb<u8>>Maps the color code (u8) found in grid_data to a specific RGB color.
grid_data: Vec<Vec<u8>>The actual grid data, where each inner Vec
cell_size: u32The size (width and height) in pixels for each cell in the grid.
grid_color: Option<Rgb<u8>>Optional RGB color for drawing grid lines. None means no lines are drawn.
Implementations§
Source§impl PixGrid
impl PixGrid
Sourcepub fn new(width: usize, height: usize) -> Self
pub fn new(width: usize, height: usize) -> Self
✨ Constructor: Creates a new PixGrid with the specified dimensions.
§Arguments
width- The number of columns (grid width).height- The number of rows (grid height).
§Default Values Used
cell_size: 1 (Default pixel size for each cell).grid_color:None(No grid lines drawn by default).default_color_code: 0 (Default color code used to fill the grid).
Sourcepub fn set_cell(
&mut self,
position: CellPosition,
color_code: u8,
) -> Result<(), &'static str>
pub fn set_cell( &mut self, position: CellPosition, color_code: u8, ) -> Result<(), &'static str>
Sets the color code of a single cell at the given logical position.
Returns Ok(()) on success or Err if the position is out of bounds.
Sourcepub fn get_cell(&self, position: CellPosition) -> Option<u8>
pub fn get_cell(&self, position: CellPosition) -> Option<u8>
Retrieves the color code of a single cell at the given logical position.
Returns Option
Sourcepub fn get_moore_neighbors(
&self,
center_pos: CellPosition,
) -> Vec<(CellPosition, u8)>
pub fn get_moore_neighbors( &self, center_pos: CellPosition, ) -> Vec<(CellPosition, u8)>
Retrieves the position and color code of the 8 surrounding cells (Moore neighborhood) for the given position. Out-of-bounds cells are skipped.
Returns a Vec<(CellPosition, u8)>.
Sourcepub fn get_von_neumann_neighbors(
&self,
center_pos: CellPosition,
) -> Vec<(CellPosition, u8)>
pub fn get_von_neumann_neighbors( &self, center_pos: CellPosition, ) -> Vec<(CellPosition, u8)>
Retrieves the position and color code of the 4 cardinal neighboring cells (Von Neumann neighborhood: Top, Bottom, Left, Right) for the given position. Out-of-bounds cells are skipped.
Returns a Vec<(CellPosition, u8)>.
Sourcepub fn parse(input_data: &str) -> Result<PixGrid, Box<dyn Error>>
pub fn parse(input_data: &str) -> Result<PixGrid, Box<dyn Error>>
Parses a string of input data into a PixGrid configuration.
The input format expects configuration parameters (cell_size, grid_color) and color definitions (code=R,G,B) followed by a separator “—” and the grid data.
§Errors
Returns an error if no color definitions are found.
Sourcepub fn dimensions(&self) -> (usize, usize)
pub fn dimensions(&self) -> (usize, usize)
Retrieves the width and height of the grid (in number of cells). Returns (width, height). Returns (0, 0) if the grid is empty.
Sourcepub fn random_sparse_fill(&mut self, cell_state: u8, density: f64)
pub fn random_sparse_fill(&mut self, cell_state: u8, density: f64)
Initializes the grid with a specific color code based on a given density (probability). Cells not assigned the activation code will remain at the default code (0).
§Arguments
cell_state: The color code (e.g., 1) to use for the randomized cells.density: The probability (0.0 to 1.0) that a cell will be set tocell_state.
Sourcepub fn random_fill_with_proportions(
&mut self,
proportions: &[(u8, f64)],
) -> Result<(), &'static str>
pub fn random_fill_with_proportions( &mut self, proportions: &[(u8, f64)], ) -> Result<(), &'static str>
Sourcepub fn export_pg(&self, output_path: &Path) -> Result<(), Box<dyn Error>>
pub fn export_pg(&self, output_path: &Path) -> Result<(), Box<dyn Error>>
Exports the current PixGrid configuration to a file in the *.pg format.
The output format includes:
- Rendering parameters (cell_size, grid_color).
- Color definitions (Code = R, G, B).
- The grid data, separated by “—”.
§Arguments
output_path- The path to the file where the data will be written.
§Returns
A Result indicating success or an error if writing to the file fails.
pub fn get_missing_codes(&self) -> Vec<u8> ⓘ
Sourcepub fn export_png(&self, output_path: &Path) -> Result<(), Box<dyn Error>>
pub fn export_png(&self, output_path: &Path) -> Result<(), Box<dyn Error>>
Export a PNG image from the stored grid data.
The image is created by scaling each u8 code in grid_data by cell_size.
Unknown color codes default to magenta (Rgb([255, 0, 255])).
§Errors
Returns an error if the grid data is empty or if image saving fails.
Sourcepub fn export_svg(&self, output_path: &Path) -> Result<(), Box<dyn Error>>
pub fn export_svg(&self, output_path: &Path) -> Result<(), Box<dyn Error>>
Export an SVG image (XML format) from the stored grid data.
Each grid cell is drawn as an SVG <rect> element.
§Errors
Returns an error if the grid data is empty or if file writing fails.
Sourcepub fn from_png(
input_path: &Path,
cell_size: u32,
grid_color_setting: Option<Rgb<u8>>,
) -> Result<PixGrid, Box<dyn Error>>
pub fn from_png( input_path: &Path, cell_size: u32, grid_color_setting: Option<Rgb<u8>>, ) -> Result<PixGrid, Box<dyn Error>>
Creates a new PixGrid structure by reading a PNG file.
It samples the top-left pixel of each cell (defined by cell_size) to determine
the cell’s color and assigns a unique u8 code to each distinct color found.
§Arguments
input_path- Path to the PNG file to read.cell_size- The expected cell size (N x N) in pixels.grid_color_setting- Sets thegrid_colorparameter for the resulting PixGrid.
§Errors
Returns an error if the image dimensions are not multiples of cell_size,
if the image is empty, or if no colors are found.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PixGrid
impl RefUnwindSafe for PixGrid
impl Send for PixGrid
impl Sync for PixGrid
impl Unpin for PixGrid
impl UnwindSafe for PixGrid
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more