//! background color, gradient and texturing use glam::{vec4, Vec3, Vec4}; use crate::rectangle::Corners; //TODO: use this // pub struct Background { // pub color: BackgroundColor, // pub texture: Option // } //TODO: move this into the color module? #[derive(Clone, Copy, Default, Debug, PartialEq)] pub enum RectBackground { #[default] Transparent, Solid(Vec4), Gradient(Corners), } impl From<(f32, f32, f32, f32)> for RectBackground { fn from(color: (f32, f32, f32, f32)) -> Self { Self::Solid(vec4(color.0, color.1, color.2, color.3)) } } impl From> for RectBackground { fn from(corners: Corners) -> Self { Self::Gradient(corners) } } impl From> for RectBackground { fn from(color: Option) -> Self { match color { Some(color) => Self::Solid(color), None => Self::Transparent, } } } impl From for RectBackground { fn from(color: Vec4) -> Self { Self::Solid(color) } } impl From<(f32, f32, f32)> for RectBackground { fn from(color: (f32, f32, f32)) -> Self { Self::Solid(vec4(color.0, color.1, color.2, 1.)) } } impl From> for RectBackground { fn from(corners: Corners) -> Self { Self::Gradient(Corners { top_left: corners.top_left.extend(1.), top_right: corners.top_right.extend(1.), bottom_left: corners.bottom_left.extend(1.), bottom_right: corners.bottom_right.extend(1.), }) } } impl From> for RectBackground { fn from(color: Option) -> Self { match color { Some(color) => Self::Solid(color.extend(1.)), None => Self::Transparent, } } } impl From for RectBackground { fn from(color: Vec3) -> Self { Self::Solid(color.extend(1.)) } } impl RectBackground { /// Currently, never returns None.\ /// `Option` has been added in preparation for future changes.\ /// (`Background::Texture` etc) pub fn corners(&self) -> Option> { match *self { Self::Transparent => Some(Corners::all(Vec4::ZERO)), Self::Solid(color) => Some(Corners::all(color)), Self::Gradient(corners) => Some(corners), } } /// Returns `true` if the background is `Transparent` or all corners have an alpha value of `0`. pub fn is_transparent(&self) -> bool { match *self { Self::Transparent => true, Self::Solid(color) => color.w == 0., Self::Gradient(corners) => { let max_alpha = corners.top_left.w .max(corners.top_right.w) .max(corners.bottom_left.w) .max(corners.bottom_right.w); max_alpha == 0. }, } } } // impl From<(GradientDirection, Vec4, Vec4)> for Background { // fn from(gradient: (GradientDirection, Vec4, Vec4)) -> Self { // Self::Gradient(gradient.0, gradient.1, gradient.2) // } // }