mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-25 08:50:46 +01:00
Color picker: our own picker with a copy icon from the icon set
The picker's copy button was an emoji the bundled fonts don't draw, and it lives inside egui's picker. The swatch now opens a compact picker of its own: R, G and B values with a Phosphor copy button, a saturation/value square and a hue strip. Hue and saturation survive black, white and grays. The copy tooltip is translated into all nine other UI languages.
This commit is contained in:
1 parent
56c52edf4d
commit
5e37a93e45
11 files changed
+136
-16
No files matched your search
@@ -186,6 +186,7 @@ pub const TABLE: &[(&str, &str)] = &[
|
||||
("Contrast", "التباين"),
|
||||
("Contrast/Luminosity", "التباين / الإضاءة"),
|
||||
("Converted maps", "الخرائط المحوّلة"),
|
||||
("Copy RGB values", "نسخ قيم RGB"),
|
||||
("Copy from", "نسخ من"),
|
||||
("Copy layer(s)", "نسخ الطبقة (الطبقات)"),
|
||||
("Copy the selected layer alone (its empty parts copy nothing)", "نسخ الطبقة المحددة وحدها (الأجزاء الفارغة لا تنسخ شيئاً)"),
|
||||
|
||||
@@ -198,6 +198,7 @@ pub const TABLE: &[(&str, &str)] = &[
|
||||
("Contrast", "Kontrast"),
|
||||
("Contrast/Luminosity", "Kontrast/Luminanz"),
|
||||
("Converted maps", "Konvertierte Maps"),
|
||||
("Copy RGB values", "RGB-Werte kopieren"),
|
||||
("Copy from", "Kopieren aus"),
|
||||
("Copy layer(s)", "Ebene(n) kopieren"),
|
||||
("Copy the selected layer alone (its empty parts copy nothing)", "Nur die ausgewählte Ebene kopieren (ihre leeren Bereiche kopieren nichts)"),
|
||||
|
||||
@@ -195,6 +195,7 @@ pub const TABLE: &[(&str, &str)] = &[
|
||||
("Contrast", "Contraste"),
|
||||
("Contrast/Luminosity", "Contraste/Luminosidad"),
|
||||
("Converted maps", "Mapas convertidos"),
|
||||
("Copy RGB values", "Copiar los valores RGB"),
|
||||
("Copy from", "Copiar de"),
|
||||
("Copy layer(s)", "Copiar capa(s)"),
|
||||
("Copy the selected layer alone (its empty parts copy nothing)", "Copiar solo la capa seleccionada (sus partes vacías no copian nada)"),
|
||||
|
||||
@@ -198,6 +198,7 @@ pub const TABLE: &[(&str, &str)] = &[
|
||||
("Contrast", "Contraste"),
|
||||
("Contrast/Luminosity", "Contraste/Luminosité"),
|
||||
("Converted maps", "Cartes converties"),
|
||||
("Copy RGB values", "Copier les valeurs RVB"),
|
||||
("Copy from", "Copier depuis"),
|
||||
("Copy layer(s)", "Copier le(s) calque(s)"),
|
||||
("Copy the selected layer alone (its empty parts copy nothing)", "Copier uniquement le calque sélectionné (ses parties vides ne copient rien)"),
|
||||
|
||||
@@ -201,6 +201,7 @@ pub const TABLE: &[(&str, &str)] = &[
|
||||
("Contrast", "Contrasto"),
|
||||
("Contrast/Luminosity", "Contrasto/Luminosità"),
|
||||
("Converted maps", "Mappe convertite"),
|
||||
("Copy RGB values", "Copia i valori RGB"),
|
||||
("Copy from", "Copia da"),
|
||||
("Copy layer(s)", "Copia livello/i"),
|
||||
("Copy the selected layer alone (its empty parts copy nothing)", "Copia solo il livello selezionato (le sue parti vuote non copiano nulla)"),
|
||||
|
||||
@@ -198,6 +198,7 @@ pub const TABLE: &[(&str, &str)] = &[
|
||||
("Contrast", "コントラスト"),
|
||||
("Contrast/Luminosity", "コントラスト/輝度"),
|
||||
("Converted maps", "変換マップ"),
|
||||
("Copy RGB values", "RGB 値をコピー"),
|
||||
("Copy from", "コピー元"),
|
||||
("Copy layer(s)", "レイヤーをコピー"),
|
||||
("Copy the selected layer alone (its empty parts copy nothing)", "選択中のレイヤーのみをコピー (空の部分は何もコピーしない)"),
|
||||
|
||||
@@ -189,6 +189,7 @@ pub const TABLE: &[(&str, &str)] = &[
|
||||
("Contrast", "대비"),
|
||||
("Contrast/Luminosity", "대비/광도"),
|
||||
("Converted maps", "변환된 맵"),
|
||||
("Copy RGB values", "RGB 값 복사"),
|
||||
("Copy from", "복사 원본"),
|
||||
("Copy layer(s)", "레이어 복사"),
|
||||
("Copy the selected layer alone (its empty parts copy nothing)", "선택한 레이어만 복사합니다 (빈 부분은 복사되지 않음)"),
|
||||
|
||||
@@ -195,6 +195,7 @@ pub const TABLE: &[(&str, &str)] = &[
|
||||
("Contrast", "Contraste"),
|
||||
("Contrast/Luminosity", "Contraste/Luminosidade"),
|
||||
("Converted maps", "Mapas convertidos"),
|
||||
("Copy RGB values", "Copiar os valores RGB"),
|
||||
("Copy from", "Copiar de"),
|
||||
("Copy layer(s)", "Copiar camada(s)"),
|
||||
("Copy the selected layer alone (its empty parts copy nothing)", "Copiar apenas a camada selecionada (suas partes vazias não copiam nada)"),
|
||||
|
||||
@@ -168,6 +168,7 @@ pub const TABLE: &[(&str, &str)] = &[
|
||||
("Contrast", "对比度"),
|
||||
("Contrast/Luminosity", "对比度/亮度"),
|
||||
("Converted maps", "转换贴图"),
|
||||
("Copy RGB values", "复制 RGB 值"),
|
||||
("Copy from", "复制自"),
|
||||
("Copy layer(s)", "复制图层"),
|
||||
("Copy the selected layer alone (its empty parts copy nothing)", "仅复制所选图层(其空白部分不复制任何内容)"),
|
||||
|
||||
@@ -209,8 +209,7 @@ pub fn color_swatch(ui: &mut Ui, id: &str, linear: &mut [f32; 3], width: f32) ->
|
||||
// Open state lives in memory (`toggle_id` above); `from_response` alone shows it always.
|
||||
egui::Popup::from_response(&resp).id(popup).open_memory(None).close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside).show(|ui| {
|
||||
// Wider than the R, G, B row at any value, so the popup keeps one size and place.
|
||||
ui.spacing_mut().slider_width = 275.0;
|
||||
if egui::color_picker::color_picker_color32(ui, &mut c, egui::color_picker::Alpha::Opaque) {
|
||||
if color_picker(ui, popup.with("picker"), &mut c, 275.0) {
|
||||
let lin = [c.r(), c.g(), c.b()].map(|v| texturecraft_engine::texel::srgb_to_linear(v as f32 / 255.0));
|
||||
*linear = lin;
|
||||
changed = true;
|
||||
@@ -219,6 +218,80 @@ pub fn color_swatch(ui: &mut Ui, id: &str, linear: &mut [f32; 3], width: f32) ->
|
||||
changed
|
||||
}
|
||||
|
||||
/// A color picker `width` wide: R, G, B (sRGB 0–255) with a copy button, a saturation/value
|
||||
/// square and a hue strip. Returns true when the color changed.
|
||||
fn color_picker(ui: &mut Ui, id: egui::Id, c: &mut Color32, width: f32) -> bool {
|
||||
use egui::ecolor::{hsv_from_rgb, rgb_from_hsv};
|
||||
let tk = Tokens::get(ui.ctx());
|
||||
let mut srgb = [c.r(), c.g(), c.b()];
|
||||
let hsv_of = |srgb: [u8; 3]| hsv_from_rgb(srgb.map(|v| v as f32 / 255.0));
|
||||
// Remembered with the color it was for, so hue and saturation survive black, white and grays.
|
||||
let (mut h, mut s, mut v) = ui.data(|d| d.get_temp::<(Color32, (f32, f32, f32))>(id)).filter(|(k, _)| k == c).map_or_else(|| hsv_of(srgb), |(_, hsv)| hsv);
|
||||
let mut typed = false;
|
||||
let mut dragged = false;
|
||||
ui.horizontal(|ui| {
|
||||
for (x, prefix) in srgb.iter_mut().zip(["R ", "G ", "B "]) {
|
||||
typed |= ui.add(egui::DragValue::new(x).speed(0.5).prefix(prefix)).changed();
|
||||
}
|
||||
if icon_button(ui, egui_phosphor::regular::COPY, crate::i18n::tr("Copy RGB values"), false, 20.0).clicked() {
|
||||
ui.copy_text(format!("{}, {}, {}", srgb[0], srgb[1], srgb[2]));
|
||||
}
|
||||
});
|
||||
if typed {
|
||||
(h, s, v) = hsv_of(srgb);
|
||||
}
|
||||
let border = Stroke::new(1.0, tk.field_border);
|
||||
// Saturation across, value up.
|
||||
let (rect, resp) = ui.allocate_exact_size(vec2(width, width), Sense::click_and_drag());
|
||||
if let Some(p) = resp.interact_pointer_pos().filter(|_| resp.dragged() || resp.clicked()) {
|
||||
(s, v) = (((p.x - rect.left()) / rect.width()).clamp(0.0, 1.0), 1.0 - ((p.y - rect.top()) / rect.height()).clamp(0.0, 1.0));
|
||||
dragged = true;
|
||||
}
|
||||
gradient(ui.painter(), rect, |x, y| rgb_from_hsv((h, x, 1.0 - y)));
|
||||
ui.painter().rect_stroke(rect, 0.0, border, egui::StrokeKind::Inside);
|
||||
let at = rect.lerp_inside(vec2(s, 1.0 - v));
|
||||
ui.painter().circle_stroke(at, 5.0, Stroke::new(2.0, Color32::WHITE));
|
||||
ui.painter().circle_stroke(at, 6.5, Stroke::new(1.0, Color32::BLACK));
|
||||
let (rect, resp) = ui.allocate_exact_size(vec2(width, 16.0), Sense::click_and_drag());
|
||||
if let Some(p) = resp.interact_pointer_pos().filter(|_| resp.dragged() || resp.clicked()) {
|
||||
h = ((p.x - rect.left()) / rect.width()).clamp(0.0, 1.0);
|
||||
dragged = true;
|
||||
}
|
||||
gradient(ui.painter(), rect, |x, _| rgb_from_hsv((x, 1.0, 1.0)));
|
||||
ui.painter().rect_stroke(rect, 0.0, border, egui::StrokeKind::Inside);
|
||||
let x = rect.left() + rect.width() * h;
|
||||
ui.painter().rect_stroke(egui::Rect::from_x_y_ranges(x - 2.0..=x + 2.0, rect.y_range()), 1.0, Stroke::new(1.5, Color32::WHITE), egui::StrokeKind::Middle);
|
||||
if dragged {
|
||||
srgb = rgb_from_hsv((h, s, v)).map(|x| (x * 255.0 + 0.5) as u8);
|
||||
}
|
||||
let next = Color32::from_rgb(srgb[0], srgb[1], srgb[2]);
|
||||
ui.data_mut(|d| d.insert_temp(id, (next, (h, s, v))));
|
||||
let changed = next != *c;
|
||||
*c = next;
|
||||
changed
|
||||
}
|
||||
|
||||
/// Fill `rect` with `rgb(x, y)` (sRGB 0..1 over 0..1 across and down), as a shaded mesh.
|
||||
fn gradient(painter: &egui::Painter, rect: egui::Rect, rgb: impl Fn(f32, f32) -> [f32; 3]) {
|
||||
const N: u32 = 24;
|
||||
let mut mesh = egui::Mesh::default();
|
||||
for j in 0..=N {
|
||||
for i in 0..=N {
|
||||
let (x, y) = (i as f32 / N as f32, j as f32 / N as f32);
|
||||
let [r, g, b] = rgb(x, y).map(|c| (c.clamp(0.0, 1.0) * 255.0 + 0.5) as u8);
|
||||
mesh.colored_vertex(rect.lerp_inside(vec2(x, y)), Color32::from_rgb(r, g, b));
|
||||
}
|
||||
}
|
||||
for j in 0..N {
|
||||
for i in 0..N {
|
||||
let k = j * (N + 1) + i;
|
||||
mesh.add_triangle(k, k + 1, k + N + 1);
|
||||
mesh.add_triangle(k + 1, k + N + 2, k + N + 1);
|
||||
}
|
||||
}
|
||||
painter.add(mesh);
|
||||
}
|
||||
|
||||
/// Panel title row with optional trailing content.
|
||||
pub fn panel_header(ui: &mut Ui, title: &str, trailing: impl FnOnce(&mut Ui)) {
|
||||
ui.horizontal(|ui| {
|
||||
|
||||
@@ -1,35 +1,73 @@
|
||||
//! The color picker popup keeps its place while the color changes.
|
||||
//! The color picker popup keeps its place while the color changes, picks from its square and hue
|
||||
//! strip, and copies its values.
|
||||
|
||||
use egui::accesskit::Role;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use texturecraft_ui_egui::{theme, widgets};
|
||||
|
||||
/// The left edge of the picker's R value.
|
||||
fn r_left(h: &Harness<'_, [f32; 3]>) -> f32 {
|
||||
h.get_all_by_role(Role::SpinButton).next().unwrap().rect().left()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_popup_stays_put_while_the_color_changes() {
|
||||
// A swatch at the right edge of the window, like the Properties panel's.
|
||||
// The picker's copy button is an emoji the bundled fonts do not draw.
|
||||
let mut h = Harness::builder().with_size(egui::vec2(800.0, 600.0)).allow_missing_glyphs().build_ui_state(
|
||||
/// A swatch at the right edge of the window, like the Properties panel's, with its picker open.
|
||||
fn open_picker(color: [f32; 3]) -> Harness<'static, [f32; 3]> {
|
||||
let mut h = Harness::builder().with_size(egui::vec2(800.0, 600.0)).build_ui_state(
|
||||
|ui, color: &mut [f32; 3]| {
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| {
|
||||
widgets::color_swatch(ui, "color", color, 120.0);
|
||||
});
|
||||
},
|
||||
[1.0; 3],
|
||||
color,
|
||||
);
|
||||
h.ctx.set_fonts(theme::font_definitions());
|
||||
theme::apply(&h.ctx, theme::Brightness::default(), theme::Accent::default());
|
||||
h.run_steps(2);
|
||||
let swatch = egui::pos2(800.0 - 60.0, 9.0);
|
||||
h.drag_at(swatch);
|
||||
h.drop_at(swatch);
|
||||
h.run_steps(3);
|
||||
let bright = r_left(&h);
|
||||
h
|
||||
}
|
||||
|
||||
/// The picker's R value.
|
||||
fn r_value(h: &Harness<'_, [f32; 3]>) -> egui::Rect {
|
||||
h.get_all_by_role(Role::SpinButton).next().unwrap().rect()
|
||||
}
|
||||
|
||||
/// Click at `p` and let the picker apply it.
|
||||
fn click(h: &mut Harness<'_, [f32; 3]>, p: egui::Pos2) {
|
||||
h.drag_at(p);
|
||||
h.drop_at(p);
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_popup_stays_put_while_the_color_changes() {
|
||||
let mut h = open_picker([1.0; 3]);
|
||||
let bright = r_value(&h).left();
|
||||
*h.state_mut() = [0.001; 3];
|
||||
h.run_steps(3);
|
||||
assert_eq!(r_left(&h), bright, "the picker does not move when R, G and B lose digits");
|
||||
assert_eq!(r_value(&h).left(), bright, "the picker does not move when R, G and B lose digits");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_square_and_the_hue_strip_pick_the_color() {
|
||||
let mut h = open_picker([1.0; 3]);
|
||||
// The square starts a row below the values; its bottom-left corner is black.
|
||||
let r = r_value(&h);
|
||||
let square = egui::Rect::from_min_size(egui::pos2(r.left() + 1.0, r.bottom() + 8.0), egui::vec2(273.0, 273.0));
|
||||
click(&mut h, square.left_bottom() + egui::vec2(2.0, -6.0));
|
||||
assert!(h.state().iter().all(|c| *c < 0.01), "black: {:?}", h.state());
|
||||
// Full saturation and value, then a hue a third of the way along: green.
|
||||
click(&mut h, square.right_top() + egui::vec2(-1.0, 1.0));
|
||||
let strip = square.bottom() + 14.0;
|
||||
click(&mut h, egui::pos2(square.left() + 275.0 / 3.0, strip));
|
||||
let [r, g, b] = *h.state();
|
||||
assert!(g > 0.9 && r < 0.05 && b < 0.05, "green: {:?}", h.state());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_copy_button_copies_the_rgb_values() {
|
||||
let mut h = open_picker([1.0, 0.0, 0.0]);
|
||||
h.get_by_label("Copy RGB values").click();
|
||||
h.step();
|
||||
let copied = h.output().platform_output.commands.iter().any(|c| matches!(c, egui::OutputCommand::CopyText(t) if t == "255, 0, 0"));
|
||||
assert!(copied, "{:?}", h.output().platform_output.commands);
|
||||
}
|
||||
Reference in new issue
Block a user