tinydraw/
lib.rs

1//! **tinydraw** is a small library for 2D drawing in Rust.
2//! It is used for drawing basic, anti-aliased shapes onto images.
3//! Support for reading and exporting images as PNG or bytes is included.
4//!
5//! Example usage:
6//! ```rust
7//! use tinydraw::ImageRGB8;
8//!
9//! let background_color: [u8; 3] = [255, 155, 0];
10//! let mut image: ImageRGB8 = ImageRGB8::new(640, 360, background_color);
11//!
12//! image.draw_line(0, 0, 639, 359, [255, 255, 255], 1, 1.0);
13//! image.draw_line(0, 359, 639, 0, [255, 255, 255], 1, 1.0);
14//! image.draw_rectangle(0, 0, 639, 359, [255, 255, 255], 3, 1.0);
15//! image.draw_ellipse(319, 179, 300, 150, [0, 0, 0], 0, 0.5);
16//! image.draw_circle(149, 179, 30, [255, 255, 255], 0, 1.0);
17//! image.draw_circle(149, 179, 20, [0, 0, 0], 0, 1.0);
18//! image.draw_circle(489, 179, 30, [255, 255, 255], 0, 1.0);
19//! image.draw_circle(489, 179, 20, [0, 0, 0], 0, 1.0);
20//! image.draw_ellipse(319, 90, 80, 30, [255, 255, 255], 0, 1.0);
21//! image.draw_ellipse(319, 90, 60, 20, [0, 0, 0], 0, 1.0);
22//!
23//! let bytes: &[u8] = image.to_bytes(); // get image as bytes
24//! // image.to_png("image.png").unwrap(); // export image as PNG
25//! ```
26//!
27//! **Shapes:** line, rectangle, ellipse, circle
28//!
29//! **Colorspaces:** RGB8
30
31pub mod image;
32#[doc(inline)]
33pub use image::ImageRGB8;
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn example_1() {
41        let background_color: [u8; 3] = [255, 155, 0];
42        let mut image: ImageRGB8 = ImageRGB8::new(640, 360, background_color);
43
44        image.draw_line(0, 0, 639, 359, [255, 255, 255], 1, 1.0);
45        image.draw_line(0, 359, 639, 0, [255, 255, 255], 1, 1.0);
46
47        image.draw_rectangle(0, 0, 639, 359, [255, 255, 255], 3, 1.0);
48
49        image.draw_ellipse(319, 179, 300, 150, [0, 0, 0], 0, 0.5);
50
51        image.draw_circle(149, 179, 30, [255, 255, 255], 0, 1.0);
52        image.draw_circle(149, 179, 20, [0, 0, 0], 0, 1.0);
53
54        image.draw_circle(489, 179, 30, [255, 255, 255], 0, 1.0);
55        image.draw_circle(489, 179, 20, [0, 0, 0], 0, 1.0);
56
57
58        image.draw_ellipse(319, 90, 80, 30, [255, 255, 255], 0, 1.0);
59        image.draw_ellipse(319, 90, 60, 20, [0, 0, 0], 0, 1.0);
60
61        let _bytes: &[u8] = image.to_bytes();
62        // image.to_png("image.png").unwrap();
63    }
64}