diff options
| author | Austin Hellyer <[email protected]> | 2016-11-16 14:08:29 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2016-11-16 14:08:29 -0800 |
| commit | 24b126206e5d556054a40b65a2f229d133558c83 (patch) | |
| tree | fe0dee0f8cc4e2aa9fa7bb695cd15e2e1a7de4e2 /src | |
| parent | Travis: allow nightly to fail (diff) | |
| download | serenity-24b126206e5d556054a40b65a2f229d133558c83.tar.xz serenity-24b126206e5d556054a40b65a2f229d133558c83.zip | |
Add Colour::from_rgb
from_rgb creates a `Colour` from an RGB representation. This is meant as
the "opposite" usage of `Colour::new`.
Diffstat (limited to 'src')
| -rw-r--r-- | src/utils/colour.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/utils/colour.rs b/src/utils/colour.rs index e2c70e6..adaf1cc 100644 --- a/src/utils/colour.rs +++ b/src/utils/colour.rs @@ -65,6 +65,40 @@ impl Colour { } } + /// Generates a new Colour from an RGB value, creating an inner u32 + /// representation. + /// + /// # Examples + /// + /// Creating a `Colour` via its RGB values will set its inner u32 correctly: + /// + /// ```rust + /// use serenity::utils::Colour; + /// + /// assert!(Colour::from_rgb(255, 0, 0).value == 0xFF0000); + /// assert!(Colour::from_rgb(217, 23, 211).value == 0xD917D3); + /// ``` + /// + /// And you can then retrieve those same RGB values via its methods: + /// + /// ```rust + /// use serenity::utils::Colour; + /// + /// let colour = Colour::from_rgb(217, 45, 215); + /// + /// assert_eq!(colour.get_r(), 217); + /// assert_eq!(colour.get_g(), 45); + /// assert_eq!(colour.get_b(), 215); + /// assert_eq!(colour.get_tuple(), (217, 45, 215)); + /// ``` + pub fn from_rgb(r: u8, g: u8, b: u8) -> Colour { + let mut uint = r as u32; + uint = (uint << 8) | (g as u32); + uint = (uint << 8) | (b as u32); + + Colour::new(uint) + } + #[doc(hidden)] pub fn decode(value: Value) -> Result<Colour> { match value { |