image_builder/rect.rs
1use image::Rgba;
2
3use crate::colors::{self, Color};
4
5/// Specifications of a rectangular shape.
6#[derive(Clone)]
7pub struct Rect {
8 position: (u32, u32),
9 size: (u32, u32),
10 color: Color,
11}
12impl Rect {
13 /// This method instantiates a specifications of a rectangular shape.
14 /// ## Example
15 /// ```
16 /// use image_builder::Rect;
17 ///
18 /// Rect::new();
19 /// ```
20 pub fn new() -> Rect {
21 Rect {
22 position: (0, 0),
23 size: (10, 10),
24 color: colors::GREEN,
25 }
26 }
27
28 /// This method allows you to adjust the position of the rect within the image being constructed.
29 /// ## Example
30 /// ```
31 /// use image_builder::Rect;
32 ///
33 /// Rect::new()
34 /// .position(100, 100);
35 /// ```
36 pub fn position(&mut self, x: u32, y: u32) -> Self {
37 self.position = (x, y);
38 self.clone()
39 }
40
41 /// Define the size of the rect.
42 /// ## Example
43 /// ```
44 /// use image_builder::Rect;
45 ///
46 /// Rect::new()
47 /// .size(150, 50);
48 /// ```
49 pub fn size(&mut self, width: u32, height: u32) -> Self {
50 self.size = (width, height);
51 self.clone()
52 }
53
54 /// Define the color of the rect.
55 /// ## Examples
56 /// ```
57 /// use image_builder::{Rect, colors};
58 ///
59 /// Rect::new()
60 /// .color(colors::RED);
61 /// ```
62 /// ```
63 /// use image_builder::Rect;
64 ///
65 /// Rect::new()
66 /// .color([150, 30, 255, 150]); // rgba values
67 /// ```
68 pub fn color(&mut self, color: Color) -> Self {
69 self.color = color;
70 self.clone()
71 }
72}
73
74#[derive(Clone)]
75pub struct RectValues {
76 pub x: i32,
77 pub y: i32,
78 pub width: u32,
79 pub height: u32,
80 pub color: Rgba<u8>,
81}
82pub fn extract(rect: &Rect) -> RectValues {
83 RectValues {
84 x: rect.position.0 as i32,
85 y: rect.position.1 as i32,
86 width: rect.size.0,
87 height: rect.size.1,
88 color: Rgba(rect.color),
89 }
90}