Clamps the user-supplied `xy` coordinates to the LCD space.
(&self, xy: PixelsXY)
| 139 | |
| 140 | /// Clamps the user-supplied `xy` coordinates to the LCD space. |
| 141 | fn clamp_xy(&self, xy: PixelsXY) -> LcdXY { |
| 142 | fn clamp(value: i16, max: usize) -> usize { |
| 143 | if value < 0 { |
| 144 | 0 |
| 145 | } else { |
| 146 | let value = usize::try_from(value).expect("Positive value must fit"); |
| 147 | if value > max { max } else { value } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | LcdXY { |
| 152 | x: clamp(xy.x, self.size_pixels.width - 1), |
| 153 | y: clamp(xy.y, self.size_pixels.height - 1), |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /// Given a top-left `xy` coordinate, adds the user-supplied `size` to it and clamps the result |
| 158 | /// to the LCD space. |