summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 5032f68..021f25a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -846,14 +846,17 @@ macro_rules! byte_of_hex {
};
}
-struct Rgb(u8, u8, u8);
+pub struct Rgb(pub u8, pub u8, pub u8);
impl Rgb
{
+ #[inline]
fn r(&self) -> u8 { self.0 }
+ #[inline]
fn g(&self) -> u8 { self.1 }
+ #[inline]
fn b(&self) -> u8 { self.2 }
}
@@ -871,6 +874,18 @@ impl TryFrom<&[u8; 6]> for Rgb
}
}
+impl From<[u8; 3]> for Rgb
+{
+ fn from(bytes: [u8; 3]) -> Self
+ {
+ let r = bytes[0];
+ let g = bytes[1];
+ let b = bytes[2];
+
+ Self(r, g, b)
+ }
+}
+
impl From<u32> for Rgb
{
fn from(rgb: u32) -> Self
@@ -1028,6 +1043,7 @@ impl Palette
Ok(res)
}
+ pub fn iter(&self) -> PaletteIterator { PaletteIterator::new(&self) }
/* [Palette::from_stdin] */
} /* [impl Palette] */
@@ -1109,6 +1125,38 @@ impl From<&RawPalette> for Palette
}
}
+pub struct PaletteIterator
+{
+ pal: Palette,
+ cur: usize,
+}
+
+impl PaletteIterator
+{
+ fn new(pal: &Palette) -> Self { Self { pal: pal.clone(), cur: 0 } }
+}
+
+impl Iterator for PaletteIterator
+{
+ type Item = Rgb;
+
+ fn next(&mut self) -> Option<Self::Item>
+ {
+ if self.cur >= PALETTE_SIZE {
+ None
+ } else {
+ let off = self.cur * 3;
+ let rgb = Rgb::from([
+ self.pal.0[off],
+ self.pal.0[off + 1],
+ self.pal.0[off + 2],
+ ]);
+ self.cur += 1;
+ Some(rgb)
+ }
+ }
+}
+
const CONSOLE_PATHS: [&str; 6] = [
"/proc/self/fd/0",
"/dev/tty",