Refactor fill color stuff

This commit is contained in:
griffi-gh 2024-03-21 23:20:47 +01:00
parent 36345119a3
commit 99527d0dcb
4 changed files with 76 additions and 26 deletions

View file

@ -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,

View file

@ -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;

View file

@ -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<Vec4>` 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<Vec4>);
pub struct FillColor(Corners<Vec4>);
impl Default for CornersColors {
impl FillColor {
pub const fn new(corners: Corners<Vec4>) -> 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<Corners<Vec4>> for CornersColors {
impl From<Corners<Vec4>> for FillColor {
fn from(corners: Corners<Vec4>) -> Self {
Self(corners)
}
}
impl From<CornersColors> for Corners<Vec4> {
fn from(corners: CornersColors) -> Self {
impl From<FillColor> for Corners<Vec4> {
fn from(corners: FillColor) -> Self {
corners.0
}
}
impl From<Vec4> for CornersColors {
impl From<Vec4> 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<Vec3> for CornersColors {
impl From<Vec3> 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),

View file

@ -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<T> {