1#[derive(Debug, Clone, Copy)]
3pub struct Area {
4 pub x: u16,
6 pub y: u16,
8 pub w: u16,
10 pub h: u16,
12}
13
14impl Area {
15 pub const fn new(x: u16, y: u16, w: u16, h: u16) -> Self {
16 Self { x, y, w, h }
17 }
18
19 pub const fn from_origin(w: u16, h: u16) -> Self {
20 Self { x: 0, y: 0, w, h }
21 }
22
23 pub const fn from_origin_size(size: (u16, u16)) -> Self {
24 Self {
25 x: 0,
26 y: 0,
27 w: size.0,
28 h: size.1,
29 }
30 }
31
32 pub const fn position(&self) -> (u16, u16) {
33 (self.x, self.y)
34 }
35
36 pub const fn total_pixels(&self) -> usize {
37 self.w as usize * self.h as usize
38 }
39
40 pub const fn bottom_right(&self) -> (u16, u16) {
41 (self.x + self.w - 1, self.y + self.h - 1)
42 }
43}
44
45#[cfg(feature = "embedded-graphics")]
63mod eg_impls {
64 use super::*;
65 use embedded_graphics_core::prelude::*;
66 use embedded_graphics_core::primitives::Rectangle;
67
68 impl From<Rectangle> for Area {
69 fn from(value: Rectangle) -> Self {
70 Area {
71 x: value.top_left.x as _,
72 y: value.top_left.y as _,
73 w: value.size.width as _,
74 h: value.size.height as _,
75 }
76 }
77 }
78
79 impl From<Area> for Rectangle {
80 fn from(value: Area) -> Self {
81 Rectangle {
82 top_left: Point::new(value.x as _, value.y as _),
83 size: Size::new(value.w as _, value.h as _),
84 }
85 }
86 }
87}