hUI/hui/src/background.rs

106 lines
2.6 KiB
Rust
Raw Normal View History

2024-02-27 19:31:12 +00:00
//! background color, gradient and texturing
use glam::{vec4, Vec3, Vec4};
2024-02-20 19:56:58 +00:00
use crate::rectangle::Corners;
2024-02-25 14:59:12 +00:00
//TODO: use this
// pub struct Background {
// pub color: BackgroundColor,
// pub texture: Option<TextureH>
2024-02-20 19:56:58 +00:00
// }
2024-02-29 15:02:05 +00:00
//TODO: move this into the color module?
2024-02-20 19:56:58 +00:00
#[derive(Clone, Copy, Default, Debug, PartialEq)]
2024-03-06 23:50:47 +00:00
pub enum BackgroundColor {
2024-02-20 19:56:58 +00:00
#[default]
Transparent,
Solid(Vec4),
Gradient(Corners<Vec4>),
}
2024-03-06 23:50:47 +00:00
impl From<(f32, f32, f32, f32)> for BackgroundColor {
fn from(color: (f32, f32, f32, f32)) -> Self {
Self::Solid(vec4(color.0, color.1, color.2, color.3))
}
}
2024-03-06 23:50:47 +00:00
impl From<Corners<Vec4>> for BackgroundColor {
fn from(corners: Corners<Vec4>) -> Self {
Self::Gradient(corners)
2024-02-20 19:56:58 +00:00
}
}
2024-03-06 23:50:47 +00:00
impl From<Option<Vec4>> for BackgroundColor {
2024-02-20 19:56:58 +00:00
fn from(color: Option<Vec4>) -> Self {
match color {
Some(color) => Self::Solid(color),
None => Self::Transparent,
}
}
}
2024-03-06 23:50:47 +00:00
impl From<Vec4> for BackgroundColor {
fn from(color: Vec4) -> Self {
Self::Solid(color)
}
}
2024-03-06 23:50:47 +00:00
impl From<(f32, f32, f32)> for BackgroundColor {
fn from(color: (f32, f32, f32)) -> Self {
Self::Solid(vec4(color.0, color.1, color.2, 1.))
}
}
2024-03-06 23:50:47 +00:00
impl From<Corners<Vec3>> for BackgroundColor {
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-03-06 23:50:47 +00:00
impl From<Option<Vec3>> for BackgroundColor {
fn from(color: Option<Vec3>) -> Self {
match color {
Some(color) => Self::Solid(color.extend(1.)),
None => Self::Transparent,
}
}
}
2024-03-06 23:50:47 +00:00
impl From<Vec3> for BackgroundColor {
fn from(color: Vec3) -> Self {
Self::Solid(color.extend(1.))
}
}
2024-03-06 23:50:47 +00:00
impl BackgroundColor {
/// Returns the colors of individual corners
pub fn corners(&self) -> Corners<Vec4> {
2024-02-20 19:56:58 +00:00
match *self {
2024-03-06 23:50:47 +00:00
Self::Transparent => Corners::all(Vec4::ZERO),
Self::Solid(color) => Corners::all(color),
Self::Gradient(corners) => corners,
2024-02-20 19:56:58 +00:00
}
}
/// 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.
},
}
}
}