mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 21:30:03 -04:00
Improve text redering and do all color operation in gamma space (#2071)
This commit is contained in:
@@ -195,17 +195,19 @@ impl FontImage {
|
||||
|
||||
/// Returns the textures as `sRGBA` premultiplied pixels, row by row, top to bottom.
|
||||
///
|
||||
/// `gamma` should normally be set to 1.0.
|
||||
/// 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> + '_ {
|
||||
/// `gamma` should normally be set to `None`.
|
||||
///
|
||||
/// If you are having problems with text looking skinny and pixelated, try using a low gamma, e.g. `0.4`.
|
||||
pub fn srgba_pixels(
|
||||
&'_ self,
|
||||
gamma: Option<f32>,
|
||||
) -> impl ExactSizeIterator<Item = Color32> + '_ {
|
||||
let gamma = gamma.unwrap_or(0.55); // TODO(emilk): this default coverage gamma is a magic constant, chosen by eye. I don't even know why we need it.
|
||||
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
|
||||
let alpha = coverage.powf(gamma);
|
||||
// We want to multiply with `vec4(alpha)` in the fragment shader:
|
||||
let a = fast_round(alpha * 255.0);
|
||||
Color32::from_rgba_premultiplied(a, a, a, a)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user