Implement some std::op traits for Pixel

This commit is contained in:
cdo committed 2026-10-25 22:24:52 +00:00
1 parent 25f2db9765
commit bab002fefd
1 file changed
+65 -3
+65 -3
View File
@@ -39,9 +39,71 @@ pub trait Format {
} }
impl Extent { impl Extent {
pub fn size(&self) -> u32 { pub fn size(&self) -> u32 {
self.x * self.y self.x * self.y
} }
}
impl Pixel {
pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
}
impl std::ops::AddAssign for Pixel {
fn add_assign(&mut self, other: Pixel) {
self.r += other.r;
self.g += other.g;
self.b += other.b;
self.a += other.a;
}
}
impl std::ops::MulAssign<f32> for Pixel {
fn mul_assign(&mut self, scalar: f32) {
self.r *= scalar;
self.g *= scalar;
self.b *= scalar;
self.a *= scalar;
}
}
impl std::ops::DivAssign<f32> for Pixel {
fn div_assign(&mut self, scalar: f32) {
self.r /= scalar;
self.g /= scalar;
self.b /= scalar;
self.a /= scalar;
}
}
impl std::ops::Mul<f32> for Pixel {
type Output = Self;
fn mul(mut self, scalar: f32) -> Self {
self *= scalar;
self
}
}
impl std::ops::Div<f32> for Pixel {
type Output = Self;
fn div(mut self, scalar: f32) -> Self {
self /= scalar;
self
}
}
impl std::default::Default for Pixel {
fn default() -> Self {
Self {
r: 0.0,
g: 0.0,
b: 0.0,
a: 0.0,
}
}
} }
impl From<std::io::Error> for Error { impl From<std::io::Error> for Error {