rectangle/
rectangle.rs

1use palette::Srgba;
2use rough_tiny_skia::SkiaGenerator;
3use roughr::core::{FillStyle, OptionsBuilder};
4use tiny_skia::*;
5
6const WIDTH: f32 = 192.0;
7const HEIGHT: f32 = 108.0;
8/// For now, assume pixel density (dots per inch)
9const DPI: f32 = 96.;
10
11fn main() {
12    let options = OptionsBuilder::default()
13        .stroke(Srgba::from_components((114u8, 87u8, 82u8, 255u8)).into_format())
14        .fill(Srgba::from_components((254u8, 246u8, 201u8, 255u8)).into_format())
15        .fill_style(FillStyle::Hachure)
16        .fill_weight(DPI * 0.01)
17        .build()
18        .unwrap();
19    let generator = SkiaGenerator::new(options);
20    let rect_width = 100.0;
21    let rect_height = 50.0;
22    let rect = generator.rectangle::<f32>(
23        (WIDTH - rect_width) / 2.0,
24        (HEIGHT - rect_height) / 2.0,
25        rect_width,
26        rect_height,
27    );
28
29    let mut pixmap = Pixmap::new(WIDTH as u32, HEIGHT as u32).unwrap();
30    let mut background_paint = Paint::default();
31    background_paint.set_color_rgba8(150, 192, 183, 200);
32
33    pixmap.fill_rect(
34        Rect::from_xywh(0.0, 0.0, WIDTH, HEIGHT).unwrap(),
35        &background_paint,
36        Transform::identity(),
37        None,
38    );
39
40    rect.draw(&mut pixmap.as_mut());
41
42    pixmap.save_png("skia_rectangle.png").unwrap();
43}