Skip to content

Commit

Permalink
terminal: Make rgb_for_index take a u8 instead of a &u8 (zed-in…
Browse files Browse the repository at this point in the history
…dustries#8726)

This PR makes the `rgb_for_index` take a `u8` instead of a `&u8`.

`u8` is `Copy` and is only 1 byte, so there really isn't any reason to
pass a reference to it.

Release Notes:

- N/A
  • Loading branch information
maxdeviant authored Mar 2, 2024
1 parent 4b81b15 commit 12440d5
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions crates/terminal/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,7 @@ pub fn get_color_at_index(index: usize, theme: &Theme) -> Hsla {
15 => colors.terminal_ansi_bright_white,
// 16-231 are mapped to their RGB colors on a 0-5 range per channel
16..=231 => {
let (r, g, b) = rgb_for_index(&(index as u8)); // Split the index into its ANSI-RGB components
let (r, g, b) = rgb_for_index(index as u8); // Split the index into its ANSI-RGB components
let step = (u8::MAX as f32 / 5.).floor() as u8; // Split the RGB range into 5 chunks, with floor so no overflow
rgba_color(r * step, g * step, b * step) // Map the ANSI-RGB components to an RGB color
}
Expand Down Expand Up @@ -1542,8 +1542,8 @@ pub fn get_color_at_index(index: usize, theme: &Theme) -> Hsla {
/// ```
///
/// This function does the reverse, calculating the `r`, `g`, and `b` components from a given index.
fn rgb_for_index(i: &u8) -> (u8, u8, u8) {
debug_assert!((&16..=&231).contains(&i));
fn rgb_for_index(i: u8) -> (u8, u8, u8) {
debug_assert!((16..=231).contains(&i));
let i = i - 16;
let r = (i - (i % 36)) / 36;
let g = ((i % 36) - (i % 6)) / 6;
Expand Down Expand Up @@ -1578,7 +1578,7 @@ mod tests {
fn test_rgb_for_index() {
// Test every possible value in the color cube.
for i in 16..=231 {
let (r, g, b) = rgb_for_index(&i);
let (r, g, b) = rgb_for_index(i);
assert_eq!(i, 16 + 36 * r + 6 * g + b);
}
}
Expand Down

0 comments on commit 12440d5

Please sign in to comment.