hUI/hui/src/background.rs

114 lines
2.9 KiB
Rust
Raw Normal View History

2024-02-27 13:31:12 -06:00
//! background color, gradient and texturing
use glam::{vec4, Vec3, Vec4};
2024-02-20 13:56:58 -06:00
use crate::rectangle::Corners;
2024-02-25 08:59:12 -06:00
//TODO: use this
// pub struct Background {
// pub color: BackgroundColor,
// pub texture: Option<TextureH>
2024-02-20 13:56:58 -06:00
// }
2024-02-29 09:02:05 -06:00
//TODO: move this into the color module?
2024-02-20 13:56:58 -06:00
#[derive(Clone, Copy, Default, Debug, PartialEq)]
2024-02-29 09:02:05 -06:00
pub enum RectBackground {
2024-02-20 13:56:58 -06:00
#[default]
Transparent,
Solid(Vec4),
Gradient(Corners<Vec4>),
}
2024-02-29 09:02:05 -06:00
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))
}
}
2024-02-29 09:02:05 -06:00
impl From<Corners<Vec4>> for RectBackground {
fn from(corners: Corners<Vec4>) -> Self {
Self::Gradient(corners)
2024-02-20 13:56:58 -06:00
}
}
2024-02-29 09:02:05 -06:00
impl From<Option<Vec4>> for RectBackground {
2024-02-20 13:56:58 -06:00
fn from(color: Option<Vec4>) -> Self {
match color {
Some(color) => Self::Solid(color),
None => Self::Transparent,
}
}
}
2024-02-29 09:02:05 -06:00
impl From<Vec4> for RectBackground {
fn from(color: Vec4) -> Self {
Self::Solid(color)
}
}
2024-02-29 09:02:05 -06:00
impl From<(f32, f32, f32)> for RectBackground {
fn from(color: (f32, f32, f32)) -> Self {
Self::Solid(vec4(color.0, color.1, color.2, 1.))
}
}
2024-02-29 09:02:05 -06:00
impl From<Corners<Vec3>> for RectBackground {
fn from(corners: Corners<Vec3>) -> 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.),
})
}
}
2024-02-29 09:02:05 -06:00
impl From<Option<Vec3>> for RectBackground {
fn from(color: Option<Vec3>) -> Self {
match color {
Some(color) => Self::Solid(color.extend(1.)),
None => Self::Transparent,
}
}
}
2024-02-29 09:02:05 -06:00
impl From<Vec3> for RectBackground {
fn from(color: Vec3) -> Self {
Self::Solid(color.extend(1.))
}
}
2024-02-29 09:02:05 -06:00
impl RectBackground {
2024-02-20 13:56:58 -06:00
/// Currently, never returns None.\
/// `Option` has been added in preparation for future changes.\
/// (`Background::Texture` etc)
pub fn corners(&self) -> Option<Corners<Vec4>> {
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)
// }
// }