From 16d8fd7c6a39725738835bd62b31ff6b563da9e3 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 21 Mar 2024 23:20:47 +0100 Subject: [PATCH] Refactor fill color stuff --- hui/src/element/builtin/slider.rs | 6 +- hui/src/rectangle.rs | 5 +- .../{corners/corners_colors.rs => color.rs} | 88 +++++++++++++++---- hui/src/rectangle/corners.rs | 3 - 4 files changed, 76 insertions(+), 26 deletions(-) rename hui/src/rectangle/{corners/corners_colors.rs => color.rs} (54%) diff --git a/hui/src/element/builtin/slider.rs b/hui/src/element/builtin/slider.rs index 30eb69c..ec65bd5 100644 --- a/hui/src/element/builtin/slider.rs +++ b/hui/src/element/builtin/slider.rs @@ -8,7 +8,7 @@ use crate::{ element::{MeasureContext, ProcessContext, UiElement}, layout::{Size2d, compute_size}, measure::Response, - rectangle::CornersColors, + rectangle::FillColor, signal::{trigger::SignalTriggerArg, Signal}, }; @@ -42,11 +42,11 @@ pub struct Slider { /// Color of the slider handle #[setters(into)] - pub handle_color: CornersColors, + pub handle_color: FillColor, /// Color of the slider track #[setters(into)] - pub track_color: CornersColors, + pub track_color: FillColor, /// Follow mode pub follow_mode: SliderFollowMode, diff --git a/hui/src/rectangle.rs b/hui/src/rectangle.rs index 68917ff..d26f7a6 100644 --- a/hui/src/rectangle.rs +++ b/hui/src/rectangle.rs @@ -7,4 +7,7 @@ mod sides; pub use sides::Sides; mod corners; -pub use corners::{Corners, CornersColors}; +pub use corners::Corners; + +mod color; +pub use color::FillColor; diff --git a/hui/src/rectangle/corners/corners_colors.rs b/hui/src/rectangle/color.rs similarity index 54% rename from hui/src/rectangle/corners/corners_colors.rs rename to hui/src/rectangle/color.rs index 267954a..35eeffe 100644 --- a/hui/src/rectangle/corners/corners_colors.rs +++ b/hui/src/rectangle/color.rs @@ -1,72 +1,122 @@ use super::Corners; use glam::{Vec3, Vec4, vec4}; -/// Like Corners, but specialized for colors\ -/// Opaque type, needs to be casted to `Corners` to be used +/// Represents the fill color of a rectangle +/// +/// Can be a single color or a simple gradient with different colors for each corner #[derive(Clone, Copy, Debug, PartialEq)] -pub struct CornersColors(Corners); +pub struct FillColor(Corners); -impl Default for CornersColors { +impl FillColor { + pub const fn new(corners: Corners) -> Self { + Self(corners) + } + + /// Transparent background (alpha = 0) + pub const TRANSPARENT: Self = Self::rgba(0., 0., 0., 0.); + + /// Transparent background (alpha = 0) + pub const fn transparent() -> Self { + Self::TRANSPARENT + } + + /// Check if the fill color is completely transparent + /// + /// (i.e. all corners have an alpha value of 0.0) + pub fn is_transparent(&self) -> bool { + self.0.top_left.w == 0. && + self.0.top_right.w == 0. && + self.0.bottom_left.w == 0. && + self.0.bottom_right.w == 0. + } + + /// Construct a solid color fill from values representing the red, green, blue and alpha channels + pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self { + Self(Corners { + top_left: vec4(r, g, b, a), + top_right: vec4(r, g, b, a), + bottom_left: vec4(r, g, b, a), + bottom_right: vec4(r, g, b, a), + }) + } + + /// Construct a solid color fill from three values representing the red, green and blue channels + pub const fn rgb(r: f32, g: f32, b: f32) -> Self { + Self(Corners { + top_left: vec4(r, g, b, 1.0), + top_right: vec4(r, g, b, 1.0), + bottom_left: vec4(r, g, b, 1.0), + bottom_right: vec4(r, g, b, 1.0), + }) + } + + /// Construct a simple gradient fill from four colors representing the corners of the rectangle + pub const fn corners(top_left: Vec4, top_right: Vec4, bottom_left: Vec4, bottom_right: Vec4) -> Self { + Self(Corners { top_left, top_right, bottom_left, bottom_right }) + } +} + +impl Default for FillColor { fn default() -> Self { Self(Corners::all(vec4(0.0, 0.0, 0.0, 1.0))) } } -impl From> for CornersColors { +impl From> for FillColor { fn from(corners: Corners) -> Self { Self(corners) } } -impl From for Corners { - fn from(corners: CornersColors) -> Self { +impl From for Corners { + fn from(corners: FillColor) -> Self { corners.0 } } -impl From for CornersColors { +impl From for FillColor { fn from(value: Vec4) -> Self { Self(Corners::all(value)) } } -impl From<(f32, f32, f32, f32)> for CornersColors { +impl From<(f32, f32, f32, f32)> for FillColor { fn from((r, g, b, a): (f32, f32, f32, f32)) -> Self { Self(Corners::all(vec4(r, g, b, a))) } } -impl From<[f32; 4]> for CornersColors { +impl From<[f32; 4]> for FillColor { fn from([r, g, b, a]: [f32; 4]) -> Self { Self(Corners::all(vec4(r, g, b, a))) } } -impl From for CornersColors { +impl From for FillColor { fn from(value: Vec3) -> Self { Self(Corners::all(vec4(value.x, value.y, value.z, 1.0))) } } -impl From<(f32, f32, f32)> for CornersColors { +impl From<(f32, f32, f32)> for FillColor { fn from((r, g, b): (f32, f32, f32)) -> Self { Self(Corners::all(vec4(r, g, b, 1.0))) } } -impl From<[f32; 3]> for CornersColors { +impl From<[f32; 3]> for FillColor { fn from([r, g, b]: [f32; 3]) -> Self { Self(Corners::all(vec4(r, g, b, 1.0))) } } -impl From<(Vec4, Vec4, Vec4, Vec4)> for CornersColors { +impl From<(Vec4, Vec4, Vec4, Vec4)> for FillColor { fn from((top_left, top_right, bottom_left, bottom_right): (Vec4, Vec4, Vec4, Vec4)) -> Self { Self(Corners { top_left, top_right, bottom_left, bottom_right }) } } -impl From<((f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32))> for CornersColors { +impl From<((f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32))> for FillColor { fn from(value: ((f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32))) -> Self { Self(Corners { top_left: vec4(value.0.0, value.0.1, value.0.2, value.0.3), @@ -77,7 +127,7 @@ impl From<((f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32), (f3 } } -impl From<[[f32; 4]; 4]> for CornersColors { +impl From<[[f32; 4]; 4]> for FillColor { fn from(value: [[f32; 4]; 4]) -> Self { Self(Corners { top_left: vec4(value[0][0], value[0][1], value[0][2], value[0][3]), @@ -88,7 +138,7 @@ impl From<[[f32; 4]; 4]> for CornersColors { } } -impl From<(Vec3, Vec3, Vec3, Vec3)> for CornersColors { +impl From<(Vec3, Vec3, Vec3, Vec3)> for FillColor { fn from((top_left, top_right, bottom_left, bottom_right): (Vec3, Vec3, Vec3, Vec3)) -> Self { Self(Corners { top_left: vec4(top_left.x, top_left.y, top_left.z, 1.0), @@ -99,7 +149,7 @@ impl From<(Vec3, Vec3, Vec3, Vec3)> for CornersColors { } } -impl From<((f32, f32, f32), (f32, f32, f32), (f32, f32, f32), (f32, f32, f32))> for CornersColors { +impl From<((f32, f32, f32), (f32, f32, f32), (f32, f32, f32), (f32, f32, f32))> for FillColor { fn from(value: ((f32, f32, f32), (f32, f32, f32), (f32, f32, f32), (f32, f32, f32))) -> Self { Self(Corners { top_left: vec4(value.0.0, value.0.1, value.0.2, 1.0), @@ -110,7 +160,7 @@ impl From<((f32, f32, f32), (f32, f32, f32), (f32, f32, f32), (f32, f32, f32))> } } -impl From<[[f32; 3]; 4]> for CornersColors { +impl From<[[f32; 3]; 4]> for FillColor { fn from(value: [[f32; 3]; 4]) -> Self { Self(Corners { top_left: vec4(value[0][0], value[0][1], value[0][2], 1.0), diff --git a/hui/src/rectangle/corners.rs b/hui/src/rectangle/corners.rs index ea683f8..8fda415 100644 --- a/hui/src/rectangle/corners.rs +++ b/hui/src/rectangle/corners.rs @@ -1,6 +1,3 @@ -mod corners_colors; -pub use corners_colors::CornersColors; - /// Represents 4 corners of a rectangular shape. #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub struct Corners {