Fix gamma correction for colored glyphs in iced_wgpu

This commit is contained in:
Héctor Ramón Jiménez 2023-09-19 23:00:20 +02:00
parent 9af0a27e67
commit be340a8cd8
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
4 changed files with 43 additions and 14 deletions

View file

@ -89,6 +89,26 @@ impl Color {
}
}
/// Creates a [`Color`] from its linear RGBA components.
pub fn from_linear_rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
// As described in:
// https://en.wikipedia.org/wiki/SRGB
fn gamma_component(u: f32) -> f32 {
if u < 0.0031308 {
12.92 * u
} else {
1.055 * u.powf(1.0 / 2.4) - 0.055
}
}
Self {
r: gamma_component(r),
g: gamma_component(g),
b: gamma_component(b),
a,
}
}
/// Converts the [`Color`] into its RGBA8 equivalent.
#[must_use]
pub fn into_rgba8(self) -> [u8; 4] {