diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 31 |
1 files changed, 28 insertions, 3 deletions
@@ -1,23 +1,31 @@ use proportion::*; + #[derive(Copy, Clone, Default, PartialEq, Eq, Hash)] pub struct Colour { - /// Channel order for nybbles is `0xAARRGGBB`. + /// Colour encoded as `0xAARRGGBB`. pub value: u32, } impl Colour { + /// Construct a Colour from an RGB u32. pub const fn rgb(value: u32) -> Self { Self { value: value | 0xff000000, } } + + /// Construct a Colour from an RGBA u32. pub const fn rgba(value: u32) -> Self { Self { value } } + + /// Construct a Colour from a brightness value. pub const fn grey(value: u32) -> Self { Self::rgb((value & 0xf) * 0x111111) } + + /// Construct a Colour from separate red, green, and blue channel values. pub const fn from_rgb(red: u8, green: u8, blue: u8) -> Self { let mut value = 0x0000ff00; value |= red as u32; @@ -25,6 +33,8 @@ impl Colour { value <<= 8; value |= blue as u32; Self { value } } + + /// Construct a Colour from separate red, green, blue, and alpha channel values. pub const fn from_rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self { let mut value = alpha as u32; value <<= 8; value |= red as u32; @@ -32,6 +42,8 @@ impl Colour { value <<= 8; value |= blue as u32; Self { value } } + + /// Construct a colour by blending two colours by a proportion. pub fn mix<I: proportion::Internal>(bg: Self, fg: Self, proportion: Proportion<I>) -> Self { let proportion = Proportion::<u8>::new(proportion.as_u8()); let bg_mix = proportion.invert(); @@ -41,6 +53,8 @@ impl Colour { let blue = (bg.blue() * bg_mix) + (fg.blue() * fg_mix); Self::from_rgb(red, green, blue) } + + /// Construct a colour by blending two colours by the alpha of the fg colour. pub fn mix_with_alpha(bg: Self, fg: Self) -> Self { let proportion = Proportion::new(fg.alpha()); let bg_mix = proportion.invert(); @@ -60,6 +74,7 @@ impl Colour { pub fn as_rgba_array(&self) -> [u8; 4] { [self.red(), self.green(), self.blue(), self.alpha()] } + pub fn as_rgb_hex(&self) -> String { format!("{:02x}{:02x}{:02x}", self.red(), self.green(), self.blue()) @@ -69,30 +84,39 @@ impl Colour { format!("{:02x}{:02x}{:02x}{:02x}", self.red(), self.green(), self.blue(), self.alpha()) } + pub fn red(&self) -> u8 { (self.value >> 16) as u8 } + pub fn green(&self) -> u8 { (self.value >> 8) as u8 } + pub fn blue(&self) -> u8 { self.value as u8 } + pub fn alpha(&self) -> u8 { (self.value >> 24) as u8 } + pub fn is_opaque(&self) -> bool { self.alpha() == 0xff } + pub fn with_red(&self, red: u8) -> Self { Colour::rgba((self.value & 0xffffff00) | (red as u32) << 16) } + pub fn with_green(&self, green: u8) -> Self { Colour::rgba((self.value & 0xffff00ff) | (green as u32) << 8) } + pub fn with_blue(&self, blue: u8) -> Self { Colour::rgba((self.value & 0xff00ffff) | blue as u32) } + pub fn with_alpha(&self, alpha: u8) -> Self { Colour::rgba((self.value & 0x00ffffff) | (alpha as u32) << 24) } @@ -100,17 +124,18 @@ impl Colour { pub fn set_red(&mut self, red: u8) { self.value = (self.value & 0xffffff00) | (red as u32) << 16 } + pub fn set_green(&mut self, green: u8) { self.value = (self.value & 0xffff00ff) | (green as u32) << 8 } + pub fn set_blue(&mut self, blue: u8) { self.value = (self.value & 0xff00ffff) | blue as u32 } + pub fn set_alpha(&mut self, alpha: u8) { self.value = (self.value & 0x00ffffff) | (alpha as u32) << 24 } - - } impl Colour { |