simplify Color data type
There's something fishy with colorspaces and blending. This commit removes the `window::Color` type and replaces it and the confusing array of color types exposed by the `palette` crate with a pair of much simpler types: `LinearRgb` - a tuple of f32 linear color components `SrgbaPixel` - the u32 sRGBA pixel representation This doesn't change anything about rendering, it just makes it a bit simpler and makes the SrgbaPixel -> LinearRgb conversion happen slightly earlier which shaves off some ad-hoc conversions. Refs: https://github.com/wez/wezterm/issues/544
This commit is contained in:
1 parent
59ec969a26
commit
a0d39989df
9 files changed
+162
-323
No files matched your search
Generated
-1
@@ -4654,7 +4654,6 @@ dependencies = [
|
||||
"metrics",
|
||||
"mio",
|
||||
"objc",
|
||||
"palette",
|
||||
"pretty_env_logger",
|
||||
"promise",
|
||||
"resize",
|
||||
|
||||
@@ -2,10 +2,12 @@ use super::utilsprites::RenderMetrics;
|
||||
use ::window::bitmaps::atlas::{Atlas, Sprite};
|
||||
#[cfg(test)]
|
||||
use ::window::bitmaps::ImageTexture;
|
||||
use ::window::bitmaps::{Image, Texture2d};
|
||||
use ::window::bitmaps::{BitmapImage, Image, Texture2d};
|
||||
use ::window::color::{LinearRgba, SrgbaPixel};
|
||||
use ::window::glium;
|
||||
use ::window::glium::backend::Context as GliumContext;
|
||||
use ::window::glium::texture::SrgbTexture2d;
|
||||
use ::window::*;
|
||||
use ::window::{Point, Rect};
|
||||
use anyhow::{anyhow, Context};
|
||||
use config::{configuration, AllowSquareGlyphOverflow, TextStyle};
|
||||
use euclid::num::Zero;
|
||||
@@ -605,8 +607,8 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
self.metrics.cell_size.width as usize,
|
||||
self.metrics.cell_size.height as usize,
|
||||
);
|
||||
let black = ::window::color::Color::rgba(0, 0, 0, 0);
|
||||
let white = ::window::color::Color::rgb(0xff, 0xff, 0xff);
|
||||
let black = SrgbaPixel::rgba(0, 0, 0, 0);
|
||||
let white = SrgbaPixel::rgba(0xff, 0xff, 0xff, 0xff);
|
||||
|
||||
let cell_rect = Rect::new(Point::new(0, 0), self.metrics.cell_size);
|
||||
|
||||
@@ -627,7 +629,6 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
cell_rect.origin.y + y as isize,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -639,7 +640,6 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
cell_rect.origin.y + self.metrics.cell_size.height,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
};
|
||||
|
||||
@@ -659,7 +659,6 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
cell_rect.origin.y + y as isize,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -699,14 +698,14 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
}
|
||||
BlockKey::Full(alpha) => {
|
||||
let alpha = match alpha {
|
||||
BlockAlpha::Full => 0xff,
|
||||
BlockAlpha::Dark => (0.75 * 255.) as u8,
|
||||
BlockAlpha::Medium => (0.5 * 255.) as u8,
|
||||
BlockAlpha::Light => (0.25 * 255.) as u8,
|
||||
BlockAlpha::Full => 1.0,
|
||||
BlockAlpha::Dark => 0.75,
|
||||
BlockAlpha::Medium => 0.5,
|
||||
BlockAlpha::Light => 0.25,
|
||||
};
|
||||
let fill = ::window::color::Color::with_linear_rgba_u8(alpha, alpha, alpha, alpha);
|
||||
let fill = LinearRgba::with_components(alpha, alpha, alpha, alpha);
|
||||
|
||||
buffer.clear_rect(cell_rect, fill);
|
||||
buffer.clear_rect(cell_rect, fill.srgba_pixel());
|
||||
}
|
||||
BlockKey::Quadrants(quads) => {
|
||||
let y_half = self.metrics.cell_size.height as f32 / 2.;
|
||||
@@ -750,8 +749,8 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
self.metrics.cell_size.width as usize,
|
||||
self.metrics.cell_size.height as usize,
|
||||
);
|
||||
let black = ::window::color::Color::rgba(0, 0, 0, 0);
|
||||
let white = ::window::color::Color::rgb(0xff, 0xff, 0xff);
|
||||
let black = SrgbaPixel::rgba(0, 0, 0, 0);
|
||||
let white = SrgbaPixel::rgba(0xff, 0xff, 0xff, 0xff);
|
||||
|
||||
let cell_rect = Rect::new(Point::new(0, 0), self.metrics.cell_size);
|
||||
|
||||
@@ -767,7 +766,6 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
cell_rect.origin.y + self.metrics.descender_row + row,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -785,7 +783,7 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
let range =
|
||||
buffer.horizontal_pixel_range_mut(0, self.metrics.cell_size.width as usize, y);
|
||||
for c in range.iter_mut() {
|
||||
*c = color.0;
|
||||
*c = color.as_srgba32();
|
||||
count -= 1;
|
||||
if count == 0 {
|
||||
color = if color == white { black } else { white };
|
||||
@@ -807,7 +805,7 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
let range =
|
||||
buffer.horizontal_pixel_range_mut(0, self.metrics.cell_size.width as usize, y);
|
||||
for c in range.iter_mut() {
|
||||
*c = color.0;
|
||||
*c = color.as_srgba32();
|
||||
count -= 1;
|
||||
if count == 0 {
|
||||
color = if color == white { black } else { white };
|
||||
@@ -832,9 +830,9 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
fn add(x: usize, y: usize, val: u8, max_y: usize, buffer: &mut Image) {
|
||||
let y = y.min(max_y);
|
||||
let pixel = buffer.pixel_mut(x, y);
|
||||
let (current, _, _, _) = Color(*pixel).as_rgba();
|
||||
let (current, _, _, _) = SrgbaPixel::with_srgba_u32(*pixel).as_rgba();
|
||||
let value = current.saturating_add(val);
|
||||
*pixel = Color::rgb(value, value, value).0;
|
||||
*pixel = SrgbaPixel::rgba(value, value, value, 0xff).as_srgba32();
|
||||
}
|
||||
|
||||
for x in 0..self.metrics.cell_size.width as usize {
|
||||
@@ -864,7 +862,6 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
cell_rect.origin.y + first_line + row,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
buffer.draw_line(
|
||||
Point::new(
|
||||
@@ -876,7 +873,6 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
cell_rect.origin.y + self.metrics.descender_plus_two + row,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -893,7 +889,6 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
cell_rect.origin.y + self.metrics.strike_row + row,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -907,7 +902,6 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
cell_rect.origin.y + row,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
+11
-14
@@ -4,7 +4,8 @@
|
||||
|
||||
use crate::renderstate::TripleVertexBuffer;
|
||||
use ::window::bitmaps::TextureRect;
|
||||
use ::window::*;
|
||||
use ::window::color::LinearRgba;
|
||||
use ::window::glium::buffer::Mapping;
|
||||
use std::cell::RefMut;
|
||||
|
||||
/// Each cell is composed of two triangles built from 4 vertices.
|
||||
@@ -75,7 +76,7 @@ pub struct Quads {
|
||||
}
|
||||
|
||||
pub struct MappedQuads<'a> {
|
||||
mapping: glium::buffer::Mapping<'a, [Vertex]>,
|
||||
mapping: Mapping<'a, [Vertex]>,
|
||||
quads: Quads,
|
||||
}
|
||||
|
||||
@@ -170,24 +171,21 @@ impl<'a> Quad<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_fg_color(&mut self, color: Color) {
|
||||
let color = color.to_tuple_rgba();
|
||||
pub fn set_fg_color(&mut self, color: LinearRgba) {
|
||||
for v in self.vert.iter_mut() {
|
||||
v.fg_color = color;
|
||||
v.fg_color = color.tuple();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_underline_color(&mut self, color: Color) {
|
||||
let color = color.to_tuple_rgba();
|
||||
pub fn set_underline_color(&mut self, color: LinearRgba) {
|
||||
for v in self.vert.iter_mut() {
|
||||
v.underline_color = color;
|
||||
v.underline_color = color.tuple();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_bg_color(&mut self, color: Color) {
|
||||
let color = color.to_tuple_rgba();
|
||||
pub fn set_bg_color(&mut self, color: LinearRgba) {
|
||||
for v in self.vert.iter_mut() {
|
||||
v.bg_color = color;
|
||||
v.bg_color = color.tuple();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,10 +204,9 @@ impl<'a> Quad<'a> {
|
||||
self.vert[V_BOT_RIGHT].cursor = (coords.max_x(), coords.max_y());
|
||||
}
|
||||
|
||||
pub fn set_cursor_color(&mut self, color: Color) {
|
||||
let color = color.to_tuple_rgba();
|
||||
pub fn set_cursor_color(&mut self, color: LinearRgba) {
|
||||
for v in self.vert.iter_mut() {
|
||||
v.cursor_color = color;
|
||||
v.cursor_color = color.tuple();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ use wezterm_term::color::{ColorAttribute, ColorPalette, RgbColor};
|
||||
use wezterm_term::{CellAttributes, Line, StableRowIndex};
|
||||
use window::bitmaps::atlas::SpriteSlice;
|
||||
use window::bitmaps::Texture2d;
|
||||
use window::Color;
|
||||
use window::color::LinearRgba;
|
||||
|
||||
pub struct RenderScreenLineOpenGLParams<'a> {
|
||||
pub line_idx: usize,
|
||||
@@ -40,14 +40,14 @@ pub struct RenderScreenLineOpenGLParams<'a> {
|
||||
pub config: &'a ConfigHandle,
|
||||
pub pos: &'a PositionedPane,
|
||||
|
||||
pub cursor_border_color: Color,
|
||||
pub foreground: Color,
|
||||
pub cursor_border_color: LinearRgba,
|
||||
pub foreground: LinearRgba,
|
||||
pub is_active: bool,
|
||||
|
||||
pub selection_fg: Color,
|
||||
pub selection_bg: Color,
|
||||
pub cursor_fg: Color,
|
||||
pub cursor_bg: Color,
|
||||
pub selection_fg: LinearRgba,
|
||||
pub selection_bg: LinearRgba,
|
||||
pub cursor_fg: LinearRgba,
|
||||
pub cursor_bg: LinearRgba,
|
||||
}
|
||||
|
||||
pub struct ComputeCellFgBgParams<'a> {
|
||||
@@ -55,20 +55,20 @@ pub struct ComputeCellFgBgParams<'a> {
|
||||
pub cell_idx: usize,
|
||||
pub cursor: &'a StableCursorPosition,
|
||||
pub selection: &'a Range<usize>,
|
||||
pub fg_color: Color,
|
||||
pub bg_color: Color,
|
||||
pub fg_color: LinearRgba,
|
||||
pub bg_color: LinearRgba,
|
||||
pub palette: &'a ColorPalette,
|
||||
pub is_active_pane: bool,
|
||||
pub config: &'a ConfigHandle,
|
||||
pub selection_fg: Color,
|
||||
pub selection_bg: Color,
|
||||
pub cursor_fg: Color,
|
||||
pub cursor_bg: Color,
|
||||
pub selection_fg: LinearRgba,
|
||||
pub selection_bg: LinearRgba,
|
||||
pub cursor_fg: LinearRgba,
|
||||
pub cursor_bg: LinearRgba,
|
||||
}
|
||||
|
||||
pub struct ComputeCellFgBgResult {
|
||||
pub fg_color: Color,
|
||||
pub bg_color: Color,
|
||||
pub fg_color: LinearRgba,
|
||||
pub bg_color: LinearRgba,
|
||||
pub cursor_shape: Option<CursorShape>,
|
||||
}
|
||||
|
||||
@@ -86,8 +86,8 @@ impl super::TermWindow {
|
||||
let palette = self.palette();
|
||||
let background = rgbcolor_alpha_to_window_color(palette.background, background_alpha);
|
||||
|
||||
let (r, g, b, a) = background.to_tuple_rgba();
|
||||
frame.clear_color_srgb(r, g, b, a);
|
||||
let (r, g, b, a) = background.tuple();
|
||||
frame.clear_color(r, g, b, a);
|
||||
}
|
||||
|
||||
for pass in 0.. {
|
||||
@@ -203,10 +203,10 @@ impl super::TermWindow {
|
||||
foreground,
|
||||
pos,
|
||||
is_active: true,
|
||||
selection_fg: Color(0),
|
||||
selection_bg: Color(0),
|
||||
cursor_fg: Color(0),
|
||||
cursor_bg: Color(0),
|
||||
selection_fg: LinearRgba::default(),
|
||||
selection_bg: LinearRgba::default(),
|
||||
cursor_fg: LinearRgba::default(),
|
||||
cursor_bg: LinearRgba::default(),
|
||||
},
|
||||
&mut quads,
|
||||
)?;
|
||||
@@ -996,9 +996,9 @@ impl super::TermWindow {
|
||||
params: &RenderScreenLineOpenGLParams,
|
||||
hsv: Option<config::HsbTransform>,
|
||||
cursor_shape: Option<CursorShape>,
|
||||
glyph_color: Color,
|
||||
underline_color: Color,
|
||||
bg_color: Color,
|
||||
glyph_color: LinearRgba,
|
||||
underline_color: LinearRgba,
|
||||
bg_color: LinearRgba,
|
||||
white_space: TextureRect,
|
||||
) -> anyhow::Result<()> {
|
||||
let sprite = gl_state
|
||||
@@ -1042,9 +1042,9 @@ impl super::TermWindow {
|
||||
params: &RenderScreenLineOpenGLParams,
|
||||
hsv: Option<config::HsbTransform>,
|
||||
cursor_shape: Option<CursorShape>,
|
||||
glyph_color: Color,
|
||||
underline_color: Color,
|
||||
bg_color: Color,
|
||||
glyph_color: LinearRgba,
|
||||
underline_color: LinearRgba,
|
||||
bg_color: LinearRgba,
|
||||
white_space: TextureRect,
|
||||
) -> anyhow::Result<()> {
|
||||
let padding = self
|
||||
@@ -1238,10 +1238,13 @@ impl super::TermWindow {
|
||||
}
|
||||
}
|
||||
|
||||
fn rgbcolor_to_window_color(color: RgbColor) -> Color {
|
||||
fn rgbcolor_to_window_color(color: RgbColor) -> LinearRgba {
|
||||
rgbcolor_alpha_to_window_color(color, 0xff)
|
||||
}
|
||||
|
||||
fn rgbcolor_alpha_to_window_color(color: RgbColor, alpha: u8) -> Color {
|
||||
Color::rgba(color.red, color.green, color.blue, alpha)
|
||||
fn rgbcolor_alpha_to_window_color(color: RgbColor, alpha: u8) -> LinearRgba {
|
||||
// Note `RgbColor` is intended to be SRGB, but in practice it appears
|
||||
// as though it is linear RGB, hence this is using with_rgba rather than
|
||||
// with_srgba.
|
||||
LinearRgba::with_rgba(color.red, color.green, color.blue, alpha)
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
use super::glyphcache::GlyphCache;
|
||||
use ::window::bitmaps::atlas::{OutOfTextureSpace, Sprite};
|
||||
use ::window::bitmaps::{BitmapImage, Image, Texture2d};
|
||||
use ::window::*;
|
||||
use ::window::color::SrgbaPixel;
|
||||
use ::window::{Point, Rect, Size};
|
||||
use anyhow::Context;
|
||||
use config::configuration;
|
||||
use std::rc::Rc;
|
||||
@@ -68,8 +69,8 @@ impl<T: Texture2d> UtilSprites<T> {
|
||||
metrics.cell_size.height as usize,
|
||||
);
|
||||
|
||||
let black = ::window::color::Color::rgba(0, 0, 0, 0);
|
||||
let white = ::window::color::Color::rgb(0xff, 0xff, 0xff);
|
||||
let black = SrgbaPixel::rgba(0, 0, 0, 0);
|
||||
let white = SrgbaPixel::rgba(0xff, 0xff, 0xff, 0xff);
|
||||
|
||||
let cell_rect = Rect::new(Point::new(0, 0), metrics.cell_size);
|
||||
|
||||
@@ -92,7 +93,6 @@ impl<T: Texture2d> UtilSprites<T> {
|
||||
cell_rect.origin.y + i,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
// Bottom border
|
||||
buffer.draw_line(
|
||||
@@ -105,7 +105,6 @@ impl<T: Texture2d> UtilSprites<T> {
|
||||
cell_rect.origin.y + metrics.cell_size.height.saturating_sub(1 + i),
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
}
|
||||
for i in 0..border_width {
|
||||
@@ -117,7 +116,6 @@ impl<T: Texture2d> UtilSprites<T> {
|
||||
cell_rect.origin.y + metrics.cell_size.height,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
// Right border
|
||||
buffer.draw_line(
|
||||
@@ -130,7 +128,6 @@ impl<T: Texture2d> UtilSprites<T> {
|
||||
cell_rect.origin.y + metrics.cell_size.height,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
}
|
||||
let cursor_box = glyph_cache.atlas.allocate(&buffer)?;
|
||||
@@ -145,7 +142,6 @@ impl<T: Texture2d> UtilSprites<T> {
|
||||
cell_rect.origin.y + metrics.cell_size.height,
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
}
|
||||
let cursor_i_beam = glyph_cache.atlas.allocate(&buffer)?;
|
||||
@@ -163,7 +159,6 @@ impl<T: Texture2d> UtilSprites<T> {
|
||||
cell_rect.origin.y + metrics.cell_size.height.saturating_sub(1 + i),
|
||||
),
|
||||
white,
|
||||
Operator::Source,
|
||||
);
|
||||
}
|
||||
let cursor_underline = glyph_cache.atlas.allocate(&buffer)?;
|
||||
|
||||
@@ -26,7 +26,6 @@ libloading = "0.6"
|
||||
line_drawing = "0.8"
|
||||
log = "0.4"
|
||||
metrics = { version="0.14", features=["std"]}
|
||||
palette = "0.5"
|
||||
promise = { path = "../promise" }
|
||||
resize = "0.5"
|
||||
serde = {version="1.0", features = ["rc", "derive"]}
|
||||
|
||||
+16
-28
@@ -1,7 +1,6 @@
|
||||
use crate::color::Color;
|
||||
use crate::{Operator, Point, Rect, Size};
|
||||
use crate::color::{LinearRgba, SrgbaPixel};
|
||||
use crate::{Point, Rect, Size};
|
||||
use glium::texture::SrgbTexture2d;
|
||||
use palette::LinSrgba;
|
||||
use std::cell::RefCell;
|
||||
|
||||
pub mod atlas;
|
||||
@@ -158,13 +157,13 @@ pub trait BitmapImage {
|
||||
}
|
||||
|
||||
/// Clear the entire image to the specific color
|
||||
fn clear(&mut self, color: Color) {
|
||||
fn clear(&mut self, color: SrgbaPixel) {
|
||||
for c in self.pixels_mut() {
|
||||
*c = color.0;
|
||||
*c = color.as_srgba32();
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_rect(&mut self, rect: Rect, color: Color) {
|
||||
fn clear_rect(&mut self, rect: Rect, color: SrgbaPixel) {
|
||||
let (dim_width, dim_height) = self.image_dimensions();
|
||||
let max_x = rect.max_x().min(dim_width as isize) as usize;
|
||||
let max_y = rect.max_y().min(dim_height as isize) as usize;
|
||||
@@ -178,18 +177,17 @@ pub trait BitmapImage {
|
||||
for y in dest_y..max_y {
|
||||
let range = self.horizontal_pixel_range_mut(dest_x, max_x, y);
|
||||
for c in range {
|
||||
*c = color.0;
|
||||
*c = color.as_srgba32();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw a line starting at `start` and ending at `end`.
|
||||
/// The line will be anti-aliased and applied to the surface using the
|
||||
/// specified Operator.
|
||||
fn draw_line(&mut self, start: Point, end: Point, color: Color, operator: Operator) {
|
||||
/// The line will be anti-aliased and applied to the surface.
|
||||
fn draw_line(&mut self, start: Point, end: Point, color: SrgbaPixel) {
|
||||
let (dim_width, dim_height) = self.image_dimensions();
|
||||
let linear: LinSrgba = color.into();
|
||||
let (red, green, blue, alpha) = linear.into_components();
|
||||
let linear = color.to_linear();
|
||||
let (red, green, blue, alpha) = linear.tuple();
|
||||
|
||||
for ((x, y), value) in line_drawing::XiaolinWu::<f32, isize>::new(
|
||||
(start.x as f32, start.y as f32),
|
||||
@@ -203,13 +201,13 @@ pub trait BitmapImage {
|
||||
}
|
||||
let pix = self.pixel_mut(x as usize, y as usize);
|
||||
|
||||
let color: Color = LinSrgba::from_components((red, green, blue, alpha * value)).into();
|
||||
*pix = color.composite(Color(*pix), operator).0;
|
||||
let color = LinearRgba::with_components(red, green, blue, alpha * value);
|
||||
*pix = color.srgba_pixel().as_srgba32();
|
||||
}
|
||||
}
|
||||
|
||||
/// Draw a 1-pixel wide rectangle
|
||||
fn draw_rect(&mut self, rect: Rect, color: Color, operator: Operator) {
|
||||
fn draw_rect(&mut self, rect: Rect, color: SrgbaPixel) {
|
||||
let bottom_right = rect.origin.add_size(&rect.size);
|
||||
|
||||
// Draw the vertical lines down either side
|
||||
@@ -217,36 +215,26 @@ pub trait BitmapImage {
|
||||
rect.origin,
|
||||
Point::new(rect.origin.x, bottom_right.y),
|
||||
color,
|
||||
operator,
|
||||
);
|
||||
self.draw_line(
|
||||
Point::new(bottom_right.x, rect.origin.y),
|
||||
bottom_right,
|
||||
color,
|
||||
operator,
|
||||
);
|
||||
// And the horizontals for the top and bottom
|
||||
self.draw_line(
|
||||
rect.origin,
|
||||
Point::new(bottom_right.x, rect.origin.y),
|
||||
color,
|
||||
operator,
|
||||
);
|
||||
self.draw_line(
|
||||
Point::new(rect.origin.x, bottom_right.y),
|
||||
bottom_right,
|
||||
color,
|
||||
operator,
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_image(
|
||||
&mut self,
|
||||
dest_top_left: Point,
|
||||
src_rect: Option<Rect>,
|
||||
im: &dyn BitmapImage,
|
||||
operator: Operator,
|
||||
) {
|
||||
fn draw_image(&mut self, dest_top_left: Point, src_rect: Option<Rect>, im: &dyn BitmapImage) {
|
||||
let (im_width, im_height) = im.image_dimensions();
|
||||
let src_rect = src_rect
|
||||
.unwrap_or_else(|| Rect::from_size(Size::new(im_width as isize, im_height as isize)));
|
||||
@@ -295,7 +283,7 @@ pub trait BitmapImage {
|
||||
dest_y as usize,
|
||||
);
|
||||
for (src_pix, dest_pix) in src_pixels.iter().zip(dest_pixels.iter_mut()) {
|
||||
*dest_pix = Color(*src_pix).composite(Color(*dest_pix), operator).0;
|
||||
*dest_pix = *src_pix;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -441,7 +429,7 @@ impl ImageTexture {
|
||||
impl Texture2d for ImageTexture {
|
||||
fn write(&self, rect: Rect, im: &dyn BitmapImage) {
|
||||
let mut image = self.image.borrow_mut();
|
||||
image.draw_image(rect.origin, None, im, Operator::Source);
|
||||
image.draw_image(rect.origin, None, im);
|
||||
}
|
||||
|
||||
fn read(&self, _rect: Rect, _im: &mut dyn BitmapImage) {
|
||||
|
||||
+80
-192
@@ -1,10 +1,8 @@
|
||||
use crate::Operator;
|
||||
use palette::{Blend, LinSrgb, LinSrgba, Srgb, Srgba};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref SRGB_TO_F32_TABLE: [f32;256] = generate_srgb8_to_linear_f32_table();
|
||||
static ref F32_TO_U8_TABLE: [u32;104] = generate_linear_f32_to_srgb8_table();
|
||||
static ref RGB_TO_SRGB_TABLE: [u8;256] = generate_rgb_to_srgb8_table();
|
||||
static ref RGB_TO_F32_TABLE: [f32;256] = generate_rgb_to_linear_f32_table();
|
||||
}
|
||||
|
||||
fn generate_rgb_to_srgb8_table() -> [u8; 256] {
|
||||
@@ -16,11 +14,19 @@ fn generate_rgb_to_srgb8_table() -> [u8; 256] {
|
||||
table
|
||||
}
|
||||
|
||||
fn generate_rgb_to_linear_f32_table() -> [f32; 256] {
|
||||
let mut table = [0.; 256];
|
||||
for (val, entry) in table.iter_mut().enumerate() {
|
||||
*entry = (val as f32) / 255.0;
|
||||
}
|
||||
table
|
||||
}
|
||||
|
||||
fn generate_srgb8_to_linear_f32_table() -> [f32; 256] {
|
||||
let mut table = [0.; 256];
|
||||
for (val, entry) in table.iter_mut().enumerate() {
|
||||
let c = (val as f32) / 255.0;
|
||||
*entry = if c < 0.04045 {
|
||||
*entry = if c <= 0.04045 {
|
||||
c / 12.92
|
||||
} else {
|
||||
((c + 0.055) / 1.055).powf(2.4)
|
||||
@@ -148,171 +154,28 @@ fn linear_f32_to_srgb8_using_table(f: f32) -> u8 {
|
||||
((bias + scale * t) >> 16) as u8
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
#[allow(clippy::unreadable_literal)]
|
||||
fn linear_f32_to_srgb8_vec(s: LinSrgba) -> Color {
|
||||
use std::arch::x86_64::*;
|
||||
|
||||
unsafe fn i32_get(m: *const __m128i, idx: isize) -> i32 {
|
||||
let u: *const i32 = m as _;
|
||||
*u.offset(idx)
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let clamp_min_4 = _mm_set1_epi32((127 - 13) << 23);
|
||||
let almost_one_4 = _mm_set1_epi32(0x3f7fffff);
|
||||
let mant_mask_4 = _mm_set1_epi32(0xff);
|
||||
let top_scale_4 = _mm_set1_epi32(0x02000000);
|
||||
|
||||
let f = _mm_set_ps(s.red, s.green, s.blue, s.alpha);
|
||||
|
||||
let clamped = _mm_min_ps(
|
||||
_mm_max_ps(f, _mm_castsi128_ps(clamp_min_4)),
|
||||
_mm_castsi128_ps(almost_one_4),
|
||||
);
|
||||
|
||||
let tabidx = _mm_srli_epi32(_mm_castps_si128(clamped), 20);
|
||||
|
||||
let tabval = _mm_set_epi32(
|
||||
*F32_TO_U8_TABLE.get_unchecked((i32_get(&tabidx, 0) - (127 - 13) * 8) as usize) as i32,
|
||||
*F32_TO_U8_TABLE.get_unchecked((i32_get(&tabidx, 1) - (127 - 13) * 8) as usize) as i32,
|
||||
*F32_TO_U8_TABLE.get_unchecked((i32_get(&tabidx, 2) - (127 - 13) * 8) as usize) as i32,
|
||||
*F32_TO_U8_TABLE.get_unchecked((i32_get(&tabidx, 3) - (127 - 13) * 8) as usize) as i32,
|
||||
);
|
||||
|
||||
let tabmult1 = _mm_srli_epi32(_mm_castps_si128(clamped), 12);
|
||||
let tabmult2 = _mm_and_si128(tabmult1, mant_mask_4);
|
||||
let tabmult3 = _mm_or_si128(tabmult2, top_scale_4);
|
||||
let tabprod = _mm_madd_epi16(tabval, tabmult3);
|
||||
let result = _mm_srli_epi32(tabprod, 16);
|
||||
|
||||
Color::rgba(
|
||||
i32_get(&result, 0) as u8,
|
||||
i32_get(&result, 1) as u8,
|
||||
i32_get(&result, 2) as u8,
|
||||
i32_get(&result, 3) as u8,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert from srgb in u8 0-255 to linear floating point rgb 0-1.0
|
||||
fn srgb8_to_linear_f32(val: u8) -> f32 {
|
||||
unsafe { *SRGB_TO_F32_TABLE.get_unchecked(val as usize) }
|
||||
}
|
||||
|
||||
/// A color stored as big endian bgra32
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Color(pub u32);
|
||||
|
||||
impl From<LinSrgba> for Color {
|
||||
#[inline]
|
||||
#[allow(clippy::many_single_char_names)]
|
||||
fn from(s: LinSrgba) -> Color {
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
{
|
||||
if is_x86_feature_detected!("sse2") {
|
||||
return linear_f32_to_srgb8_vec(s);
|
||||
}
|
||||
}
|
||||
|
||||
let r = linear_f32_to_srgb8_using_table(s.red);
|
||||
let g = linear_f32_to_srgb8_using_table(s.green);
|
||||
let b = linear_f32_to_srgb8_using_table(s.blue);
|
||||
let a = linear_f32_to_srgb8_using_table(s.alpha);
|
||||
Color::rgba(r, g, b, a)
|
||||
}
|
||||
fn rgb_to_linear_f32(val: u8) -> f32 {
|
||||
unsafe { *RGB_TO_F32_TABLE.get_unchecked(val as usize) }
|
||||
}
|
||||
|
||||
impl From<Srgb> for Color {
|
||||
#[inline]
|
||||
fn from(s: Srgb) -> Color {
|
||||
let b: Srgb<u8> = s.into_format();
|
||||
let b = b.into_components();
|
||||
Color::rgb(b.0, b.1, b.2)
|
||||
}
|
||||
}
|
||||
/// A pixel holding SRGBA32 data in big endian format
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct SrgbaPixel(u32);
|
||||
|
||||
impl From<Srgba> for Color {
|
||||
#[inline]
|
||||
fn from(s: Srgba) -> Color {
|
||||
let b: Srgba<u8> = s.into_format();
|
||||
let b = b.into_components();
|
||||
Color::rgba(b.0, b.1, b.2, b.3)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Color> for LinSrgb {
|
||||
#[inline]
|
||||
fn from(c: Color) -> LinSrgb {
|
||||
let c = c.as_rgba();
|
||||
LinSrgb::new(
|
||||
srgb8_to_linear_f32(c.0),
|
||||
srgb8_to_linear_f32(c.1),
|
||||
srgb8_to_linear_f32(c.2),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Color> for LinSrgba {
|
||||
#[inline]
|
||||
fn from(c: Color) -> LinSrgba {
|
||||
let c = c.as_rgba();
|
||||
LinSrgba::new(
|
||||
srgb8_to_linear_f32(c.0),
|
||||
srgb8_to_linear_f32(c.1),
|
||||
srgb8_to_linear_f32(c.2),
|
||||
srgb8_to_linear_f32(c.3),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Color> for Srgb {
|
||||
#[inline]
|
||||
fn from(c: Color) -> Srgb {
|
||||
let c = c.as_rgba();
|
||||
let s = Srgb::<u8>::new(c.0, c.1, c.2);
|
||||
s.into_format()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Color> for Srgba {
|
||||
#[inline]
|
||||
fn from(c: Color) -> Srgba {
|
||||
let c = c.as_rgba();
|
||||
let s = Srgba::<u8>::new(c.0, c.1, c.2, c.3);
|
||||
s.into_format()
|
||||
}
|
||||
}
|
||||
|
||||
impl Color {
|
||||
#[inline]
|
||||
pub fn rgb(red: u8, green: u8, blue: u8) -> Color {
|
||||
Color::rgba(red, green, blue, 0xff)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_alpha(self, alpha: u8) -> Self {
|
||||
let (r, g, b, _) = self.as_rgba();
|
||||
Self::rgba(r, g, b, alpha)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Color {
|
||||
impl SrgbaPixel {
|
||||
/// Create a pixel with the provided sRGBA values in u8 format
|
||||
pub fn rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
|
||||
#[allow(clippy::cast_lossless)]
|
||||
let word = (blue as u32) << 24 | (green as u32) << 16 | (red as u32) << 8 | alpha as u32;
|
||||
Color(word.to_be())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn with_linear_rgba_u8(red: u8, green: u8, blue: u8, alpha: u8) -> Color {
|
||||
Self::rgba(
|
||||
linear_u8_to_srgb8(red),
|
||||
linear_u8_to_srgb8(green),
|
||||
linear_u8_to_srgb8(blue),
|
||||
linear_u8_to_srgb8(alpha),
|
||||
)
|
||||
Self(word.to_be())
|
||||
}
|
||||
|
||||
/// Returns the unpacked sRGBA components as u8
|
||||
#[inline]
|
||||
pub fn as_rgba(self) -> (u8, u8, u8, u8) {
|
||||
let host = u32::from_be(self.0);
|
||||
@@ -324,42 +187,67 @@ impl Color {
|
||||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_tuple_rgba(self) -> (f32, f32, f32, f32) {
|
||||
let c: Srgba = self.into();
|
||||
c.into_format().into_components()
|
||||
/// Returns RGBA channels in linear f32 format
|
||||
pub fn to_linear(self) -> LinearRgba {
|
||||
let (r, g, b, a) = self.as_rgba();
|
||||
LinearRgba::with_srgba(r, g, b, a)
|
||||
}
|
||||
|
||||
/// Compute the composite of two colors according to the supplied operator.
|
||||
/// self is the src operand, dest is the dest operand.
|
||||
#[inline]
|
||||
pub fn composite(self, dest: Color, operator: Operator) -> Color {
|
||||
match operator {
|
||||
Operator::Over => {
|
||||
let src: LinSrgba = self.into();
|
||||
let dest: LinSrgba = dest.into();
|
||||
src.over(dest).into()
|
||||
}
|
||||
Operator::Source => self,
|
||||
Operator::Multiply => {
|
||||
let src: LinSrgba = self.into();
|
||||
let dest: LinSrgba = dest.into();
|
||||
let result: Color = src.multiply(dest).into();
|
||||
result
|
||||
}
|
||||
Operator::MultiplyThenOver(ref tint) => {
|
||||
// First multiply by the tint color. This colorizes the glyph.
|
||||
let src: LinSrgba = self.into();
|
||||
let tint: LinSrgba = (*tint).into();
|
||||
let mut tinted = src.multiply(tint);
|
||||
// We take the alpha from the source. This is important because
|
||||
// we're using Multiply to tint the glyph and if we don't reset the
|
||||
// alpha we tend to end up with a background square of the tint color.
|
||||
tinted.alpha = src.alpha;
|
||||
// Then blend the tinted glyph over the destination background
|
||||
let dest: LinSrgba = dest.into();
|
||||
tinted.over(dest).into()
|
||||
}
|
||||
}
|
||||
/// Create a pixel with the provided big-endian u32 SRGBA data
|
||||
pub fn with_srgba_u32(word: u32) -> Self {
|
||||
Self(word)
|
||||
}
|
||||
|
||||
/// Returns the underlying big-endian u32 SRGBA data
|
||||
pub fn as_srgba32(self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// A pixel value encoded as linear RGBA values in f32 format (range: 0.0-1.0)
|
||||
#[derive(Copy, Clone, Debug, Default)]
|
||||
pub struct LinearRgba(f32, f32, f32, f32);
|
||||
|
||||
impl LinearRgba {
|
||||
/// Convert SRGBA u8 components to LinearRgba.
|
||||
/// Note that alpha in SRGBA colorspace is already linear,
|
||||
/// so this only applies gamma correction to RGB.
|
||||
pub fn with_srgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
|
||||
Self(
|
||||
srgb8_to_linear_f32(red),
|
||||
srgb8_to_linear_f32(green),
|
||||
srgb8_to_linear_f32(blue),
|
||||
rgb_to_linear_f32(alpha),
|
||||
)
|
||||
}
|
||||
|
||||
/// Convert linear RGBA u8 components to LinearRgba (f32)
|
||||
pub fn with_rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
|
||||
Self(
|
||||
rgb_to_linear_f32(red),
|
||||
rgb_to_linear_f32(green),
|
||||
rgb_to_linear_f32(blue),
|
||||
rgb_to_linear_f32(alpha),
|
||||
)
|
||||
}
|
||||
|
||||
/// Create using the provided f32 components in the range 0.0-1.0
|
||||
pub fn with_components(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
|
||||
Self(red, green, blue, alpha)
|
||||
}
|
||||
|
||||
/// Convert to an SRGB u32 pixel
|
||||
pub fn srgba_pixel(self) -> SrgbaPixel {
|
||||
SrgbaPixel::rgba(
|
||||
linear_f32_to_srgb8_using_table(self.0),
|
||||
linear_f32_to_srgb8_using_table(self.1),
|
||||
linear_f32_to_srgb8_using_table(self.2),
|
||||
(self.3 * 255.) as u8,
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns the individual RGBA channels as f32 components 0.0-1.0
|
||||
pub fn tuple(self) -> (f32, f32, f32, f32) {
|
||||
(self.0, self.1, self.2, self.3)
|
||||
}
|
||||
}
|
||||
@@ -25,35 +25,11 @@ pub fn default_dpi() -> f64 {
|
||||
mod egl;
|
||||
|
||||
pub use bitmaps::{BitmapImage, Image};
|
||||
pub use color::Color;
|
||||
pub use connection::*;
|
||||
pub use glium;
|
||||
pub use os::*;
|
||||
pub use wezterm_input_types::*;
|
||||
|
||||
/// Compositing operator.
|
||||
/// We implement a small subset of possible compositing operators.
|
||||
/// More information on these and their temrinology can be found
|
||||
/// in the Cairo documentation here:
|
||||
/// https://www.cairographics.org/operators/
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Operator {
|
||||
/// Apply the alpha channel of src and combine src with dest,
|
||||
/// according to the classic OVER composite operator
|
||||
Over,
|
||||
/// Ignore dest; take src as the result of the operation
|
||||
Source,
|
||||
/// Multiply src x dest. The result is at least as dark as
|
||||
/// the darker of the two input colors. This is used to
|
||||
/// apply a color tint.
|
||||
Multiply,
|
||||
/// Multiply src with the provided color, then apply the
|
||||
/// Over operator on the result with the dest as the dest.
|
||||
/// This is used to colorize the src and then blend the
|
||||
/// result into the destination.
|
||||
MultiplyThenOver(Color),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Clipboard {
|
||||
Clipboard,
|
||||
|
||||
Reference in new issue
Block a user