From f08648e4b5a9f7855a2ed9068a386e2f3c596322 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Tue, 16 Dec 2025 10:40:44 +0100 Subject: Initial implementation --- src/patterns.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/patterns.rs (limited to 'src/patterns.rs') diff --git a/src/patterns.rs b/src/patterns.rs new file mode 100644 index 0000000..12c790a --- /dev/null +++ b/src/patterns.rs @@ -0,0 +1,77 @@ +use crate::utils::parse_color; + +/// 8x8 1-bit pattern data +/// 0 = foreground color, 1 = background color +pub const STITCH: [u8; 64] = [ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, +]; + +pub const SHINGLES: [u8; 64] = [ + 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, + 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, +]; + +pub const SHADOW_GRID: [u8; 64] = [ + 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, +]; + +pub const WICKER: [u8; 64] = [ + 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, + 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, +]; + +/// Creates a cairo surface pattern from pattern data +pub fn create_pattern( + pattern_data: &[u8; 64], + foreground: &str, + background: &str, +) -> Result { + let surface = cairo::ImageSurface::create(cairo::Format::Rgb24, 8, 8)?; + + { + let ctx = cairo::Context::new(&surface)?; + let fg = parse_color(foreground); + let bg = parse_color(background); + + for y in 0..8 { + for x in 0..8 { + let value = pattern_data[y * 8 + x]; + let (r, g, b) = if value == 0 { fg } else { bg }; + + ctx.set_source_rgb(r, g, b); + ctx.rectangle(x as f64, y as f64, 1.0, 1.0); + ctx.fill()?; + } + } + } + + let pattern = cairo::SurfacePattern::create(&surface); + pattern.set_extend(cairo::Extend::Repeat); + Ok(pattern) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_pattern_data_lengths() { + assert_eq!(STITCH.len(), 64); + assert_eq!(SHINGLES.len(), 64); + assert_eq!(SHADOW_GRID.len(), 64); + assert_eq!(WICKER.len(), 64); + } + + #[test] + fn test_pattern_values() { + // All pattern values should be 0 or 1 + for &val in &STITCH { + assert!(val == 0 || val == 1); + } + for &val in &SHINGLES { + assert!(val == 0 || val == 1); + } + } +} -- cgit