diff --git a/hui/src/color.rs b/hui/src/color.rs index 69c99fd..3a9e0af 100644 --- a/hui/src/color.rs +++ b/hui/src/color.rs @@ -1,7 +1,34 @@ -//! various predefined color constants +//! various predefined color constants and helper functions use glam::{vec4, Vec4}; +/// Create a color from red, green, blue components +pub fn rgb(r: u8, g: u8, b: u8) -> Vec4 { + vec4(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0) +} + +/// Create a color from red, green, blue, alpha components +pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Vec4 { + vec4(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, a as f32 / 255.0) +} + +/// Create an RGB color from a u32 (/hex) value +pub fn rgb_hex(value: u32) -> Vec4 { + let r = (value >> 16) & 0xff; + let g = (value >> 8) & 0xff; + let b = value & 0xff; + vec4(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0) +} + +/// Create an RGBA color from a u32 (/hex) value +pub fn rgba_hex(value: u32) -> Vec4 { + let r = (value >> 16) & 0xff; + let g = (value >> 8) & 0xff; + let b = value & 0xff; + let a = (value >> 24) & 0xff; + vec4(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, a as f32 / 255.0) +} + #[cfg_attr(doc, doc="")] /// `#00000000` Transparent pub const TRANSPARENT: Vec4 = vec4(0.0, 0.0, 0.0, 0.0);