Implement conversion between sRGB and sRGBA
This commit is contained in:
1 parent
26a03c002a
commit
74acc5ae7c
1 file changed
+37
@@ -20,8 +20,45 @@ impl SRGBA {
|
||||
Self { r, g, b, a }
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<SRGB> for SRGBA {
|
||||
fn into(self) -> SRGB {
|
||||
SRGB::new(self.r, self.g, self.b)
|
||||
}
|
||||
}
|
||||
|
||||
impl SRGB {
|
||||
pub fn new(r: f32, g: f32, b: f32) -> Self {
|
||||
Self { r, g, b }
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<SRGBA> for SRGB {
|
||||
fn into(self) -> SRGBA {
|
||||
SRGBA::new(self.r, self.g, self.b, 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn srgb_from_srgba_drops_alpha()
|
||||
{
|
||||
let srgb: SRGB = SRGBA::new(0.1, 0.2, 0.3, 0.4).into();
|
||||
assert_eq!(srgb.r, 0.1);
|
||||
assert_eq!(srgb.g, 0.2);
|
||||
assert_eq!(srgb.b, 0.3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn srgba_from_srgb_adds_alpha_1()
|
||||
{
|
||||
let srgba: SRGBA = SRGB::new(0.1, 0.2, 0.3).into();
|
||||
assert_eq!(srgba.r, 0.1);
|
||||
assert_eq!(srgba.g, 0.2);
|
||||
assert_eq!(srgba.b, 0.3);
|
||||
assert_eq!(srgba.a, 1.0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user