use slint::{Image, Rgba8Pixel, SharedPixelBuffer};
pub const GRID: usize = 16;
pub const CELL_PX: usize = 24;
const EMPTY: [u8; 4] = [0, 0, 0, 0];
pub struct Canvas {
grid: usize,
cell_px: usize,
cells: Vec<[u8; 4]>,
}
impl Canvas {
pub fn new(grid: usize, cell_px: usize) -> Self {
Self {
grid,
cell_px,
cells: vec![EMPTY; grid * grid],
}
}
pub fn paint_brush(&mut self, col: usize, row: usize, size: usize, color: Option<[u8; 4]>) {
let size = size.max(1) as i64;
let half = size / 2;
let grid = self.grid as i64;
for dy in 0..size {
for dx in 0..size {
let c = col as i64 - half + dx;
let r = row as i64 - half + dy;
if (0..grid).contains(&c) && (0..grid).contains(&r) {
let idx = r as usize * self.grid + c as usize;
self.cells[idx] = color.unwrap_or(EMPTY);
}
}
}
}
pub fn clear(&mut self) {
self.cells.fill(EMPTY);
}
#[cfg(test)]
pub fn cell(&self, col: usize, row: usize) -> [u8; 4] {
self.cells[row * self.grid + col]
}
pub fn render_display(&self) -> Image {
let size = (self.grid * self.cell_px) as u32;
let mut buf = SharedPixelBuffer::<Rgba8Pixel>::new(size, size);
{
let bytes = buf.make_mut_bytes();
for r in 0..self.grid {
for c in 0..self.grid {
self.fill_cell(bytes, size, c, r);
}
}
self.draw_grid(bytes, size);
}
Image::from_rgba8(buf)
}
pub fn export_png(&self, path: &std::path::Path) -> Result<(), String> {
let mut data = Vec::with_capacity(self.cells.len() * 4);
for px in &self.cells {
data.extend_from_slice(px);
}
let img = image::RgbaImage::from_raw(self.grid as u32, self.grid as u32, data)
.ok_or_else(|| "无法创建 PNG 缓冲".to_string())?;
img.save(path).map_err(|e| format!("保存 PNG 失败: {e}"))
}
fn fill_cell(&self, bytes: &mut [u8], img_size: u32, col: usize, row: usize) {
let cell = self.cell_px as i32;
let x0 = (col * self.cell_px) as i32;
let y0 = (row * self.cell_px) as i32;
let checker = (col + row).is_multiple_of(2);
let (light, dark) = if checker {
([208, 208, 220, 255], [186, 186, 200, 255])
} else {
([186, 186, 200, 255], [208, 208, 220, 255])
};
let px = self.cells[row * self.grid + col];
for y in 0..cell {
for x in 0..cell {
let color = if px[3] == 0 {
if checker {
light
} else {
dark
}
} else {
let mut color = px;
if x < 2 || y < 2 {
color = shade(color, 1.18);
}
if x >= cell - 3 || y >= cell - 3 {
color = shade(color, 0.68);
}
color
};
let gx = (x0 + x) as u32;
let gy = (y0 + y) as u32;
let i = ((gy * img_size + gx) * 4) as usize;
bytes[i..i + 4].copy_from_slice(&color);
}
}
}
fn draw_grid(&self, bytes: &mut [u8], img_size: u32) {
let line = [72, 66, 96, 255];
let mut put = |x: u32, y: u32| {
let i = ((y * img_size + x) * 4) as usize;
bytes[i..i + 4].copy_from_slice(&line);
};
let cell = self.cell_px as i32;
for k in 1..=self.grid as i32 {
let x = (k * cell - 1) as u32;
for y in 0..img_size {
put(x, y);
}
let yy = (k * cell - 1) as u32;
for x in 0..img_size {
put(x, yy);
}
}
}
}
fn shade(color: [u8; 4], factor: f32) -> [u8; 4] {
let f = |v: u8| (v as f32 * factor).round().clamp(0.0, 255.0) as u8;
[f(color[0]), f(color[1]), f(color[2]), color[3]]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn paint_sets_cell() {
let mut c = Canvas::new(16, 24);
c.paint_brush(3, 4, 1, Some([255, 0, 77, 255]));
assert_eq!(c.cell(3, 4), [255, 0, 77, 255]);
assert_eq!(c.cell(3, 3), EMPTY);
}
#[test]
fn brush_clamps_at_edges() {
let mut c = Canvas::new(16, 24);
c.paint_brush(0, 0, 3, Some([0, 0, 0, 255]));
assert_eq!(c.cell(0, 0), [0, 0, 0, 255]);
assert_eq!(c.cell(1, 1), [0, 0, 0, 255]);
assert_eq!(c.cell(2, 2), EMPTY);
}
#[test]
fn erase_clears_cell() {
let mut c = Canvas::new(16, 24);
c.paint_brush(8, 8, 1, Some([1, 2, 3, 255]));
c.paint_brush(8, 8, 1, None);
assert_eq!(c.cell(8, 8), EMPTY);
}
#[test]
fn clear_resets_all() {
let mut c = Canvas::new(16, 24);
c.paint_brush(5, 5, 3, Some([9, 9, 9, 255]));
c.clear();
assert!(c.cells.iter().all(|&p| p == EMPTY));
}
#[test]
fn export_png_roundtrip() {
let dir = std::env::temp_dir();
let path = dir.join("slint_pixel_test_export.png");
let mut c = Canvas::new(16, 24);
c.paint_brush(0, 0, 1, Some([255, 0, 77, 255]));
c.export_png(&path).unwrap();
let img = image::open(&path).unwrap().to_rgba8();
assert_eq!(img.width(), 16);
assert_eq!(img.height(), 16);
assert_eq!(img.get_pixel(0, 0).0, [255, 0, 77, 255]);
assert_eq!(img.get_pixel(15, 15).0, [0, 0, 0, 0]);
std::fs::remove_file(&path).ok();
}
}