Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic dimming functionality #2751

Closed
wants to merge 12 commits into from
Prev Previous commit
Next Next commit
Update helix-tui/src/buffer.rs
Co-authored-by: yvt <[email protected]>
  • Loading branch information
farwyler and yvt committed Jul 2, 2022
commit ccbd0d7131736ccebb58392c8baf05b2d5a3ff27
12 changes: 4 additions & 8 deletions helix-tui/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,10 @@ impl Buffer {
/// shade < 0 -> darken rgb color
/// shade > 0 -> lighten rgb color
pub fn dim(&mut self, area: Rect, shade: i8) {
let shade_amount = shade.unsigned_abs().saturating_mul(2);
let shade_fn = if shade < 0 {
u8::saturating_sub
} else {
u8::saturating_add
};
// TODO: replace with saturating_add_signed when mixed_integer_ops (https://github.com/rust-lang/rust/issues/87840) get stabilized.
let shaded = |v| shade_fn(v, shade_amount);
let alpha = i32::from(shade).unsigned_abs();
let (src_factor, dst_factor) = (alpha, 256 - alpha);
let src = if shade > 0 { 255u32 } else { 0u32 } * src_factor;
let shaded = |dst_color: u8| ((u32::from(dst_color) * dst_factor + src) >> 8) as u8;

let mut cache: HashMap<Color, Color> = HashMap::new();
let mut modify = |col| {
Expand Down