feat: added a from_vec function to every color that it can be constructed with a Vec4 to easily interpolate colors

This commit is contained in:
lisk77 2025-03-30 14:54:28 +02:00
parent f891de2909
commit 13fd31f632
13 changed files with 46 additions and 208 deletions

View file

@ -109,4 +109,8 @@ impl Color for Hsla {
fn to_vec(&self) -> Vec4 {
Vec4::new(self.hue, self.saturation, self.lightness, self.alpha)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x(), color.y(), color.z(), color.w())
}
}

View file

@ -107,4 +107,8 @@ impl Color for Hsva {
fn to_vec(&self) -> Vec4 {
Vec4::new(self.hue, self.saturation, self.value, self.alpha)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x(), color.y(), color.z(), color.w())
}
}

View file

@ -170,4 +170,8 @@ impl Color for Hwba {
fn to_vec(&self) -> Vec4 {
Vec4::new(self.hue, self.whiteness, self.blackness, self.alpha)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x(), color.y(), color.z(), color.w())
}
}

View file

@ -151,4 +151,8 @@ impl Color for Laba {
fn to_vec(&self) -> Vec4 {
Vec4::new(self.lightness, self.a, self.b, self.alpha)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x(), color.y(), color.z(), color.w())
}
}

View file

@ -101,4 +101,8 @@ impl Color for Lcha {
fn to_vec(&self) -> Vec4 {
Vec4::new(self.lightness, self.chroma, self.hue, self.alpha)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x(), color.y(), color.z(), color.w())
}
}

View file

@ -25,4 +25,5 @@ mod oklcha;
pub trait Color: Copy {
fn to_wgpu(&self) -> wgpu::Color;
fn to_vec(&self) -> Vec4;
fn from_vec(color: Vec4) -> Self;
}

View file

@ -139,4 +139,8 @@ impl Color for LinearRgba {
fn to_vec(&self) -> Vec4 {
Vec4::new(self.red, self.green, self.blue, self.alpha)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x(), color.y(), color.z(), color.w())
}
}

View file

@ -121,4 +121,8 @@ impl Color for Oklaba {
fn to_vec(&self) -> Vec4 {
Vec4::new(self.lightness, self.a, self.b, self.alpha)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x(), color.y(), color.z(), color.w())
}
}

View file

@ -100,4 +100,8 @@ impl Color for Oklcha {
fn to_vec(&self) -> Vec4 {
Vec4::new(self.lightness, self.chroma, self.hue, self.alpha)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x(), color.y(), color.z(), color.w())
}
}

View file

@ -352,6 +352,10 @@ impl Color for sRgba<f32> {
fn to_vec(&self) -> Vec4 {
Vec4::new(self.red, self.green, self.blue, self.alpha)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x(), color.y(), color.z(), color.w())
}
}
impl Color for sRgba<u8> {
@ -367,4 +371,8 @@ impl Color for sRgba<u8> {
self.alpha as f32
)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x() as u8, color.y() as u8, color.z() as u8, color.w() as u8)
}
}

View file

@ -117,4 +117,8 @@ impl Color for Xyza {
fn to_vec(&self) -> Vec4 {
Vec4::new(self.x, self.y, self.z, self.alpha)
}
fn from_vec(color: Vec4) -> Self {
Self::new(color.x(), color.y(), color.z(), color.w())
}
}