PixGrid

Struct PixGrid 

Source
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 is a row of color codes.

§cell_size: u32

The 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

Source

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).
Source

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.

Source

pub fn get_cell(&self, position: CellPosition) -> Option<u8>

Retrieves the color code of a single cell at the given logical position.

Returns Option, which is None if the position is out of bounds.

Source

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)>.

Source

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)>.

Source

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.

Source

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.

Source

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 to cell_state.
Source

pub fn random_fill_with_proportions( &mut self, proportions: &[(u8, f64)], ) -> Result<(), &'static str>

Fills the grid randomly according to a given vector of code proportions.

§Arguments
  • proportions: A slice of tuples (color_code: u8, proportion: f64). The sum of all proportions must be close to 1.0.
§Errors

Returns an Err if the proportions are invalid (sum is not close to 1.0).

Source

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:

  1. Rendering parameters (cell_size, grid_color).
  2. Color definitions (Code = R, G, B).
  3. 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.

Source

pub fn get_missing_codes(&self) -> Vec<u8>

Source

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.

Source

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.

Source

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 the grid_color parameter 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§

Source§

impl Clone for PixGrid

Source§

fn clone(&self) -> PixGrid

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PixGrid

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V