#[cfg(test)]
mod tests {
use mirui::draw::{DrawCommand, Renderer, SwRenderer};
use mirui::types::{Color, Rect};
#[test]
fn fill_rect_opaque() {
let mut buf = vec![0u8; 10 * 10 * 4]; let mut r = SwRenderer::new(&mut buf, 10, 10);
let cmd = DrawCommand::Fill {
area: Rect {
x: 2,
y: 2,
w: 3,
h: 3,
},
color: Color::rgb(255, 0, 0),
radius: 0,
opa: 255,
};
let clip = Rect {
x: 0,
y: 0,
w: 10,
h: 10,
};
r.draw(&cmd, &clip);
let idx = (2 * 10 + 2) * 4;
assert_eq!(buf[idx], 255); assert_eq!(buf[idx + 1], 0); assert_eq!(buf[idx + 2], 0); assert_eq!(buf[idx + 3], 255);
assert_eq!(buf[0], 0);
assert_eq!(buf[1], 0);
assert_eq!(buf[2], 0);
assert_eq!(buf[3], 0);
}
#[test]
fn fill_rect_clipped() {
let mut buf = vec![0u8; 10 * 10 * 4];
let mut r = SwRenderer::new(&mut buf, 10, 10);
let cmd = DrawCommand::Fill {
area: Rect {
x: 0,
y: 0,
w: 10,
h: 10,
},
color: Color::rgb(0, 255, 0),
radius: 0,
opa: 255,
};
let clip = Rect {
x: 0,
y: 0,
w: 5,
h: 5,
};
r.draw(&cmd, &clip);
let idx = (3 * 10 + 3) * 4;
assert_eq!(buf[idx], 0);
assert_eq!(buf[idx + 1], 255);
let idx = (7 * 10 + 7) * 4;
assert_eq!(buf[idx], 0);
assert_eq!(buf[idx + 1], 0);
}
#[test]
fn fill_rect_semi_transparent() {
let mut buf = vec![0u8; 4 * 4 * 4]; for chunk in buf.chunks_exact_mut(4) {
chunk[0] = 255;
chunk[1] = 255;
chunk[2] = 255;
chunk[3] = 255;
}
let mut r = SwRenderer::new(&mut buf, 4, 4);
let cmd = DrawCommand::Fill {
area: Rect {
x: 0,
y: 0,
w: 4,
h: 4,
},
color: Color::rgb(0, 0, 0),
radius: 0,
opa: 128, };
let clip = Rect {
x: 0,
y: 0,
w: 4,
h: 4,
};
r.draw(&cmd, &clip);
let idx = 0;
assert!(buf[idx] > 120 && buf[idx] < 135);
}
}