1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 22:00:03 -04:00

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

Added some DragValue widgets in the color_picker widget as input fields
for managing the RGBA values.
In case the provided values result in a not valid premultiplied alpha
RGBA color, a button will appear next to the input fields, to be used to
multiply the values with the alpha channel.


![image](https://user-images.githubusercontent.com/68190772/218438019-8b46936f-d025-4287-ac27-2b937f8f3d7c.png)

Closes <https://github.com/emilk/egui/issues/2716>.

---------

Co-authored-by: IVANMK-7 <68190772+IVANMK-7@users.noreply.github.com>
Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
Co-authored-by: Brian Janssen <tosti007@users.noreply.github.com>
This commit is contained in:
Ivan
2024-01-07 16:12:59 +01:00
committed by GitHub
parent 51e5d28b39
commit bfed2b4195
3 changed files with 252 additions and 59 deletions

View File

@@ -28,23 +28,23 @@ impl Hsva {
/// From `sRGBA` with premultiplied alpha
#[inline]
pub fn from_srgba_premultiplied(srgba: [u8; 4]) -> Self {
pub fn from_srgba_premultiplied([r, g, b, a]: [u8; 4]) -> Self {
Self::from_rgba_premultiplied(
linear_f32_from_gamma_u8(srgba[0]),
linear_f32_from_gamma_u8(srgba[1]),
linear_f32_from_gamma_u8(srgba[2]),
linear_f32_from_linear_u8(srgba[3]),
linear_f32_from_gamma_u8(r),
linear_f32_from_gamma_u8(g),
linear_f32_from_gamma_u8(b),
linear_f32_from_linear_u8(a),
)
}
/// From `sRGBA` without premultiplied alpha
#[inline]
pub fn from_srgba_unmultiplied(srgba: [u8; 4]) -> Self {
pub fn from_srgba_unmultiplied([r, g, b, a]: [u8; 4]) -> Self {
Self::from_rgba_unmultiplied(
linear_f32_from_gamma_u8(srgba[0]),
linear_f32_from_gamma_u8(srgba[1]),
linear_f32_from_gamma_u8(srgba[2]),
linear_f32_from_linear_u8(srgba[3]),
linear_f32_from_gamma_u8(r),
linear_f32_from_gamma_u8(g),
linear_f32_from_gamma_u8(b),
linear_f32_from_linear_u8(a),
)
}
@@ -83,6 +83,15 @@ impl Hsva {
}
}
#[inline]
pub fn from_additive_srgb([r, g, b]: [u8; 3]) -> Self {
Self::from_additive_rgb([
linear_f32_from_gamma_u8(r),
linear_f32_from_gamma_u8(g),
linear_f32_from_gamma_u8(b),
])
}
#[inline]
pub fn from_rgb(rgb: [f32; 3]) -> Self {
let (h, s, v) = hsv_from_rgb(rgb);
@@ -131,6 +140,8 @@ impl Hsva {
}
}
/// To linear space rgba in 0-1 range.
///
/// Represents additive colors using a negative alpha.
#[inline]
pub fn to_rgba_unmultiplied(&self) -> [f32; 4] {
@@ -150,6 +161,7 @@ impl Hsva {
]
}
/// To gamma-space 0-255.
#[inline]
pub fn to_srgba_unmultiplied(&self) -> [u8; 4] {
let [r, g, b, a] = self.to_rgba_unmultiplied();