Add inversion functions, rename check_rgba -> new

This commit is contained in:
Clark Moody 2020-02-21 15:40:37 -06:00
parent 62fddce2e6
commit 27a4cbccea

View file

@ -1,6 +1,5 @@
/// A color in the sRGB color space. /// A color in the sRGB color space.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
#[allow(missing_docs)]
pub struct Color { pub struct Color {
/// Red component, 0.0 - 1.0 /// Red component, 0.0 - 1.0
pub r: f32, pub r: f32,
@ -12,7 +11,6 @@ pub struct Color {
pub a: f32, pub a: f32,
} }
impl Color { impl Color {
/// The black color. /// The black color.
pub const BLACK: Color = Color { pub const BLACK: Color = Color {
@ -44,7 +42,7 @@ impl Color {
} }
/// Ensures RGBA values on the range [0.0, 1.0] /// Ensures RGBA values on the range [0.0, 1.0]
pub fn check_rgba(r: f32, g: f32, b: f32, a:f32) -> Color { pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color {
Color { Color {
r: Color::clamp(r), r: Color::clamp(r),
g: Color::clamp(g), g: Color::clamp(g),
@ -57,7 +55,7 @@ impl Color {
/// ///
/// [`Color`]: struct.Color.html /// [`Color`]: struct.Color.html
pub fn from_rgb(r: f32, g: f32, b: f32) -> Color { pub fn from_rgb(r: f32, g: f32, b: f32) -> Color {
Color::check_rgba(r, g, b, 1.0) Color::new(r, g, b, 1.0)
} }
/// Creates a [`Color`] from its RGB8 components. /// Creates a [`Color`] from its RGB8 components.
@ -100,6 +98,18 @@ impl Color {
self.a, self.a,
] ]
} }
/// Invert the Color in-place
pub fn invert(&mut self) {
self.r = Color::clamp(1.0f32 - self.r);
self.b = Color::clamp(1.0f32 - self.g);
self.g = Color::clamp(1.0f32 - self.b);
}
/// Return an inverted Color
pub fn inverse(self) -> Color {
Color::new(1.0f32 - self.r, 1.0f32 - self.g, 1.0f32 - self.b, self.a)
}
} }
impl From<[f32; 3]> for Color { impl From<[f32; 3]> for Color {