Skip to content

armgit / klad

Public Imported from GitHub
0

A drawing app built for agent/human collaboration

Rust

Coverage::between reads its four pixels directly

Indicode BV2b2e1aff07b8
diff --git a/crates/core/src/fill.rs b/crates/core/src/fill.rs
index 534d9b1..e4d6030 100644
--- a/crates/core/src/fill.rs
+++ b/crates/core/src/fill.rs
@@ -123,15 +123,33 @@ impl Coverage {
}
pub fn between(&self, x: f32, y: f32) -> f32 {
- let at = |x: i64, y: i64| match x >= 0 && y >= 0 {
- true => self.at(x as u32, y as u32) as f32,
- false => 0.0,
- };
let (fx, fy) = (x - 0.5, y - 0.5);
let (ix, iy) = (fx.floor() as i64, fy.floor() as i64);
let (tx, ty) = (fx - ix as f32, fy - iy as f32);
- let top = at(ix, iy) * (1.0 - tx) + at(ix + 1, iy) * tx;
- let bottom = at(ix, iy + 1) * (1.0 - tx) + at(ix + 1, iy + 1) * tx;
+ let (lx, ly, w) = (ix - self.x as i64, iy - self.y as i64, self.width as i64);
+ let [a, b, c, d] = match lx >= 0 && ly >= 0 && lx + 1 < w && ly + 1 < self.height as i64 {
+ true => {
+ let (i, row) = ((ly * w + lx) as usize, self.width as usize);
+ [
+ self.alpha[i],
+ self.alpha[i + 1],
+ self.alpha[i + row],
+ self.alpha[i + row + 1],
+ ]
+ }
+ false => {
+ let at = |x: i64, y: i64| match x >= 0 && y >= 0 {
+ true => self.at(x as u32, y as u32),
+ false => 0,
+ };
+ [at(ix, iy), at(ix + 1, iy), at(ix, iy + 1), at(ix + 1, iy + 1)]
+ }
+ };
+ if a == b && b == c && c == d {
+ return a as f32;
+ }
+ let top = a as f32 * (1.0 - tx) + b as f32 * tx;
+ let bottom = c as f32 * (1.0 - tx) + d as f32 * tx;
top * (1.0 - ty) + bottom * ty
}