diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 61 |
1 files changed, 61 insertions, 0 deletions
@@ -1125,6 +1125,67 @@ impl From<&RawPalette> for Palette } } +#[cfg(feature = "gui")] +impl From<&[slint::Color]> for Palette +{ + fn from(colors: &[slint::Color]) -> Self + { + let mut idx: usize = 0; + let mut pal: [u8; PALETTE_BYTES] = [0; PALETTE_BYTES]; + + for &col in colors.iter() { + pal[idx] = col.red(); + pal[idx + 1] = col.green(); + pal[idx + 2] = col.blue(); + idx += 3; + } + + Self(pal) + } +} + +/** Convert palette to the default text format so it can be parsed as a scheme. */ +impl Into<String> for Palette +{ + fn into(self) -> String + { + let mut acc = String::with_capacity(16 * 10); + for i in 0..PALETTE_SIZE { + let idx = i * 3; + let (r, g, b) = (self.0[idx], self.0[idx + 1], self.0[idx + 2]); + acc.push_str(&format!("{}#{:02.x}{:02.x}{:02.x}\n", i, r, g, b)); + } + acc + } +} + +#[test] +fn palette_dump_as_text() +{ + let pal = Palette::from(&SOLARIZED_COLORS_DARK); + let txt = indoc::indoc! { r#" + 0#002b36 + 1#dc322f + 2#859900 + 3#b58900 + 4#268bd2 + 5#d33682 + 6#2aa198 + 7#eee8d5 + 8#002b36 + 9#cb4b16 + 10#586e75 + 11#657b83 + 12#839496 + 13#6c71c4 + 14#93a1a1 + 15#fdf6e3 + "#}; + + let pal: String = pal.into(); + assert_eq!(pal, txt); +} + pub struct PaletteIterator { pal: Palette, |