diff options
author | Genny <gen@nyble.dev> | 2020-11-06 23:33:01 -0600 |
---|---|---|
committer | Genny <gen@nyble.dev> | 2020-11-06 23:33:01 -0600 |
commit | c54872f011c135bbbc963f4a2b6b1d8ee4fa92bc (patch) | |
tree | 6ee047a5929fbd92a664c041b0596b53f58d59aa /src/components/colortable.rs | |
parent | 85b5d7de253f5f91dbca4047196e005856fd52de (diff) | |
download | gifed-c54872f011c135bbbc963f4a2b6b1d8ee4fa92bc.tar.gz gifed-c54872f011c135bbbc963f4a2b6b1d8ee4fa92bc.zip |
Add gif components
Diffstat (limited to 'src/components/colortable.rs')
-rw-r--r-- | src/components/colortable.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/components/colortable.rs b/src/components/colortable.rs new file mode 100644 index 0000000..f756169 --- /dev/null +++ b/src/components/colortable.rs @@ -0,0 +1,67 @@ +use std::ops::Deref; +pub use super::Color; + +pub struct ColorTable { + table: Vec<Color> +} + +impl ColorTable { + pub fn new() -> Self { + Self { + table: vec![] + } + } + + /// Returns the number of colors in the color table as used by the packed + /// fields in the Logical Screen Descriptor and Image Descriptor. You can + /// get the actual size with the [`len`](struct.ColorTable.html#method.len) method. + pub fn packed_len(&self) -> u8 { + ((self.table.len() as f32).log2().ceil() - 1f32) as u8 + } + + /// Returns the number of items in the table + pub fn len(&self) -> usize { + self.table.len() + } + + /// Pushes a color on to the end of the table + pub fn push(&mut self, color: Color) { + self.table.push(color); + } +} + +impl Deref for ColorTable { + type Target = [Color]; + + fn deref(&self) -> &Self::Target { + &self.table + } +} + +impl From<Vec<Color>> for ColorTable { + fn from(table: Vec<Color>) -> Self{ + ColorTable { + table + } + } +} + +impl From<&ColorTable> for Box<[u8]> { + fn from(table: &ColorTable) -> Self { + let mut vec = vec![]; + + for color in table.iter() { + vec.extend_from_slice(&[color.r, color.g, color.b]); + } + + let packed_len = 2u8.pow(table.packed_len() as u32 + 1); + let padding = (packed_len as usize - table.len()) * 3; + if padding > 0 { + vec.extend_from_slice(&vec![0; padding]); + } + + vec.into_boxed_slice() + } +} + +//TODO: TryFrom Vec<u8> (must be multiple of 3 len) and From Vec<Color> \ No newline at end of file |