summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2022-08-27 21:45:19 +0200
committerPhilipp Gesang <phg@phi-gamma.net>2022-08-27 22:40:44 +0200
commitddeb59a89092b0bd7e7b77e3c71dc9bace1e23e5 (patch)
tree17790540394c40511be8911ba62281a00f75d77f /src/lib.rs
parentc4473f41f22d1e9de1b38e2cb6700410d71e3712 (diff)
downloadvtcol-ddeb59a89092b0bd7e7b77e3c71dc9bace1e23e5.tar.gz
edit: implement the actual save operation
We save palettes in the format that can be directly re-parsed by vtcol.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 021f25a..8ecc4a6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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,