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

Do all blending in gamma space

This is an experiment to investigate solutions to
* https://github.com/emilk/egui/issues/1410
This commit is contained in:
Emil Ernerfeldt
2022-03-23 13:45:50 +01:00
parent 0f0031ebbb
commit 4969f7e18b
5 changed files with 30 additions and 90 deletions

View File

@@ -311,22 +311,22 @@ impl Rgba {
}
pub fn from_luminance_alpha(l: f32, a: f32) -> Self {
crate::epaint_assert!(0.0 <= l && l <= 1.0);
crate::epaint_assert!(0.0 <= a && a <= 1.0);
crate::epaint_assert!(0.0 <= l && l <= 1.0, "l: {}", l);
crate::epaint_assert!(0.0 <= a && a <= 1.0, "a: {}", a);
Self([l * a, l * a, l * a, a])
}
/// Transparent black
#[inline(always)]
pub fn from_black_alpha(a: f32) -> Self {
crate::epaint_assert!(0.0 <= a && a <= 1.0);
crate::epaint_assert!(0.0 <= a && a <= 1.0, "a: {}", a);
Self([0.0, 0.0, 0.0, a])
}
/// Transparent white
#[inline(always)]
pub fn from_white_alpha(a: f32) -> Self {
crate::epaint_assert!(0.0 <= a && a <= 1.0);
crate::epaint_assert!(0.0 <= a && a <= 1.0, "a: {}", a);
Self([a, a, a, a])
}

View File

@@ -197,13 +197,10 @@ impl FontImage {
/// If you are having problems with text looking skinny and pixelated, try
/// setting a lower gamma, e.g. `0.5`.
pub fn srgba_pixels(&'_ self, gamma: f32) -> impl ExactSizeIterator<Item = Color32> + '_ {
self.pixels.iter().map(move |coverage| {
// This is arbitrarily chosen to make text look as good as possible.
// In particular, it looks good with gamma=1 and the default eframe backend,
// which uses linear blending.
// See https://github.com/emilk/egui/issues/1410
let a = fast_round(coverage.powf(gamma / 2.2) * 255.0);
Color32::from_rgba_premultiplied(a, a, a, a) // this makes no sense, but works
self.pixels.iter().map(move |&coverage| {
Color32::from(crate::Rgba::from_white_alpha(
coverage.clamp(0.0, 1.0).powf(gamma),
))
})
}
@@ -252,10 +249,6 @@ impl From<FontImage> for ImageData {
}
}
fn fast_round(r: f32) -> u8 {
(r + 0.5).floor() as _ // rust does a saturating cast since 1.45
}
// ----------------------------------------------------------------------------
/// A change to an image.