From adc525cf75a08eef7aa96c7ca56eef5617becc06 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 7 Aug 2022 11:55:44 +0200 Subject: edit: present color palette No actual editing functionality yet, just the colors layed out as squares on an 8?2 grid. --- src/lib.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'src/lib.rs') 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 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 + { + 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", -- cgit v1.2.3