Files
texturecraft/crates/engine/tests/export_colour_space.rs
SteamSmith 16e70a0145 feat(export): choose the colour space of exported colour maps
New texturecraft_texel::ColorSpace (sRGB, Linear Rec.709, ACEScg, ACES2065-1): the matrices are
derived at first use from the published primaries and white points (BT.709, ACES S-2014-004,
SMPTE ST 2065-1) with Bradford adaptation, so nothing is copied from a colour-management
library. ExportOptions::color_space (command and dialog: `colorSpace`) converts the colour maps
on their way out; data maps (normal, height, roughness...) are never converted.

None keeps today's behaviour: sRGB in 8/16-bit files, linear Rec.709 in float files. A linear
space in an integer file is written without the sRGB curve; ACEScg and ACES2065-1 in integer
files warn that values outside 0-1 are clamped (EXR and HDR keep them). `$colorSpace` in a file
name template now also says ACEScg / ACES2065-1. The export dialog gets a Color space combo
(label translated in all ten languages).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 17:28:09 -05:00

58 lines
2.8 KiB
Rust

//! `file.exportTextures` / `export.plan` with `colorSpace`.
use serde_json::{Value, json};
use texturecraft_engine::geom::primitives;
use texturecraft_engine::texel::ColorSpace;
use texturecraft_engine::{Session, execute};
fn session() -> Session {
let mut s = Session::new_project("t", primitives::builtin("plane").unwrap(), "builtin:plane", None, 128).unwrap();
execute(&mut s, "layer.add.fill", &json!({ "color": [0.8, 0.2, 0.1] })).unwrap();
s
}
fn export(s: &mut Session, tag: &str, extra: Value) -> (Value, std::path::PathBuf) {
let dir = std::env::temp_dir().join(format!("tc_export_cs_{tag}_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let mut p = json!({ "dir": dir.display().to_string(), "preset": "glTF PBR Metal Roughness", "format": "exr", "bitDepth": 32, "padding": "infinite" });
for (k, v) in extra.as_object().unwrap() {
p[k] = v.clone();
}
(execute(s, "file.exportTextures", &p).unwrap(), dir)
}
fn first_pixel(path: &std::path::Path) -> [f32; 3] {
let img = image::open(path).unwrap().to_rgb32f();
// The plane's UV layout covers the whole image; read its middle.
let px = img.get_pixel(img.width() / 2, img.height() / 2);
[px[0], px[1], px[2]]
}
#[test]
fn colour_maps_are_written_in_the_chosen_space() {
let mut s = session();
let (plain, dir_plain) = export(&mut s, "plain", json!({}));
let (aces, dir_aces) = export(&mut s, "aces", json!({ "colorSpace": "ACEScg" }));
let base =
|r: &Value| std::path::PathBuf::from(r["files"].as_array().unwrap().iter().map(|f| f.as_str().unwrap()).find(|f| f.contains("BaseColor")).unwrap());
let (a, b) = (first_pixel(&base(&plain)), first_pixel(&base(&aces)));
let want = ColorSpace::AcesCg.from_linear_rec709(a);
assert!((0..3).all(|k| (b[k] - want[k]).abs() < 1e-3), "{b:?} vs {want:?}");
assert!((0..3).any(|k| (a[k] - b[k]).abs() > 0.01), "the colour actually changed: {a:?} vs {b:?}");
for d in [dir_plain, dir_aces] {
let _ = std::fs::remove_dir_all(d);
}
}
#[test]
fn the_plan_reports_the_space_and_clamping_and_bad_names_are_errors() {
let mut s = session();
let plan = execute(&mut s, "export.plan", &json!({ "preset": "glTF PBR Metal Roughness", "colorSpace": "ACEScg" })).unwrap();
assert!(plan.as_array().unwrap().iter().any(|f| f["warnings"].as_array().is_some_and(|w| !w.is_empty())), "8-bit ACEScg warns: {plan}");
let float = execute(&mut s, "export.plan", &json!({ "preset": "glTF PBR Metal Roughness", "colorSpace": "ACEScg", "format": "exr" })).unwrap();
assert!(float.as_array().unwrap().iter().all(|f| f["warnings"].as_array().is_some_and(Vec::is_empty)), "{float}");
for bad in ["Display P3", "", "Rec.2020"] {
assert!(execute(&mut s, "export.plan", &json!({ "preset": "glTF PBR Metal Roughness", "colorSpace": bad })).is_err(), "{bad:?}");
}
}