1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
use cairo::{Context, Error, Extend, Format, ImageSurface, SurfacePattern};
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: (f64, f64, f64),
background: (f64, f64, f64),
) -> Result<SurfacePattern, Error> {
let surface = ImageSurface::create(Format::Rgb24, 8, 8)?;
{
let context = Context::new(&surface)?;
for y in 0_u32..8 {
for x in 0_u32..8 {
let value = pattern_data[(y * 8 + x) as usize];
let (r, g, b) = if value == 0 { foreground } else { background };
context.set_source_rgb(r, g, b);
context.rectangle(f64::from(x), f64::from(y), 1.0, 1.0);
context.fill()?;
}
}
}
let pattern = SurfacePattern::create(&surface);
pattern.set_extend(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() {
for &pixel in &STITCH {
assert!(pixel == 0 || pixel == 1);
}
for &pixel in &SHINGLES {
assert!(pixel == 0 || pixel == 1);
}
}
}
|