Use Color in more places

This commit is contained in:
Jeremy Soller 2022-10-27 09:07:47 -06:00
parent 4edca946ea
commit 3ece9236b3
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
9 changed files with 84 additions and 53 deletions

View file

@ -6,10 +6,14 @@ pub use fontdb::{Family, Stretch, Style, Weight};
pub struct Color(pub u32);
impl Color {
/// Create new color with red, green, and blue components
#[inline]
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self::rgba(r, g, b, 0xFF)
}
/// Create new color with red, green, blue, and alpha components
#[inline]
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self(
((a as u32) << 24) |
@ -18,6 +22,30 @@ impl Color {
(b as u32)
)
}
/// Get the red component
#[inline]
pub fn r(&self) -> u8 {
((self.0 & 0x00FF0000) >> 16) as u8
}
/// Get the green component
#[inline]
pub fn g(&self) -> u8 {
((self.0 & 0x0000FF00) >> 8) as u8
}
/// Get the blue component
#[inline]
pub fn b(&self) -> u8 {
(self.0 & 0x000000FF) as u8
}
/// Get the alpha component
#[inline]
pub fn a(&self) -> u8 {
((self.0 & 0xFF000000) >> 24) as u8
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]