ferrochart_core/geom.rs
1//! Coordinate, span and color types.
2//!
3//! Physical pixels (`Px`) and CSS (logical) pixels (`Css`) are distinct types and
4//! are only converted through `to_px(dpr)` / `to_css(dpr)`. Coordinates are never
5//! passed as bare `f32`. Everything here is `Copy` so it can be
6//! used inside a `Frame`.
7//!
8
9/// A length or a single coordinate in physical pixels.
10///
11/// # Examples
12///
13/// ```
14/// use ferrochart_core::geom::{Css, Px};
15///
16/// assert_eq!(Css(10.0).to_px(2.0), Px(20.0));
17/// assert_eq!(Px(20.0).to_css(2.0), Css(10.0));
18/// ```
19#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)]
20pub struct Px(pub f32);
21
22/// A length or a single coordinate in CSS (logical) pixels.
23///
24/// # Examples
25///
26/// ```
27/// use ferrochart_core::geom::{Css, Px};
28///
29/// assert_eq!(Css(1.5).to_px(2.0), Px(3.0));
30/// ```
31#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Default)]
32#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33pub struct Css(pub f32);
34
35impl Css {
36 /// Converts to physical pixels at device pixel ratio `dpr`.
37 pub fn to_px(self, dpr: f32) -> Px {
38 Px(self.0 * dpr)
39 }
40}
41
42impl Px {
43 /// Converts to CSS pixels at device pixel ratio `dpr`.
44 pub fn to_css(self, dpr: f32) -> Css {
45 Css(self.0 / dpr)
46 }
47}
48
49/// A point in physical pixels, origin at the top-left corner.
50///
51/// # Examples
52///
53/// ```
54/// use ferrochart_core::geom::{PointCss, PointPx};
55///
56/// let p = PointCss { x: 10.0, y: 4.0 }.to_px(2.0);
57/// assert_eq!(p, PointPx { x: 20.0, y: 8.0 });
58/// ```
59#[derive(Clone, Copy, PartialEq, Debug, Default)]
60pub struct PointPx {
61 /// Horizontal position.
62 pub x: f32,
63 /// Vertical position (down is positive).
64 pub y: f32,
65}
66
67/// A point in CSS pixels, origin at the top-left corner.
68///
69/// # Examples
70///
71/// ```
72/// use ferrochart_core::geom::PointCss;
73///
74/// assert_eq!(PointCss { x: 1.0, y: 2.0 }.to_px(1.0).x, 1.0);
75/// ```
76#[derive(Clone, Copy, PartialEq, Debug, Default)]
77pub struct PointCss {
78 /// Horizontal position.
79 pub x: f32,
80 /// Vertical position (down is positive).
81 pub y: f32,
82}
83
84impl PointCss {
85 /// Converts to physical pixels at device pixel ratio `dpr`.
86 pub fn to_px(self, dpr: f32) -> PointPx {
87 PointPx {
88 x: self.x * dpr,
89 y: self.y * dpr,
90 }
91 }
92}
93
94impl PointPx {
95 /// Converts to CSS pixels at device pixel ratio `dpr`.
96 pub fn to_css(self, dpr: f32) -> PointCss {
97 PointCss {
98 x: self.x / dpr,
99 y: self.y / dpr,
100 }
101 }
102}
103
104/// A size in physical pixels.
105///
106/// # Examples
107///
108/// ```
109/// use ferrochart_core::geom::{SizeCss, SizePx};
110///
111/// assert_eq!(SizeCss { w: 400.0, h: 300.0 }.to_px(2.0), SizePx { w: 800.0, h: 600.0 });
112/// ```
113#[derive(Clone, Copy, PartialEq, Debug, Default)]
114pub struct SizePx {
115 /// Width.
116 pub w: f32,
117 /// Height.
118 pub h: f32,
119}
120
121/// A size in CSS pixels.
122///
123/// # Examples
124///
125/// ```
126/// use ferrochart_core::geom::SizeCss;
127///
128/// let s = SizeCss { w: 800.0, h: 450.0 };
129/// assert_eq!(s.to_px(1.0).w, 800.0);
130/// ```
131#[derive(Clone, Copy, PartialEq, Debug, Default)]
132pub struct SizeCss {
133 /// Width.
134 pub w: f32,
135 /// Height.
136 pub h: f32,
137}
138
139impl SizeCss {
140 /// Converts to physical pixels at device pixel ratio `dpr`.
141 pub fn to_px(self, dpr: f32) -> SizePx {
142 SizePx {
143 w: self.w * dpr,
144 h: self.h * dpr,
145 }
146 }
147}
148
149/// An axis-aligned rectangle in physical pixels.
150///
151/// # Examples
152///
153/// ```
154/// use ferrochart_core::geom::RectPx;
155///
156/// let r = RectPx { x: 10.0, y: 20.0, w: 30.0, h: 40.0 };
157/// assert_eq!((r.right(), r.bottom()), (40.0, 60.0));
158/// ```
159#[derive(Clone, Copy, PartialEq, Debug, Default)]
160pub struct RectPx {
161 /// Left edge.
162 pub x: f32,
163 /// Top edge.
164 pub y: f32,
165 /// Width.
166 pub w: f32,
167 /// Height.
168 pub h: f32,
169}
170
171impl RectPx {
172 /// Right edge (`x + w`).
173 pub fn right(self) -> f32 {
174 self.x + self.w
175 }
176
177 /// Bottom edge (`y + h`).
178 pub fn bottom(self) -> f32 {
179 self.y + self.h
180 }
181
182 /// Whether `p` lies inside (left/top edges inclusive, right/bottom exclusive).
183 ///
184 /// # Examples
185 ///
186 /// ```
187 /// use ferrochart_core::geom::{PointPx, RectPx};
188 ///
189 /// let r = RectPx { x: 0.0, y: 0.0, w: 10.0, h: 10.0 };
190 /// assert!(r.contains(PointPx { x: 0.0, y: 9.5 }));
191 /// assert!(!r.contains(PointPx { x: 10.0, y: 5.0 }));
192 /// ```
193 pub fn contains(self, p: PointPx) -> bool {
194 p.x >= self.x && p.x < self.right() && p.y >= self.y && p.y < self.bottom()
195 }
196
197 /// Intersection with `other`; an empty rectangle (zero size) if they do not overlap.
198 ///
199 /// # Examples
200 ///
201 /// ```
202 /// use ferrochart_core::geom::RectPx;
203 ///
204 /// let a = RectPx { x: 0.0, y: 0.0, w: 10.0, h: 10.0 };
205 /// let b = RectPx { x: 5.0, y: 5.0, w: 10.0, h: 10.0 };
206 /// assert_eq!(a.intersect(b), RectPx { x: 5.0, y: 5.0, w: 5.0, h: 5.0 });
207 /// ```
208 #[must_use]
209 pub fn intersect(self, other: Self) -> Self {
210 let x = self.x.max(other.x);
211 let y = self.y.max(other.y);
212 let r = self.right().min(other.right());
213 let b = self.bottom().min(other.bottom());
214 Self {
215 x,
216 y,
217 w: (r - x).max(0.0),
218 h: (b - y).max(0.0),
219 }
220 }
221}
222
223/// A span inside one of the `Frame` pools: `start..start + len`.
224///
225/// Used instead of `Range<u32>` because `Range` is not `Copy`.
226///
227/// # Examples
228///
229/// ```
230/// use ferrochart_core::geom::Span;
231///
232/// let s = Span { start: 4, len: 3 };
233/// assert_eq!(s.range(), 4..7);
234/// ```
235#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
236pub struct Span {
237 /// First element.
238 pub start: u32,
239 /// Number of elements.
240 pub len: u32,
241}
242
243impl Span {
244 /// The span as a `usize` range, for slicing a pool.
245 pub fn range(self) -> core::ops::Range<usize> {
246 let start = self.start as usize;
247 start..start + self.len as usize
248 }
249}
250
251/// An sRGB color with straight (not premultiplied) alpha.
252///
253/// # Examples
254///
255/// ```
256/// use ferrochart_core::geom::Rgba;
257///
258/// assert_eq!(Rgba::hex(0x26A6_9A), Rgba { r: 0x26, g: 0xA6, b: 0x9A, a: 255 });
259/// assert_eq!(Rgba::hex(0x26A6_9A).with_alpha(128).a, 128);
260/// ```
261#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
262#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
263pub struct Rgba {
264 /// Red.
265 pub r: u8,
266 /// Green.
267 pub g: u8,
268 /// Blue.
269 pub b: u8,
270 /// Alpha (255 = opaque).
271 pub a: u8,
272}
273
274impl Rgba {
275 /// Opaque color from `0xRRGGBB`.
276 pub const fn hex(rgb: u32) -> Self {
277 Self {
278 r: ((rgb >> 16) & 0xFF) as u8,
279 g: ((rgb >> 8) & 0xFF) as u8,
280 b: (rgb & 0xFF) as u8,
281 a: 255,
282 }
283 }
284
285 /// The same color with alpha `a`.
286 #[must_use]
287 pub const fn with_alpha(self, a: u8) -> Self {
288 Self { a, ..self }
289 }
290
291 /// Color from `0xRRGGBBAA`, the per-point color format of
292 /// [`ValueColumns::color`](crate::data::ValueColumns).
293 ///
294 /// # Examples
295 ///
296 /// ```
297 /// use ferrochart_core::geom::Rgba;
298 ///
299 /// assert_eq!(Rgba::from_u32(0x26A6_9A80), Rgba::hex(0x26A6_9A).with_alpha(0x80));
300 /// ```
301 pub const fn from_u32(rgba: u32) -> Self {
302 Self {
303 r: (rgba >> 24) as u8,
304 g: ((rgba >> 16) & 0xFF) as u8,
305 b: ((rgba >> 8) & 0xFF) as u8,
306 a: (rgba & 0xFF) as u8,
307 }
308 }
309}