docs: Document the Color type in the swf crate

This commit is contained in:
Napen123 2021-02-06 15:09:48 -07:00 committed by Mike Welsh
parent 52ebc48b2c
commit 404fc72cb5
1 changed files with 50 additions and 0 deletions

View File

@ -241,15 +241,42 @@ pub struct Rectangle {
pub y_max: Twips,
}
/// An RGBA (red, green, blue, alpha) color.
///
/// All components are stored as [`u8`] and have a color range of 0-255.
#[derive(Debug, PartialEq, Clone)]
pub struct Color {
/// The red component value.
pub r: u8,
/// The green component value.
pub g: u8,
/// The blue component value.
pub b: u8,
/// The alpha component value.
pub a: u8,
}
impl Color {
/// Creates a `Color` from a 32-bit `rgb` value and an `alpha` value.
///
/// The byte-ordering of the 32-bit `rgb` value is XXRRGGBB.
/// The most significant byte, represented by XX, is ignored;
/// the `alpha` value is provided separately.
/// This is followed by the the red (RR), green (GG), and blue (BB) components values,
/// respectively.
///
/// # Examples
///
/// ```rust
/// use swf::Color;
///
/// let red = Color::from_rgb(0xFF0000, 255);
/// let green = Color::from_rgb(0x00FF00, 255);
/// let blue = Color::from_rgb(0x0000FF, 255);
/// ```
pub fn from_rgb(rgb: u32, alpha: u8) -> Self {
Self {
r: ((rgb & 0xFF_0000) >> 16) as u8,
@ -258,6 +285,29 @@ impl Color {
a: alpha,
}
}
/// Converts the color to a 32-bit RGB value.
///
/// The alpha value does not get stored.
///
/// # Examples
///
/// Basic usage:
/// ```rust
/// use swf::Color;
///
/// let color = Color::from_rgb(0xFF00FF, 255);
/// assert_eq!(color.to_rgb(), 0xFF00FF);
/// ```
///
/// Alpha values do not get stored:
/// ```rust
/// use swf::Color;
///
/// let color1 = Color::from_rgb(0xFF00FF, 255);
/// let color2 = Color::from_rgb(0xFF00FF, 0);
/// assert_eq!(color1.to_rgb(), color2.to_rgb());
/// ```
pub fn to_rgb(&self) -> u32 {
((self.r as u32) << 16) | ((self.g as u32) << 8) | (self.b as u32)
}