diff options
Diffstat (limited to 'components/pixels/lib.rs')
-rw-r--r-- | components/pixels/lib.rs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/components/pixels/lib.rs b/components/pixels/lib.rs index dc6285ebaab..de29e5fa2ee 100644 --- a/components/pixels/lib.rs +++ b/components/pixels/lib.rs @@ -4,7 +4,7 @@ extern crate euclid; -use euclid::{Rect, Size2D}; +use euclid::{Point2D, Rect, Size2D}; use std::borrow::Cow; pub fn get_rect(pixels: &[u8], size: Size2D<u32>, rect: Rect<u32>) -> Cow<[u8]> { @@ -63,3 +63,21 @@ pub fn premultiply_inplace(pixels: &mut [u8]) -> bool { pub fn multiply_u8_color(a: u8, b: u8) -> u8 { return (a as u32 * b as u32 / 255) as u8; } + +pub fn clip( + mut origin: Point2D<i32>, + mut size: Size2D<u32>, + surface: Size2D<u32>, +) -> Option<Rect<u32>> { + if origin.x < 0 { + size.width = size.width.saturating_sub(-origin.x as u32); + origin.x = 0; + } + if origin.y < 0 { + size.height = size.height.saturating_sub(-origin.y as u32); + origin.y = 0; + } + Rect::new(origin.to_u32(), size) + .intersection(&Rect::from_size(surface)) + .filter(|rect| !rect.is_empty()) +} |