aboutsummaryrefslogtreecommitdiff
path: root/src/patterns.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/patterns.rs')
-rw-r--r--src/patterns.rs77
1 files changed, 77 insertions, 0 deletions
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<cairo::SurfacePattern, cairo::Error> {
+ 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);
+ }
+ }
+}