Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DragValues for RGB(A) in the color picker #2734

Merged
merged 29 commits into from
Jan 7, 2024
Merged
Changes from 6 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
76e98a3
add RGBA values input fields for color_picker
IVAN-MK7 Feb 12, 2023
c863ae3
Merge branch 'emilk:master' into color-picker-rgba-input
IVAN-MK7 Feb 12, 2023
42ed529
cranky suggestions applied
IVAN-MK7 Feb 13, 2023
2cb5b20
Merge branch 'color-picker-rgba-input' of https://github.com/IVAN-MK7…
IVAN-MK7 Feb 13, 2023
be65b18
add color_picker RGBA DragValue prefix
IVAN-MK7 Mar 2, 2023
9d3bbd3
srgba_edit_ui using Color32
IVAN-MK7 Mar 2, 2023
f59c004
typos fix
IVAN-MK7 Mar 29, 2023
ba78e13
srgba_edit_ui use rgba unmultiplied u8 array
IVAN-MK7 Mar 30, 2023
0dece52
Update crates/egui/src/widgets/color_picker.rs
IVAN-MK7 Jun 7, 2023
5c83b18
srgba_edit_ui true on change of ref rbga array
IVAN-MK7 Jun 7, 2023
278dfe1
spell fix
IVAN-MK7 Jun 7, 2023
326e034
add rgba_edit_ui for f32
IVAN-MK7 Jun 8, 2023
c6e4546
merged with up to date origin master
IVAN-MK7 Oct 7, 2023
e3e23f5
removed duplicated functions
IVAN-MK7 Oct 7, 2023
9f399a4
Merge branch 'emilk-master' into color-picker-rgba-input
IVAN-MK7 Oct 7, 2023
ef89897
color_picker rearrange and input type add
IVAN-MK7 Oct 8, 2023
d3e1da8
color_text_ui now displays unmultiplied values
IVAN-MK7 Oct 8, 2023
e2df3de
rearrange buttons layout, remove rgba values label
IVAN-MK7 Nov 10, 2023
facaf95
remove colon from rgba dragvalue prefix
IVAN-MK7 Nov 10, 2023
0e4418e
Add is_additive_alpha helper
emilk Jan 7, 2024
ce29b3e
Merge branch 'master' into color-picker-rgba-input
emilk Jan 7, 2024
b278fa1
Use proper docstrings
emilk Jan 7, 2024
847d2f0
Add function for color picker type button
emilk Jan 7, 2024
61b951b
Clean up drag-values
emilk Jan 7, 2024
b581848
Code cleanup
emilk Jan 7, 2024
c0f143f
Only copy three decimals
emilk Jan 7, 2024
80c7317
Ui for picking global color input type
emilk Jan 7, 2024
e6cff8d
Better names
emilk Jan 7, 2024
b62d766
Move U8/F button
emilk Jan 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 81 additions & 9 deletions crates/egui/src/widgets/color_picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,44 @@ fn color_text_ui(ui: &mut Ui, color: impl Into<Color32>, alpha: Alpha) {
});
}

