From dc902a1cd718770bffcecfa4fffa8142db37a7e6 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 2 Apr 2021 10:16:42 +0530 Subject: add rylander dithering, `define` macro, begin work on guides. --- src/dither.rs | 66 ++++++++++++++++++++++++++++++++++++++--------------- src/guide.rs | 7 ++++++ src/lisp/prelude.rs | 23 +++++++++++++++---- 3 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 src/guide.rs (limited to 'src') diff --git a/src/dither.rs b/src/dither.rs index c19bd6d..99e2839 100644 --- a/src/dither.rs +++ b/src/dither.rs @@ -1,24 +1,5 @@ use crate::bitmap::MapPoint; -// dither intensities (rylander) -// 0: none -// 1: (1, 3) -// 2: (3, 1) -// 3: (3, 3) -// 4: (1, 1) -// 5: (1, 2) -// 6: (3, 0) -// 7: (3, 2) -// 8: (1, 0) -// 9: (0, 3) -// 10: (2, 1) -// 11: (2, 3) -// 12: (0, 1) -// 13: (0, 2) -// 14: (2, 0) -// 15: (2, 2) -// 16: (0, 0) - // dither intensities (bayers) // 0: none // 1: (0, 0) @@ -65,3 +46,50 @@ pub fn bayer(level: u8, pt: MapPoint) -> bool { } } } + +// dither intensities (rylander) +// 0: none +// 1: (1, 3) +// 2: (3, 1) +// 3: (3, 3) +// 4: (1, 1) +// 5: (1, 2) +// 6: (3, 0) +// 7: (3, 2) +// 8: (1, 0) +// 9: (0, 3) +// 10: (2, 1) +// 11: (2, 3) +// 12: (0, 1) +// 13: (0, 2) +// 14: (2, 0) +// 15: (2, 2) +// 16: (0, 0) + +pub fn rylander(level: u8, pt: MapPoint) -> bool { + if level == 0 { + false + } else { + let MapPoint { x, y } = pt; + let r = (x % 4, y % 4); + match level { + 01 => r == (1, 3) || rylander(00, pt), + 02 => r == (3, 1) || rylander(01, pt), + 03 => r == (3, 3) || rylander(02, pt), + 04 => r == (1, 1) || rylander(03, pt), + 05 => r == (1, 2) || rylander(04, pt), + 06 => r == (3, 0) || rylander(05, pt), + 07 => r == (3, 2) || rylander(06, pt), + 08 => r == (1, 0) || rylander(07, pt), + 09 => r == (0, 3) || rylander(08, pt), + 10 => r == (2, 1) || rylander(09, pt), + 11 => r == (2, 3) || rylander(10, pt), + 12 => r == (0, 1) || rylander(11, pt), + 13 => r == (0, 2) || rylander(12, pt), + 14 => r == (2, 0) || rylander(13, pt), + 15 => r == (2, 2) || rylander(14, pt), + 16 => r == (0, 0) || rylander(15, pt), + _ => panic!("Invalid dither level!"), + } + } +} diff --git a/src/guide.rs b/src/guide.rs new file mode 100644 index 0000000..006566e --- /dev/null +++ b/src/guide.rs @@ -0,0 +1,7 @@ +use crate::bitmap::Axis; + +#[derive(Debug)] +pub struct Guide { + axis: Axis, + offset: u32, +} diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs index 4a6a4ed..372bb00 100644 --- a/src/lisp/prelude.rs +++ b/src/lisp/prelude.rs @@ -26,6 +26,13 @@ macro_rules! primitive { }; } +#[macro_export] +macro_rules! define { + ($env:expr, $name:expr, $value:expr) => { + let _ = $env.insert($name.to_string(), $value); + }; +} + #[macro_export] macro_rules! type_match { ($args:expr, $($range:literal => $kind:pat),+) => { @@ -54,6 +61,8 @@ macro_rules! type_match { pub fn new_env() -> Result { let mut env = Environment::new(); + define!(env, "else", LispExpr::BoolLit(true)); + primitive!(env, Arity::Atleast(2), "+", |args, _| { let nums = args .into_iter() @@ -98,11 +107,6 @@ pub fn new_env() -> Result { Ok(LispExpr::Number(acc)) }); - primitive!(env, Arity::Exact(0), "toggle-grid", |_, app| { - app.toggle_grid(); - Ok(LispExpr::Unit) - }); - primitive!(env, Arity::Atleast(2), "and", |args, _| { if args .iter() @@ -252,6 +256,15 @@ pub fn new_env() -> Result { } }); + primitive!(env, Arity::Exact(0), "grid-enabled?", |_, app| { + Ok(LispExpr::BoolLit(app.grid.enabled)) + }); + + primitive!(env, Arity::Exact(1), "set-grid!", |args, app| { + app.grid.enabled = args[0].cast_bool(); + Ok(LispExpr::Unit) + }); + primitive!(env, Arity::Exact(0), "brush-fill", |_, app| { app.brush = Brush::Fill; return Ok(LispExpr::Unit); -- cgit v1.2.3