1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -04:00

Expose more color-related functions and types

This commit is contained in:
Emil Ernerfeldt
2021-02-07 10:36:51 +01:00
parent a9949b21af
commit d07a17ac6a
2 changed files with 144 additions and 121 deletions

View File

@@ -367,77 +367,3 @@ pub fn color_edit_button_srgba(ui: &mut Ui, srgba: &mut Color32, alpha: Alpha) -
response
}
// ----------------------------------------------------------------------------
/// Like Hsva but with the `v` (value/brightness) being gamma corrected
/// so that it is perceptually even in sliders.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct HsvaGamma {
/// hue 0-1
pub h: f32,
/// saturation 0-1
pub s: f32,
/// value 0-1, in gamma-space (~perceptually even)
pub v: f32,
/// alpha 0-1. A negative value signifies an additive color (and alpha is ignored).
pub a: f32,
}
impl From<HsvaGamma> for Rgba {
fn from(hsvag: HsvaGamma) -> Rgba {
Hsva::from(hsvag).into()
}
}
impl From<HsvaGamma> for Color32 {
fn from(hsvag: HsvaGamma) -> Color32 {
Rgba::from(hsvag).into()
}
}
impl From<HsvaGamma> for Hsva {
fn from(hsvag: HsvaGamma) -> Hsva {
let HsvaGamma { h, s, v, a } = hsvag;
Hsva {
h,
s,
v: linear_from_srgb(v),
a,
}
}
}
impl From<Hsva> for HsvaGamma {
fn from(hsva: Hsva) -> HsvaGamma {
let Hsva { h, s, v, a } = hsva;
HsvaGamma {
h,
s,
v: srgb_from_linear(v),
a,
}
}
}
/// [0, 1] -> [0, 1]
fn linear_from_srgb(s: f32) -> f32 {
if s < 0.0 {
-linear_from_srgb(-s)
} else if s <= 0.04045 {
s / 12.92
} else {
((s + 0.055) / 1.055).powf(2.4)
}
}
/// [0, 1] -> [0, 1]
fn srgb_from_linear(l: f32) -> f32 {
if l < 0.0 {
-srgb_from_linear(-l)
} else if l <= 0.0031308 {
12.92 * l
} else {
1.055 * l.powf(1.0 / 2.4) - 0.055
}
}