fn color_picker_hsvag_2d(ui: &mut Ui, hsva: &mut HsvaGamma, alpha: Alpha) {
fn color_picker_hsvag_2d(ui: &mut Ui, hsvag: &mut HsvaGamma, alpha: Alpha) {
let srgba = (std::convert::Into::<Hsva>::into(*hsvag)).to_srgba_unmultiplied();
let c32_premultiplied =
Color32::from_rgba_premultiplied(srgba[0], srgba[1], srgba[2], srgba[3]);
// Send an Opaque Alpha also when Alpha is BlendOrAdditive with negative alpha value (signals Additive blending),
// so to hide the alpha's DragValue in both cases.
let alpha_control = if alpha == Alpha::Opaque || hsvag.a < 0.0 {
Alpha::Opaque
} else {
alpha
};
IVAN-MK7 marked this conversation as resolved.
Show resolved Hide resolved
// Update hsvag only if the converted srgba is changed, this is beacause hsvag is made of f32,
// and the convertion between u8 and f32 loses a bit of the color precision, causing little flickering on hsvag based ui widgets.
if let Some(mut edited) = srgba_edit_ui(ui, c32_premultiplied, alpha_control) {
// Additive blending, signaled by the negative Alpha.
if hsvag.a < 0.0 {
let stored_a = hsvag.a;
// Alpha to 0 instead of negative, so it wont pop back to Normal blending when RGB are modified.
edited[3] = 0;
*hsvag = HsvaGamma::from(Hsva::from_srgba_unmultiplied(edited.to_array()));
// Keeps the Alpha set during Normal blending so that in case we alter RGB in Additive blending
// and the switch back to Normal blending it gets that Alpha value back.
hsvag.a = stored_a;
}
// Normal blending.
else {
*hsvag = HsvaGamma::from(Hsva::from_srgba_unmultiplied(edited.to_array()));
}
}

let current_color_size = vec2(ui.spacing().slider_width, ui.spacing().interact_size.y);
show_color(ui, *hsva, current_color_size).on_hover_text("Selected color");
show_color(ui, *hsvag, current_color_size).on_hover_text("Selected color");

color_text_ui(ui, *hsva, alpha);
color_text_ui(ui, *hsvag, alpha);

if alpha == Alpha::BlendOrAdditive {
// We signal additive blending by storing a negative alpha (a bit ironic).
let a = &mut hsva.a;
let a = &mut hsvag.a;
let mut additive = *a < 0.0;
ui.horizontal(|ui| {
ui.label("Blending:");
Expand All @@ -268,14 +297,14 @@ fn color_picker_hsvag_2d(ui: &mut Ui, hsva: &mut HsvaGamma, alpha: Alpha) {
}
});
}
let additive = hsva.a < 0.0;
let additive = hsvag.a < 0.0;

let opaque = HsvaGamma { a: 1.0, ..*hsva };
let opaque = HsvaGamma { a: 1.0, ..*hsvag };

if alpha == Alpha::Opaque {
hsva.a = 1.0;
hsvag.a = 1.0;
} else {
let a = &mut hsva.a;
let a = &mut hsvag.a;

if alpha == Alpha::OnlyBlend {
if *a < 0.0 {
Expand All @@ -287,7 +316,7 @@ fn color_picker_hsvag_2d(ui: &mut Ui, hsva: &mut HsvaGamma, alpha: Alpha) {
}
}

let HsvaGamma { h, s, v, a: _ } = hsva;
let HsvaGamma { h, s, v, a: _ } = hsvag;

color_slider_1d(ui, h, |h| {
HsvaGamma {
Expand All @@ -311,6 +340,49 @@ fn color_picker_hsvag_2d(ui: &mut Ui, hsva: &mut HsvaGamma, alpha: Alpha) {
color_slider_2d(ui, v, s, |v, s| HsvaGamma { s, v, ..opaque }.into());
}

/// Shows 4 `DragValue` widgets to be used to edit the Color32's RGBA values.
/// Alpha's `DragValue` is hidden when `Alpha::Opaque`.
///
/// Returns `Some(Color32)` on change.
fn srgba_edit_ui(ui: &mut Ui, rgba: Color32, alpha: Alpha) -> Option<Color32> {
IVAN-MK7 marked this conversation as resolved.
Show resolved Hide resolved
let [mut r, mut g, mut b, mut a] = rgba.to_array();

let mut edited = false;
ui.horizontal(|ui| {
if ui
.add(DragValue::new(&mut r).speed(0.5).prefix("R: "))
.changed()
{
edited = true;
}
if ui
.add(DragValue::new(&mut g).speed(0.5).prefix("G: "))
.changed()
{
edited = true;
}
if ui
.add(DragValue::new(&mut b).speed(0.5).prefix("B: "))
.changed()
{
edited = true;
}
if alpha != Alpha::Opaque
&& ui
.add(DragValue::new(&mut a).speed(0.5).prefix("A: "))
.changed()
{
edited = true;
}
});

if edited {
Some(Color32::from_rgba_premultiplied(r, g, b, a))
} else {
None
}
}

//// Shows a color picker where the user can change the given [`Hsva`] color.
///
/// Returns `true` on change.
Expand Down