diff --git a/Cargo.lock b/Cargo.lock index cc0923a..49f094e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1853,7 +1853,7 @@ dependencies = [ [[package]] name = "glyphon" version = "0.3.0" -source = "git+https://github.com/jackpot51/glyphon.git?branch=refactor#cd704e6bd5d0ddb815d08358766ad205fd70fada" +source = "git+https://github.com/jackpot51/glyphon.git?branch=refactor#c28dc99c86b6b598633e6623096b21632f266976" dependencies = [ "cosmic-text", "etagere", diff --git a/src/terminal.rs b/src/terminal.rs index a6bd6c5..1a40a7d 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -96,6 +96,25 @@ fn convert_color(colors: &Colors, color: Color) -> cosmic_text::Color { cosmic_text::Color::rgb(rgb.r, rgb.g, rgb.b) } +fn linear_color(color: cosmic_text::Color) -> cosmic_text::Color { + // As described in: https://en.wikipedia.org/wiki/SRGB#The_reverse_transformation + fn linear_component(byte: u8) -> u8 { + let float = byte as f32 / 255.0; + let linear = if float < 0.04045 { + float / 12.92 + } else { + ((float + 0.055) / 1.055).powf(2.4) + }; + (linear * 255.0).round() as u8 + } + cosmic_text::Color::rgba( + linear_component(color.r()), + linear_component(color.g()), + linear_component(color.b()), + color.a(), + ) +} + pub struct Terminal { default_attrs: Attrs<'static>, buffer: Arc, @@ -443,7 +462,8 @@ impl Terminal { } } - attrs = attrs.color(fg); + // Convert foreground to linear + attrs = attrs.color(linear_color(fg)); // Use metadata as background color attrs = attrs.metadata(bg.0 as usize); //TODO: more flags