Skip to content

Commit

Permalink
Fix gamma correction for colored glyphs in iced_wgpu
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Sep 19, 2023
1 parent 9af0a27 commit be340a8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
20 changes: 20 additions & 0 deletions core/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
12 changes: 9 additions & 3 deletions graphics/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use paragraph::Paragraph;

pub use cosmic_text;

use crate::color;
use crate::core::font::{self, Font};
use crate::core::text::Shaping;
use crate::core::{Color, Size};
Expand Down Expand Up @@ -131,7 +132,12 @@ pub fn to_shaping(shaping: Shaping) -> cosmic_text::Shaping {
}

pub fn to_color(color: Color) -> cosmic_text::Color {
let [r, g, b, a] = color.into_rgba8();

cosmic_text::Color::rgba(r, g, b, a)
let [r, g, b, a] = color::pack(color).components();

cosmic_text::Color::rgba(
(r * 255.0) as u8,
(g * 255.0) as u8,
(b * 255.0) as u8,
(a * 255.0) as u8,
)
}
14 changes: 13 additions & 1 deletion tiny_skia/src/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::core::alignment;
use crate::core::text::{LineHeight, Shaping};
use crate::core::{Color, Font, Pixels, Point, Rectangle};
use crate::graphics::color;
use crate::graphics::text::cache::{self, Cache};
use crate::graphics::text::editor;
use crate::graphics::text::font_system;
Expand Down Expand Up @@ -216,7 +217,18 @@ fn draw(
fn from_color(color: cosmic_text::Color) -> Color {
let [r, g, b, a] = color.as_rgba();

Color::from_rgba8(r, g, b, a as f32 / 255.0)
if color::GAMMA_CORRECTION {
// `cosmic_text::Color` is linear RGB in this case, so we
// need to convert back to sRGB
Color::from_linear_rgba(
r as f32 / 255.0,
g as f32 / 255.0,
b as f32 / 255.0,
a as f32 / 255.0,
)
} else {
Color::from_rgba8(r, g, b, a as f32 / 255.0)
}
}

#[derive(Debug, Clone, Default)]
Expand Down
13 changes: 2 additions & 11 deletions wgpu/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::core::alignment;
use crate::core::{Rectangle, Size};
use crate::graphics::color;
use crate::graphics::text::cache::{self, Cache};
use crate::graphics::text::{font_system, Editor, Paragraph};
use crate::graphics::text::{font_system, to_color, Editor, Paragraph};
use crate::layer::Text;

use std::borrow::Cow;
Expand Down Expand Up @@ -214,16 +214,7 @@ impl Pipeline {
right: (clip_bounds.x + clip_bounds.width) as i32,
bottom: (clip_bounds.y + clip_bounds.height) as i32,
},
default_color: {
let [r, g, b, a] = color::pack(color).components();

glyphon::Color::rgba(
(r * 255.0) as u8,
(g * 255.0) as u8,
(b * 255.0) as u8,
(a * 255.0) as u8,
)
},
default_color: to_color(color),
})
},
);
Expand Down

0 comments on commit be340a8

Please sign in to comment.