Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

New Document: a custom background colour, from Custom… or the swatch beside the menu (#4789) (#4801)

F4LCON62bf7391ce2a
diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsv
index cea55a169..0f0c48b8d 100644
--- a/crates/ui-egui/src/i18n/cs.tsv
+++ b/crates/ui-egui/src/i18n/cs.tsv
@@ -885,6 +885,7 @@
Background Pozadí
Background Color Barva pozadí
Background Contents Obsah pozadí
+ Custom background color Vlastní barva pozadí
Balance Vyvážení
Baseline Účaří
Baseline shift Posun účaří
diff --git a/crates/ui-egui/src/i18n/de.tsv b/crates/ui-egui/src/i18n/de.tsv
index b23510e12..bce9bb32d 100644
--- a/crates/ui-egui/src/i18n/de.tsv
+++ b/crates/ui-egui/src/i18n/de.tsv
@@ -865,6 +865,7 @@
Background Hintergrund
Background Color Hintergrundfarbe
Background Contents Hintergrundinhalte
+ Custom background color Benutzerdefinierte Hintergrundfarbe
Balance Balance
Baseline Grundlinie
Baseline shift Grundlinienverschiebung
diff --git a/crates/ui-egui/src/i18n/el.tsv b/crates/ui-egui/src/i18n/el.tsv
index 1dd9ba365..7b72bd7eb 100644
--- a/crates/ui-egui/src/i18n/el.tsv
+++ b/crates/ui-egui/src/i18n/el.tsv
@@ -2085,6 +2085,7 @@
Avoid Widows/Orphans Αποφυγή μονών γραμμών στην αρχή ή στο τέλος σελίδας
BLANK DOCUMENT PRESETS ({n}) ΠΡΟΕΠΙΛΟΓΕΣ ΚΕΝΟΥ ΕΓΓΡΑΦΟΥ ({n})
Background Contents Περιεχόμενα φόντου
+ Custom background color Προσαρμοσμένο χρώμα φόντου
Background from Layer Φόντο από επίπεδο
Baseline Γραμμή βάσης
Baseline shift Μετατόπιση γραμμής βάσης
diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsv
index 5d1ff7114..8a68c5821 100644
--- a/crates/ui-egui/src/i18n/es.tsv
+++ b/crates/ui-egui/src/i18n/es.tsv
@@ -874,6 +874,7 @@
Background Fondo
Background Color Color de fondo
Background Contents Contenido del fondo
+ Custom background color Color de fondo personalizado
Balance Balance
Baseline Línea de base
Baseline shift Desplazamiento de línea de base
diff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsv
index 697461611..3aca15845 100644
--- a/crates/ui-egui/src/i18n/fr.tsv
+++ b/crates/ui-egui/src/i18n/fr.tsv
@@ -860,6 +860,7 @@
Background Arrière-plan
Background Color Couleur d'arrière-plan
Background Contents Contenu de l'arrière-plan
+ Custom background color Couleur d'arrière-plan personnalisée
Balance Balance
Baseline Ligne de base
Baseline shift Décalage de la ligne de base
diff --git a/crates/ui-egui/src/i18n/hu.tsv b/crates/ui-egui/src/i18n/hu.tsv
index 9aa29a7cb..f60b798d8 100644
--- a/crates/ui-egui/src/i18n/hu.tsv
+++ b/crates/ui-egui/src/i18n/hu.tsv
@@ -2644,6 +2644,7 @@ cameraRaw Yellow Sárga
Autosave minutes Automatikus mentés gyakorisága (perc)
Background Color Háttérszín
Background Contents Háttér tartalma
+ Custom background color Egyéni háttérszín
Batch Kötegelt feldolgozás
Binary assets added Hozzáadott bináris elemek
Binary assets removed Eltávolított bináris elemek
diff --git a/crates/ui-egui/src/i18n/id.tsv b/crates/ui-egui/src/i18n/id.tsv
index 8182765c4..b4289bc4a 100644
--- a/crates/ui-egui/src/i18n/id.tsv
+++ b/crates/ui-egui/src/i18n/id.tsv
@@ -854,6 +854,7 @@
Background Background
Background Color Warna Background
Background Contents Isi Background
+ Custom background color Warna background kustom
Balance Keseimbangan
Baseline Baseline
Baseline shift Geser baseline
diff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsv
index 08d8adcc1..2bd87d46c 100644
--- a/crates/ui-egui/src/i18n/it.tsv
+++ b/crates/ui-egui/src/i18n/it.tsv
@@ -869,6 +869,7 @@
Background Sfondo
Background Color Colore sfondo
Background Contents Contenuto sfondo
+ Custom background color Colore sfondo personalizzato
Balance Bilanciamento
Baseline Linea base
Baseline shift Scostamento linea base
diff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsv
index 8e878c2c2..4e45e1413 100644
--- a/crates/ui-egui/src/i18n/ja.tsv
+++ b/crates/ui-egui/src/i18n/ja.tsv
@@ -873,6 +873,7 @@
Background 背景
Background Color 背景色
Background Contents 背景の内容
+ Custom background color カスタム背景色
Balance バランス
Baseline ベースライン
Baseline shift ベースラインシフト
diff --git a/crates/ui-egui/src/i18n/ko.tsv b/crates/ui-egui/src/i18n/ko.tsv
index b7fe66cbb..ce1ded936 100644
--- a/crates/ui-egui/src/i18n/ko.tsv
+++ b/crates/ui-egui/src/i18n/ko.tsv
@@ -848,6 +848,7 @@
Background 배경
Background Color 배경색
Background Contents 배경 내용
+ Custom background color 사용자 정의 배경색
Balance 균형
Baseline 기준선
Baseline shift 기준선 이동
diff --git a/crates/ui-egui/src/i18n/nl.tsv b/crates/ui-egui/src/i18n/nl.tsv
index 9ffc6e89b..a32df3974 100644
--- a/crates/ui-egui/src/i18n/nl.tsv
+++ b/crates/ui-egui/src/i18n/nl.tsv
@@ -852,6 +852,7 @@
Background Achtergrond
Background Color Achtergrondkleur
Background Contents Achtergrondinhoud
+ Custom background color Aangepaste achtergrondkleur
Balance Balans
Baseline Basislijn
Baseline shift Basislijnverschuiving
diff --git a/crates/ui-egui/src/i18n/pl.tsv b/crates/ui-egui/src/i18n/pl.tsv
index 4601e9027..2689c3e03 100644
--- a/crates/ui-egui/src/i18n/pl.tsv
+++ b/crates/ui-egui/src/i18n/pl.tsv
@@ -871,6 +871,7 @@
Background Tło
Background Color Kolor tła
Background Contents Zawartość tła
+ Custom background color Niestandardowy kolor tła
Balance Balans
Baseline Linia bazowa
Baseline shift Przesunięcie linii bazowej
diff --git a/crates/ui-egui/src/i18n/pt-br.tsv b/crates/ui-egui/src/i18n/pt-br.tsv
index 21ce21746..2e1a7705f 100644
--- a/crates/ui-egui/src/i18n/pt-br.tsv
+++ b/crates/ui-egui/src/i18n/pt-br.tsv
@@ -854,6 +854,7 @@
Background Plano de fundo
Background Color Cor de fundo
Background Contents Conteúdo do plano de fundo
+ Custom background color Cor de fundo personalizada
Balance Equilíbrio
Baseline Linha de base
Baseline shift Deslocamento da linha de base
diff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsv
index cc008ef42..3cf585151 100644
--- a/crates/ui-egui/src/i18n/ru.tsv
+++ b/crates/ui-egui/src/i18n/ru.tsv
@@ -1527,6 +1527,7 @@
Preset Name: Имя набора:
Width and Height Ширина и высота
Background Contents Содержимое фона
+ Custom background color Свой цвет фона
White Белый
Background Color Цвет фона
Transparent Прозрачный
diff --git a/crates/ui-egui/src/i18n/sr.tsv b/crates/ui-egui/src/i18n/sr.tsv
index 7a85f8d58..72c6b4bee 100644
--- a/crates/ui-egui/src/i18n/sr.tsv
+++ b/crates/ui-egui/src/i18n/sr.tsv
@@ -850,6 +850,7 @@
Background Pozadina
Background Color Boja pozadine
Background Contents Sadržaj pozadine
+ Custom background color Prilagođena boja pozadine
Balance Balans
Baseline Osnovna linija
Baseline shift Pomak osnovne linije
diff --git a/crates/ui-egui/src/i18n/tr.tsv b/crates/ui-egui/src/i18n/tr.tsv
index 12383b694..23986a8c5 100644
--- a/crates/ui-egui/src/i18n/tr.tsv
+++ b/crates/ui-egui/src/i18n/tr.tsv
@@ -841,6 +841,7 @@
Background Arka plan
Background Color Arka Plan Rengi
Background Contents Arka Plan İçeriği
+ Custom background color Özel Arka Plan Rengi
Balance Bakiye
Baseline Temel
Baseline shift Taban çizgisi kayması
diff --git a/crates/ui-egui/src/i18n/uk.tsv b/crates/ui-egui/src/i18n/uk.tsv
index a9bae9eb2..2187e4d7f 100644
--- a/crates/ui-egui/src/i18n/uk.tsv
+++ b/crates/ui-egui/src/i18n/uk.tsv
@@ -1508,6 +1508,7 @@
Preset Name: Назва набору:
Width and Height Ширина та висота
Background Contents Вміст фону
+ Custom background color Довільний колір фону
White Білий
Background Color Колір фону
Transparent Прозорий
diff --git a/crates/ui-egui/src/i18n/vi.tsv b/crates/ui-egui/src/i18n/vi.tsv
index fc652eff8..e56eaf305 100644
--- a/crates/ui-egui/src/i18n/vi.tsv
+++ b/crates/ui-egui/src/i18n/vi.tsv
@@ -1731,6 +1731,7 @@ cameraRaw Size Kích thước
Resolution: Độ phân giải:
Color Mode Chế độ màu
Background Contents Nội dung nền
+ Custom background color Màu nền tùy chỉnh
Pixels/Inch Pixel/inch
Pixels/Centimeter Pixel/centimet
White Trắng
diff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsv
index fb6083985..14ee54ff2 100644
--- a/crates/ui-egui/src/i18n/zh-hans.tsv
+++ b/crates/ui-egui/src/i18n/zh-hans.tsv
@@ -877,6 +877,7 @@
Background 背景
Background Color 背景颜色
Background Contents 背景内容
+ Custom background color 自定背景颜色
Balance 平衡
Baseline 基线
Baseline shift 基线偏移
diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsv
index 760e39081..7f26404e6 100644
--- a/crates/ui-egui/src/i18n/zh-hant.tsv
+++ b/crates/ui-egui/src/i18n/zh-hant.tsv
@@ -880,6 +880,7 @@
Background 背景
Background Color 背景色
Background Contents 背景內容
+ Custom background color 自訂背景色
Balance 平衡
Baseline 基線
Baseline shift 基線位移
diff --git a/crates/ui-egui/src/new_doc_ui.rs b/crates/ui-egui/src/new_doc_ui.rs
index 0d2790883..c9bf902c6 100644
--- a/crates/ui-egui/src/new_doc_ui.rs
+++ b/crates/ui-egui/src/new_doc_ui.rs
@@ -285,6 +285,31 @@ fn page_icon(ui: &egui::Ui, r: Rect, w: u32, h: u32, t: &Tokens) {
ui.painter().rect_stroke(page, 1.0, Stroke::new(1.2, t.text_dim), StrokeKind::Inside);
}
+/// The Background Contents menu's entry for a colour of the user's own ([`body`]).
+const CUSTOM: &str = "custom";
+/// Side of the Background Contents colour swatch.
+const SWATCH: f32 = 22.0;
+
+/// The colour the new document's background starts with: white, black, the captured or current
+/// toolbox background colour, or a custom `"#rrggbb"`; `None` for Transparent.
+fn background_rgb(app: &crate::PhotocraftApp, f: &Map<String, Value>) -> Option<[f32; 3]> {
+ let bg = get_s(f, "background", "white");
+ match bg.as_str() {
+ "transparent" => None,
+ "black" => Some([0.0; 3]),
+ "backgroundColor" => Some(
+ f.get("backgroundColor")
+ .and_then(Value::as_array)
+ .and_then(|a| Some([a.first()?.as_f64()? as f32, a.get(1)?.as_f64()? as f32, a.get(2)?.as_f64()? as f32]))
+ .unwrap_or_else(|| {
+ let b = app.session.tools.background;
+ [b[0], b[1], b[2]]
+ }),
+ ),
+ hex => Some(crate::color_picker_ui::parse_hex(hex).unwrap_or([1.0; 3])),
+ }
+}
+
/// Restore a snapshot without replacing the new document's name or its clipboard offer.
pub fn apply_saved_preset(f: &mut Map<String, Value>, preset: &photocraft_engine::document_preset_cmds::DocumentPreset) {
apply_settings(f, &preset.settings);
@@ -681,19 +706,57 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strin
});
ui.add_space(4.0);
small_label(ui, tl!("Background Contents"));
- let mut bg = get_s(f, "background", "white");
+ // A custom colour is kept as its "#rrggbb", which `file.new` takes as is (#4789).
+ let current = get_s(f, "background", "white");
+ let mut bg = if crate::color_picker_ui::parse_hex(&current).is_some() { CUSTOM.to_string() } else { current.clone() };
let opts = [
("white".to_string(), tl!("White")),
("black".to_string(), tl!("Black")),
("backgroundColor".to_string(), tl!("Background Color")),
("transparent".to_string(), tl!("Transparent")),
+ (CUSTOM.to_string(), tl!("Custom…")),
];
- if widgets::dropdown(ui, "nd-bg", &mut bg, &opts, 240.0) {
- f.insert("background".into(), json!(bg));
- f.remove("__savedPreset");
- // A fresh choice of Background Color uses today's toolbox colour. Selecting a
- // saved preset instead restores the colour captured when it was saved.
+ let shown = background_rgb(app, f);
+ let mut pick = false;
+ ui.horizontal(|ui| {
+ if widgets::dropdown(ui, "nd-bg", &mut bg, &opts, 240.0 - SWATCH - 6.0) {
+ if bg == CUSTOM {
+ // Custom… opens the Color Picker on the colour shown so far.
+ pick = true;
+ } else {
+ f.insert("background".into(), json!(bg));
+ }
+ f.remove("__savedPreset");
+ // A fresh choice of Background Color uses today's toolbox colour. Selecting a
+ // saved preset instead restores the colour captured when it was saved.
+ f.remove("backgroundColor");
+ }
+ // Photoshop's swatch beside the menu: the colour the canvas starts with; a click
+ // opens the Color Picker, as Custom… does.
+ let (r, resp) = ui.allocate_exact_size(vec2(SWATCH, SWATCH), Sense::click());
+ match shown {
+ Some(c) => {
+ ui.painter().rect_filled(
+ r,
+ 2.0,
+ egui::Color32::from_rgb((c[0] * 255.0).round() as u8, (c[1] * 255.0).round() as u8, (c[2] * 255.0).round() as u8),
+ );
+ }
+ None => widgets::checker(ui.painter(), r, 4.0),
+ }
+ ui.painter().rect_stroke(r, 2.0, Stroke::new(1.0, Tokens::get(ui.ctx()).field_border), StrokeKind::Inside);
+ let resp = resp.on_hover_text(tl!("Custom background color")).on_hover_cursor(egui::CursorIcon::PointingHand);
+ resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, tl!("Custom background color")));
+ if resp.clicked() {
+ pick = true;
+ f.remove("__savedPreset");
+ }
+ });
+ if pick {
+ let start = shown.unwrap_or([1.0; 3]);
+ f.insert("background".into(), json!(crate::color_picker_ui::hex(start)));
f.remove("backgroundColor");
+ crate::color_picker_ui::request_field(f, "background", &crate::color_picker_ui::title_for(tl!("Background Contents")));
}
save_preset(app, ui, f);
});
@@ -977,6 +1040,48 @@ mod tests {
h
}
+ /// #4789: the swatch beside Background Contents (and Custom…) opens the Color Picker; its
+ /// colour becomes the new document's background (kept as its "#rrggbb").
+ #[test]
+ fn a_custom_background_color_is_picked_and_used() {
+ let mut h = harness_with_store(&Arc::new(Mutex::new(None)));
+ let nd = h.state().ui.dialogs[0].id;
+ assert!(h.query_by_label("Custom background color").is_some());
+ h.get_by_label("Custom background color").click();
+ h.run_steps(3);
+ let picker = h
+ .state()
+ .ui
+ .dialogs
+ .iter()
+ .find(|d| d.fields.get("__dialog").and_then(serde_json::Value::as_u64) == Some(nd))
+ .map(|d| d.id)
+ .expect("the Color Picker opens over the dialog");
+ h.state_mut().ui.dialog_mut(picker).unwrap().fields.insert("color".into(), serde_json::json!("#336699"));
+ crate::dialogs::confirm(h.state_mut(), picker).unwrap();
+ h.run_steps(3);
+ let bg = h.state().ui.dialogs.iter().find(|d| d.id == nd).unwrap().fields["background"].clone();
+ assert_eq!(bg, "#336699");
+ crate::dialogs::confirm(h.state_mut(), nd).unwrap();
+ let doc = &h.state().session.active().unwrap().doc;
+ let px = photocraft_compose::flatten(doc).px[0];
+ assert!((px[0] * 255.0 - 51.0).abs() < 1.0 && (px[1] * 255.0 - 102.0).abs() < 1.0 && (px[2] * 255.0 - 153.0).abs() < 1.0, "{px:?}");
+ }
+
+ /// #4789: the swatch shows what each Background Contents choice paints, Background Color included.
+ #[test]
+ fn the_swatch_shows_each_background_choice() {
+ let mut h = harness_with_store(&Arc::new(Mutex::new(None)));
+ h.state_mut().run("tools.setColors", serde_json::json!({"foreground": "#000000", "background": "#336699"})).unwrap();
+ let rgb = |bg: &str| super::super::background_rgb(h.state(), serde_json::json!({ "background": bg }).as_object().unwrap());
+ assert_eq!(rgb("white"), Some([1.0; 3]));
+ assert_eq!(rgb("black"), Some([0.0; 3]));
+ assert_eq!(rgb("transparent"), None);
+ assert_eq!(rgb("#ff0000"), Some([1.0, 0.0, 0.0]));
+ let c = rgb("backgroundColor").expect("Background Color shows the toolbox colour");
+ assert!((c[0] * 255.0 - 51.0).abs() < 1.0 && (c[1] * 255.0 - 102.0).abs() < 1.0 && (c[2] * 255.0 - 153.0).abs() < 1.0, "{c:?}");
+ }
+
fn name_preset(h: &mut Harness<'static, PhotocraftApp>, name: &str) {
h.get_by_label("Save Preset…").click();
h.run_steps(2);