diff --git a/crates/edit/benches/lib.rs b/crates/edit/benches/lib.rs index 372e8ebaeba..b85787115ce 100644 --- a/crates/edit/benches/lib.rs +++ b/crates/edit/benches/lib.rs @@ -219,12 +219,12 @@ fn bench_lsh(c: &mut Criterion) { fn bench_oklab(c: &mut Criterion) { c.benchmark_group("oklab") .bench_function("StraightRgba::as_oklab", |b| { - b.iter(|| black_box(oklab::StraightRgba::from_le(0xff212cbe)).as_oklab()) + b.iter(|| black_box(oklab::StraightRgba::from_rgba(0xbe2c21ff)).as_oklab()) }) .bench_function("StraightRgba::oklab_blend", |b| { b.iter(|| { - black_box(oklab::StraightRgba::from_le(0x7f212cbe)) - .oklab_blend(black_box(oklab::StraightRgba::from_le(0x7f3aae3f))) + black_box(oklab::StraightRgba::from_rgba(0xbe2c217f)) + .oklab_blend(black_box(oklab::StraightRgba::from_rgba(0x3fae3a7f))) }) }); } diff --git a/crates/edit/src/bin/edit/main.rs b/crates/edit/src/bin/edit/main.rs index 67c2fc6961d..6bf63456584 100644 --- a/crates/edit/src/bin/edit/main.rs +++ b/crates/edit/src/bin/edit/main.rs @@ -677,11 +677,11 @@ fn setup_terminal(tui: &mut Tui, state: &mut State, vt_parser: &mut vt::Parser) // Round from 16 bits to 8 bits. val = (val * 0xff + 0x7fff) / 0xffff; } - rgb = (rgb >> 8) | ((val as u32) << 16); + rgb = (rgb << 8) | (val as u32); } } - *color = StraightRgba::from_le(rgb | 0xff000000); + *color = StraightRgba::from_rgba(rgb << 8 | 0xff); color_responses += 1; osc_buffer.clear(); } diff --git a/crates/edit/src/buffer/mod.rs b/crates/edit/src/buffer/mod.rs index f5dde4adb03..58361e20b58 100644 --- a/crates/edit/src/buffer/mod.rs +++ b/crates/edit/src/buffer/mod.rs @@ -2060,7 +2060,7 @@ impl TextBuffer { right: destination.left + self.margin_width, bottom: destination.bottom, }; - fb.blend_fg(margin, StraightRgba::from_le(0x7f7f7f7f)); + fb.blend_fg(margin, StraightRgba::from_rgba(0x7f7f7f7f)); } if self.ruler > 0 { @@ -2108,7 +2108,7 @@ impl TextBuffer { right: destination.right, bottom: cursor.y + 1, }, - StraightRgba::from_le(0x7f7f7f7f), + StraightRgba::from_rgba(0x7f7f7f7f), ); } } diff --git a/crates/edit/src/framebuffer.rs b/crates/edit/src/framebuffer.rs index f6640e344fd..7933d612530 100644 --- a/crates/edit/src/framebuffer.rs +++ b/crates/edit/src/framebuffer.rs @@ -66,25 +66,25 @@ pub const INDEXED_COLORS_COUNT: usize = 18; /// Fallback theme. Matches Windows Terminal's Ottosson theme. pub const DEFAULT_THEME: [StraightRgba; INDEXED_COLORS_COUNT] = [ - StraightRgba::from_be(0x000000ff), // Black - StraightRgba::from_be(0xbe2c21ff), // Red - StraightRgba::from_be(0x3fae3aff), // Green - StraightRgba::from_be(0xbe9a4aff), // Yellow - StraightRgba::from_be(0x204dbeff), // Blue - StraightRgba::from_be(0xbb54beff), // Magenta - StraightRgba::from_be(0x00a7b2ff), // Cyan - StraightRgba::from_be(0xbebebeff), // White - StraightRgba::from_be(0x808080ff), // BrightBlack - StraightRgba::from_be(0xff3e30ff), // BrightRed - StraightRgba::from_be(0x58ea51ff), // BrightGreen - StraightRgba::from_be(0xffc944ff), // BrightYellow - StraightRgba::from_be(0x2f6affff), // BrightBlue - StraightRgba::from_be(0xfc74ffff), // BrightMagenta - StraightRgba::from_be(0x00e1f0ff), // BrightCyan - StraightRgba::from_be(0xffffffff), // BrightWhite + StraightRgba::from_rgba(0x000000ff), // Black + StraightRgba::from_rgba(0xbe2c21ff), // Red + StraightRgba::from_rgba(0x3fae3aff), // Green + StraightRgba::from_rgba(0xbe9a4aff), // Yellow + StraightRgba::from_rgba(0x204dbeff), // Blue + StraightRgba::from_rgba(0xbb54beff), // Magenta + StraightRgba::from_rgba(0x00a7b2ff), // Cyan + StraightRgba::from_rgba(0xbebebeff), // White + StraightRgba::from_rgba(0x808080ff), // BrightBlack + StraightRgba::from_rgba(0xff3e30ff), // BrightRed + StraightRgba::from_rgba(0x58ea51ff), // BrightGreen + StraightRgba::from_rgba(0xffc944ff), // BrightYellow + StraightRgba::from_rgba(0x2f6affff), // BrightBlue + StraightRgba::from_rgba(0xfc74ffff), // BrightMagenta + StraightRgba::from_rgba(0x00e1f0ff), // BrightCyan + StraightRgba::from_rgba(0xffffffff), // BrightWhite // -------- - StraightRgba::from_be(0x000000ff), // Background - StraightRgba::from_be(0xbebebeff), // Foreground + StraightRgba::from_rgba(0x000000ff), // Background + StraightRgba::from_rgba(0xbebebeff), // Foreground ]; /// A shoddy framebuffer for terminal applications. @@ -176,7 +176,7 @@ impl Framebuffer { let front = &mut self.buffers[self.frame_counter & 1]; // Trigger a full redraw. (Yes, it's a hack.) - front.fg_bitmap.fill(StraightRgba::from_le(1)); + front.fg_bitmap.fill(StraightRgba::from_rgba(0x01000000)); // Trigger a cursor update as well, just to be sure. front.cursor = Cursor::new_invalid(); } @@ -345,21 +345,21 @@ impl Framebuffer { numerator: u32, denominator: u32, ) -> StraightRgba { - let c = self.indexed_colors[index as usize].to_le(); + let c = self.indexed_colors[index as usize].to_rgba(); let a = 255 * numerator / denominator; - StraightRgba::from_le(a << 24 | (c & 0x00ffffff)) + StraightRgba::from_rgba((c & 0xffffff00) | a) } /// Returns a color opposite to the brightness of the given `color`. pub fn contrasted(&self, color: StraightRgba) -> StraightRgba { - let idx = (color.to_ne() as usize).wrapping_mul(HASH_MULTIPLIER) >> CACHE_TABLE_SHIFT; + let idx = (color.to_rgba() as usize).wrapping_mul(HASH_MULTIPLIER) >> CACHE_TABLE_SHIFT; let slot = self.contrast_colors[idx].get(); if slot.0 == color { slot.1 } else { self.contrasted_slow(color) } } #[cold] fn contrasted_slow(&self, color: StraightRgba) -> StraightRgba { - let idx = (color.to_ne() as usize).wrapping_mul(HASH_MULTIPLIER) >> CACHE_TABLE_SHIFT; + let idx = (color.to_rgba() as usize).wrapping_mul(HASH_MULTIPLIER) >> CACHE_TABLE_SHIFT; let is_dark = color.as_oklab().lightness() < self.auto_color_threshold; let contrast = self.auto_colors[is_dark as usize]; self.contrast_colors[idx].set((color, contrast)); @@ -497,13 +497,13 @@ impl Framebuffer { && back_attr[chunk_end] == attr } {} - if last_bg != bg.to_ne() as u64 { - last_bg = bg.to_ne() as u64; + if last_bg != bg.to_rgba() as u64 { + last_bg = bg.to_rgba() as u64; self.format_color(arena, &mut result, false, bg); } - if last_fg != fg.to_ne() as u64 { - last_fg = fg.to_ne() as u64; + if last_fg != fg.to_rgba() as u64 { + last_fg = fg.to_rgba() as u64; self.format_color(arena, &mut result, true, fg); } @@ -595,7 +595,7 @@ impl Framebuffer { // the output slightly and ensures that we keep "default foreground" // and "color that happens to be default foreground" separate. // (This also applies to the background color by the way.) - if color.to_ne() == 0 { + if color.to_rgba() == 0 { arena_write_fmt!(arena, dst, "\x1b[{typ}9m"); return; } diff --git a/crates/edit/src/oklab.rs b/crates/edit/src/oklab.rs index 32ef973f763..db9bff52ea6 100644 --- a/crates/edit/src/oklab.rs +++ b/crates/edit/src/oklab.rs @@ -20,49 +20,36 @@ impl StraightRgba { StraightRgba(0) } + /// Constructs a color from a packed `0xRRGGBBAA` value, as commonly used in CSS. #[inline] - pub const fn from_le(color: u32) -> Self { - StraightRgba(u32::from_le(color)) + pub const fn from_rgba(color: u32) -> Self { + StraightRgba(color) } + /// Returns the color as a packed `0xRRGGBBAA` value, as commonly used in CSS. #[inline] - pub const fn from_be(color: u32) -> Self { - StraightRgba(u32::from_be(color)) - } - - #[inline] - pub const fn to_ne(self) -> u32 { + pub const fn to_rgba(self) -> u32 { self.0 } - #[inline] - pub const fn to_le(self) -> u32 { - self.0.to_le() - } - - #[inline] - pub const fn to_be(self) -> u32 { - self.0.to_be() - } - #[inline] pub const fn red(self) -> u32 { - self.0 & 0xff + self.0 >> 24 } #[inline] pub const fn green(self) -> u32 { - (self.0 >> 8) & 0xff + (self.0 >> 16) & 0xff } #[inline] pub const fn blue(self) -> u32 { - (self.0 >> 16) & 0xff + (self.0 >> 8) & 0xff } #[inline] pub const fn alpha(self) -> u32 { - self.0 >> 24 + self.0 & 0xff } pub fn oklab_blend(self, top: StraightRgba) -> StraightRgba { @@ -96,7 +83,7 @@ impl StraightRgba { impl Debug for StraightRgba { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "#{:08x}", self.0.to_be()) // Display as a hex color + write!(f, "#{:08x}", self.0) // Display as a hex color } } @@ -148,7 +135,7 @@ impl Oklab { let b = linear_to_srgb(b); let a = (alpha * 255.0) as u32; - StraightRgba(r | (g << 8) | (b << 16) | (a << 24)) + StraightRgba((r << 24) | (g << 16) | (b << 8) | a) } /// Porter-Duff "over" composition. It's for Lab, but it works just like with RGB. @@ -227,11 +214,21 @@ const SRGB_TO_RGB_LUT: [f32; 256] = [ mod tests { use super::*; + #[test] + fn test_channels() { + let c = StraightRgba::from_rgba(0xaabbccdd); + assert_eq!(c.red(), 0xaa); + assert_eq!(c.green(), 0xbb); + assert_eq!(c.blue(), 0xcc); + assert_eq!(c.alpha(), 0xdd); + assert_eq!(c.to_rgba(), 0xaabbccdd); + } + #[test] fn test_blending() { - let lower = StraightRgba::from_be(0x3498dbff); - let upper = StraightRgba::from_be(0xe74c3c7f); - let expected = StraightRgba::from_be(0xa67f93ff); + let lower = StraightRgba::from_rgba(0x3498dbff); + let upper = StraightRgba::from_rgba(0xe74c3c7f); + let expected = StraightRgba::from_rgba(0xa67f93ff); let blended = lower.oklab_blend(upper); assert_eq!(blended, expected); } diff --git a/crates/edit/src/tui.rs b/crates/edit/src/tui.rs index 0fb7e628bc2..841952ab135 100644 --- a/crates/edit/src/tui.rs +++ b/crates/edit/src/tui.rs @@ -1210,12 +1210,12 @@ impl Tui { result.push_str(arena, " bordered: true\r\n"); } - if node.attributes.bg.to_ne() != 0 { + if node.attributes.bg.to_rgba() != 0 { result.push_repeat(arena, ' ', depth * 2); arena_write_fmt!(arena, result, " bg: {:?}\r\n", node.attributes.bg); } - if node.attributes.fg.to_ne() != 0 { + if node.attributes.fg.to_rgba() != 0 { result.push_repeat(arena, ' ', depth * 2); arena_write_fmt!(arena, result, " fg: {:?}\r\n", node.attributes.fg); }