fix: preserve palette color conversion and localization keys

This commit is contained in:
Codex authored and Dylan M. Taylor committed 2026-11-19 13:53:34 -05:00
1 parent 7d44f644e8
commit 75813b3be8
2 files changed
+81 -12

No files matched your search

+32 -12
View File
@@ -110,9 +110,22 @@ fn color_from_cms(c: PaletteColor) -> Color {
}
}
fn swatches_to_cms(list: &[Swatch]) -> Vec<PaletteSwatch> {
// GPL must retain the app-managed display conversion; ASE preserves authoring models.
fn swatches_to_cms(list: &[Swatch], rgb_only: bool) -> Vec<PaletteSwatch> {
list.iter()
.filter_map(|w| w.paint.color().map(|c| PaletteSwatch { name: w.name.clone(), color: color_to_cms(c), global: w.global, spot: w.spot }))
.filter_map(|w| {
w.paint.color().map(|c| PaletteSwatch {
name: w.name.clone(),
color: if rgb_only {
let [r, g, b, _] = c.to_rgba8(1.0);
PaletteColor::rgb8(r, g, b)
} else {
color_to_cms(c)
},
global: w.global,
spot: w.spot,
})
})
.collect()
}
@@ -120,11 +133,11 @@ fn swatches_from_cms(list: Vec<PaletteSwatch>) -> Vec<Swatch> {
list.into_iter().map(|w| Swatch { name: w.name, paint: Paint::solid(color_from_cms(w.color)), global: w.global, spot: w.spot }).collect()
}
fn to_cms_palette(lib: &SwatchLibrary) -> Palette {
fn to_cms_palette(lib: &SwatchLibrary, rgb_only: bool) -> Palette {
Palette {
name: lib.name.clone(),
swatches: swatches_to_cms(&lib.swatches),
groups: lib.groups.iter().map(|g| PaletteGroup { name: g.name.clone(), swatches: swatches_to_cms(&g.swatches) }).collect(),
swatches: swatches_to_cms(&lib.swatches, rgb_only),
groups: lib.groups.iter().map(|g| PaletteGroup { name: g.name.clone(), swatches: swatches_to_cms(&g.swatches, rgb_only) }).collect(),
}
}
@@ -142,17 +155,20 @@ fn palette_error(err: PaletteError) -> String {
PaletteError::UnsupportedFormat => "not a supported palette (.gpl, .ase, or .acb)".into(),
PaletteError::TruncatedAse => "the swatch exchange file is cut short".into(),
PaletteError::TruncatedAcb => "the color book is cut short".into(),
PaletteError::AseWriteEntryLimit { limit, entries } => {
format!("VectorCraft reads at most {limit} colors and groups from a swatch exchange file, and this library has {entries}")
PaletteError::AseWriteEntryLimit { limit, entries } => format!(
"VectorCraft reads at most {ASE_MAX_ENTRIES} colors and groups from a swatch exchange file, and this library has {entries}",
ASE_MAX_ENTRIES = limit
),
PaletteError::AseReadEntryLimit { limit } => {
format!("the swatch exchange file holds more than {ASE_MAX_ENTRIES} colors and groups", ASE_MAX_ENTRIES = limit)
}
PaletteError::AseReadEntryLimit { limit } => format!("the swatch exchange file holds more than {limit} colors and groups"),
PaletteError::AseBlockCount => "too many swatch exchange blocks".into(),
PaletteError::AseBlockLength => "a swatch exchange block is too long".into(),
PaletteError::AseNameLength => "a swatch exchange name is too long".into(),
PaletteError::AseVersion { major, minor } => format!("swatch exchange version {major}.{minor} isn't supported (only 1.x)"),
PaletteError::AcbVersion { version } => format!("color book version {version} isn't supported (only 1)"),
PaletteError::AcbEntryLimit { limit } => format!("the color book holds more than {limit} colors"),
PaletteError::AcbColorSpace { space } => format!("color books in color space {space} aren't supported (only RGB, CMYK and Lab)"),
PaletteError::AcbEntryLimit { limit } => format!("the color book holds more than {ASE_MAX_ENTRIES} colors", ASE_MAX_ENTRIES = limit),
PaletteError::AcbColorSpace { space: other } => format!("color books in color space {other} aren't supported (only RGB, CMYK and Lab)"),
PaletteError::AcbNameBytes { limit } => format!("the color book's names take more than {} MB", limit >> 20),
}
}
@@ -174,8 +190,12 @@ pub fn write(lib: &SwatchLibrary, format: PaletteFormat) -> Result<Vec<u8>, Stri
let file = NativeFile { format: NATIVE_FORMAT.into(), version: 1, library };
serde_json::to_string_pretty(&file).unwrap_or_default()
}
PaletteFormat::Gpl => return cms_palette::write_palette_typed(&to_cms_palette(lib), cms_palette::PaletteFormat::Gpl).map_err(palette_error),
PaletteFormat::Ase => return cms_palette::write_palette_typed(&to_cms_palette(lib), cms_palette::PaletteFormat::Ase).map_err(palette_error),
PaletteFormat::Gpl => {
return cms_palette::write_palette_typed(&to_cms_palette(lib, true), cms_palette::PaletteFormat::Gpl).map_err(palette_error);
}
PaletteFormat::Ase => {
return cms_palette::write_palette_typed(&to_cms_palette(lib, false), cms_palette::PaletteFormat::Ase).map_err(palette_error);
}
PaletteFormat::Css => write_css(lib, keep),
};
Ok(text.into_bytes())
+49
View File
@@ -703,3 +703,52 @@ proptest! {
}
}
}
#[test]
fn shared_gpl_adapter_uses_app_color_conversion() {
let colors = [Color::cmyk(0.2, 0.4, 0.6, 0.1), Color::lab(55.0, 30.0, -25.0), Color::gray(0.3)];
let lib = SwatchLibrary {
name: "Managed".into(),
swatches: colors
.iter()
.enumerate()
.map(|(i, &color)| Swatch { name: format!("Color {i}"), paint: Paint::solid(color), global: false, spot: false })
.collect(),
groups: vec![],
};
let mut expected = String::from("GIMP Palette\nName: Managed\nColumns: 0\n#\n");
for (i, color) in colors.iter().enumerate() {
let [r, g, b, _] = color.to_rgba8(1.0);
expected.push_str(&format!("{r:3} {g:3} {b:3}\tColor {i}\n"));
}
assert_eq!(write(&lib, PaletteFormat::Gpl).unwrap(), expected.as_bytes());
assert!(to_cms_palette(&lib, true).swatches.iter().all(|w| matches!(w.color, PaletteColor::Rgb { .. })));
assert!(matches!(to_cms_palette(&lib, false).swatches[0].color, PaletteColor::Cmyk { .. }));
assert!(matches!(to_cms_palette(&lib, false).swatches[1].color, PaletteColor::Lab { .. }));
}
#[test]
fn typed_palette_errors_keep_app_wording() {
let cases = [
(PaletteError::UnsupportedFormat, "not a supported palette (.gpl, .ase, or .acb)"),
(PaletteError::TruncatedAse, "the swatch exchange file is cut short"),
(PaletteError::TruncatedAcb, "the color book is cut short"),
(
PaletteError::AseWriteEntryLimit { limit: 3, entries: 4 },
"VectorCraft reads at most 3 colors and groups from a swatch exchange file, and this library has 4",
),
(PaletteError::AseReadEntryLimit { limit: 3 }, "the swatch exchange file holds more than 3 colors and groups"),
(PaletteError::AseBlockCount, "too many swatch exchange blocks"),
(PaletteError::AseBlockLength, "a swatch exchange block is too long"),
(PaletteError::AseNameLength, "a swatch exchange name is too long"),
(PaletteError::AseVersion { major: 2, minor: 3 }, "swatch exchange version 2.3 isn't supported (only 1.x)"),
(PaletteError::AcbVersion { version: 2 }, "color book version 2 isn't supported (only 1)"),
(PaletteError::AcbEntryLimit { limit: 3 }, "the color book holds more than 3 colors"),
(PaletteError::AcbColorSpace { space: 5 }, "color books in color space 5 aren't supported (only RGB, CMYK and Lab)"),
(PaletteError::AcbNameBytes { limit: 64 << 20 }, "the color book's names take more than 64 MB"),
];
for (error, expected) in cases {
assert_eq!(palette_error(error), expected);
}
assert_eq!(read_bytes(b"8BCB\0\x01\0\0\0\0\0\x03\0x", "fallback").unwrap_err(), "the color book is cut short");
}