aboutsummaryrefslogtreecommitdiff
path: root/src/patterns.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/patterns.rs')
-rw-r--r--src/patterns.rs37
1 files changed, 16 insertions, 21 deletions
diff --git a/src/patterns.rs b/src/patterns.rs
index 12c790a..254433f 100644
--- a/src/patterns.rs
+++ b/src/patterns.rs
@@ -1,7 +1,5 @@
-use crate::utils::parse_color;
+use cairo::{Context, Error, Extend, Format, ImageSurface, SurfacePattern};
-/// 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,
@@ -25,30 +23,28 @@ pub const WICKER: [u8; 64] = [
/// 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)?;
+ foreground: (f64, f64, f64),
+ background: (f64, f64, f64),
+) -> Result<SurfacePattern, Error> {
+ let surface = ImageSurface::create(Format::Rgb24, 8, 8)?;
{
- let ctx = cairo::Context::new(&surface)?;
- let fg = parse_color(foreground);
- let bg = parse_color(background);
+ let context = Context::new(&surface)?;
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 };
+ let (r, g, b) = if value == 0 { foreground } else { background };
- ctx.set_source_rgb(r, g, b);
- ctx.rectangle(x as f64, y as f64, 1.0, 1.0);
- ctx.fill()?;
+ context.set_source_rgb(r, g, b);
+ context.rectangle(x as f64, y as f64, 1.0, 1.0);
+ context.fill()?;
}
}
}
- let pattern = cairo::SurfacePattern::create(&surface);
- pattern.set_extend(cairo::Extend::Repeat);
+ let pattern = SurfacePattern::create(&surface);
+ pattern.set_extend(Extend::Repeat);
Ok(pattern)
}
@@ -66,12 +62,11 @@ mod tests {
#[test]
fn test_pattern_values() {
- // All pattern values should be 0 or 1
- for &val in &STITCH {
- assert!(val == 0 || val == 1);
+ for &pixel in &STITCH {
+ assert!(pixel == 0 || pixel == 1);
}
- for &val in &SHINGLES {
- assert!(val == 0 || val == 1);
+ for &pixel in &SHINGLES {
+ assert!(pixel == 0 || pixel == 1);
}
}
}