use crate::rain::gradient;
use crate::rain::rain_drop::RainDropStyle;
use crossterm::style;
const HEAD_GREEN: u8 = 255;
const TAIL_GREEN: u8 = 10;
const HEAD: gradient::Color = gradient::Color {
r: 120,
g: 255,
b: 120,
};
#[derive(Clone, Copy)]
enum Fade {
Linear,
Quadratic,
}
fn fade_green(pos: usize, len: usize, fade: Fade) -> u8 {
let last = len.saturating_sub(1);
let t = if last == 0 {
0.0
} else {
pos.min(last) as f32 / last as f32
};
let t = match fade {
Fade::Linear => t,
Fade::Quadratic => t * t,
};
let span = f32::from(HEAD_GREEN - TAIL_GREEN);
(f32::from(HEAD_GREEN) - span * t)
.round()
.clamp(f32::from(TAIL_GREEN), f32::from(HEAD_GREEN)) as u8
}
fn ramp_at(ramp: &[gradient::Color], pos: usize) -> style::Color {
if ramp.is_empty() {
return style::Color::Reset;
}
let color = ramp[pos.min(ramp.len() - 1)];
style::Color::Rgb {
r: color.r,
g: color.g,
b: color.b,
}
}
fn head_color() -> style::Color {
style::Color::Rgb {
r: HEAD.r,
g: HEAD.g,
b: HEAD.b,
}
}
pub fn pick_style(vw_style: &RainDropStyle, pos: usize) -> style::Attribute {
match vw_style {
RainDropStyle::Front => match pos {
0..HEAD_BOLD_CELLS => style::Attribute::Bold,
_ => style::Attribute::NormalIntensity,
},
_ => style::Attribute::NormalIntensity,
}
}
const HEAD_BOLD_CELLS: usize = 4;
pub fn pick_color(
vw_style: &RainDropStyle,
pos: usize,
len: usize,
ramp: &[gradient::Color],
) -> style::Color {
match vw_style {
RainDropStyle::Gradient => {
if pos == 0 {
head_color()
} else {
style::Color::Rgb {
r: 0,
g: fade_green(pos, len, Fade::Linear),
b: 0,
}
}
}
RainDropStyle::Front => {
if pos == 0 {
head_color()
} else {
style::Color::Rgb {
r: 0,
g: fade_green(pos, len, Fade::Quadratic),
b: 0,
}
}
}
RainDropStyle::Middle => style::Color::Rgb {
r: 0,
g: (f32::from(fade_green(pos, len, Fade::Linear)) * 0.45) as u8,
b: 0,
},
RainDropStyle::Back => {
if pos == 0 {
head_color()
} else {
ramp_at(ramp, pos)
}
}
_ => style::Color::DarkGrey,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::rain::digital_rain::{DigitalRain, DigitalRainOptions};
fn get_default_rain() -> DigitalRain {
let rain_options = DigitalRainOptions {
drops_range: (10, 20),
speed_range: (2, 15),
..Default::default()
};
DigitalRain::new(rain_options, (30, 30))
}
fn green(color: style::Color) -> u8 {
match color {
style::Color::Rgb { g, .. } => g,
other => panic!("expected a truecolor green, got {other:?}"),
}
}
#[test]
fn only_the_head_of_a_drop_is_bold() {
for style in [
RainDropStyle::Front,
RainDropStyle::Middle,
RainDropStyle::Back,
RainDropStyle::Fading,
RainDropStyle::Gradient,
] {
for pos in (HEAD_BOLD_CELLS + 1)..40 {
assert_ne!(
pick_style(&style, pos),
style::Attribute::Bold,
"{} is still bold at cell {pos} of its body, so the whole drop \
is brightened rather than just its head",
style_name(&style)
);
}
}
}
#[test]
fn only_the_bright_drops_have_a_bold_head() {
for style in [RainDropStyle::Front] {
for pos in 0..HEAD_BOLD_CELLS {
assert_eq!(
pick_style(&style, pos),
style::Attribute::Bold,
"{} should have a bright head, and cell {pos} is part of it",
style_name(&style)
);
}
}
for style in [
RainDropStyle::Middle,
RainDropStyle::Back,
RainDropStyle::Fading,
RainDropStyle::Gradient,
] {
for pos in 0..40 {
assert_eq!(
pick_style(&style, pos),
style::Attribute::NormalIntensity,
"{} is a dim drop but cell {pos} is bold, so it competes with \
the front drop it is meant to sit behind",
style_name(&style)
);
}
}
}
#[test]
fn the_bold_head_is_a_small_share_of_the_longest_drop() {
let tallest = 200usize;
let longest_drop = tallest * 2 / 3;
assert!(
HEAD_BOLD_CELLS * 4 < longest_drop,
"the bold head is {HEAD_BOLD_CELLS} cells of a drop that can be \
{longest_drop} long, so most of the drop would be brightened"
);
}
#[test]
fn no_cell_is_bold_on_top_of_a_palette_index() {
let ramp = DigitalRain::build_ramp(50);
for style in RainDropStyle::ALL {
for pos in 0..40 {
let attribute = pick_style(style, pos);
if attribute == style::Attribute::Bold {
let color = pick_color(style, pos, 12, &ramp);
assert!(
matches!(color, style::Color::Rgb { .. }),
"{style:?} cell {pos} is bold on top of {color:?}, which is \
not a truecolor value. Bold on a 256-colour palette index \
is a brightening hint, and most terminals answer it by \
rendering the bright variant -- which is how a grey line \
flashes white."
);
}
}
}
}
#[test]
fn the_back_layer_is_green_rather_than_pale() {
for height in [6u16, 24, 50, 100, 200, 400] {
let ramp = DigitalRain::build_ramp(height);
for pos in 1..12 {
let style::Color::Rgb { r, g, b } =
pick_color(&RainDropStyle::Back, pos, 12, &ramp)
else {
panic!("the back layer stopped being truecolor");
};
let luminance =
0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b);
assert!(
luminance < 0.25,
"at height {height}, back cell {pos} is rgb({r},{g},{b}) at \
luminance {luminance:.2}, which reads as pale rather than as \
a receding layer"
);
assert!(
g > r && g > b,
"at height {height}, back cell {pos} is rgb({r},{g},{b}), which \
is not green-dominant, so it does not read as part of the \
matrix"
);
}
}
}
#[test]
fn the_bold_band_is_exactly_as_long_as_it_says() {
let bold_cells = (0..20)
.filter(|pos| {
pick_style(&RainDropStyle::Front, *pos) == style::Attribute::Bold
})
.count();
assert_eq!(
bold_cells, HEAD_BOLD_CELLS,
"the constant says {HEAD_BOLD_CELLS} and the band is {bold_cells} cells"
);
}
fn linear(channel: u8) -> f64 {
let c = f64::from(channel) / 255.0;
if c <= 0.04045 {
c / 12.92
} else {
((c + 0.055) / 1.055).powf(2.4)
}
}
fn style_name(style: &RainDropStyle) -> &'static str {
match style {
RainDropStyle::Front => "Front",
RainDropStyle::Middle => "Middle",
RainDropStyle::Back => "Back",
RainDropStyle::Fading => "Fading",
RainDropStyle::Gradient => "Gradient",
}
}
#[test]
fn run_loop_10_iterations() {
let mut stdout = Vec::new();
let mut digital_rain = get_default_rain();
let _ = crate::common::run_loop(&mut stdout, &mut digital_rain, Some(10));
}
#[test]
fn a_drop_tail_fades_all_the_way_down_instead_of_flatlining_bright() {
for len in [2usize, 3, 8, 20, 33] {
for style in [RainDropStyle::Gradient, RainDropStyle::Front] {
let tail: Vec<u8> = (1..len)
.map(|pos| green(pick_color(&style, pos, len, &[])))
.collect();
for pair in tail.windows(2) {
assert!(
pair[0] > pair[1],
"{style:?} at len {len}: green rose from {} to {} part way \
down the drop ({tail:?})",
pair[0],
pair[1],
);
}
assert!(
*tail.last().unwrap() < 64,
"{style:?} at len {len} ends on green {} rather than fading out \
({tail:?})",
tail.last().unwrap(),
);
}
}
}
#[test]
fn the_bright_head_is_the_brightest_cell_of_its_drop() {
for len in [2usize, 3, 5, 17, 33] {
for style in [RainDropStyle::Gradient, RainDropStyle::Front] {
let head = pick_color(&style, 0, len, &[]);
let tail = pick_color(&style, len - 1, len, &[]);
for pos in 1..len {
let g = pick_color(&style, pos, len, &[]);
assert!(
green(g) <= green(head),
"{style:?} at len {len}: cell {pos} (green {}) is brighter \
than the head ({})",
green(g),
green(head),
);
}
assert!(
green(head) > green(tail),
"{style:?} at len {len}: the head ({}) is no brighter than the \
tail ({})",
green(head),
green(tail),
);
}
}
}
#[test]
fn the_head_is_bright_without_being_white() {
for style in [
RainDropStyle::Gradient,
RainDropStyle::Front,
RainDropStyle::Back,
] {
let head =
pick_color(&style, 0, 8, &[gradient::Color { r: 9, g: 9, b: 9 }]);
assert_ne!(
head,
style::Color::White,
"{style:?} head is still plain white"
);
assert!(
matches!(head, style::Color::Rgb { .. }),
"{style:?} head is not truecolor: {head:?}"
);
}
}
#[test]
fn a_ramp_index_past_the_end_of_the_table_stays_in_range() {
let ramp = gradient::two_step_color_gradient(
gradient::Color {
r: 200,
g: 200,
b: 200,
},
gradient::Color { r: 0, g: 200, b: 0 },
gradient::Color {
r: 10,
g: 10,
b: 10,
},
4,
12,
);
let tail = ramp[ramp.len() - 1];
for pos in [ramp.len(), ramp.len() + 1, 1_000, 1_000_000, usize::MAX] {
let got = pick_color(&RainDropStyle::Back, pos, 1, &ramp);
assert_eq!(
got,
style::Color::Rgb {
r: tail.r,
g: tail.g,
b: tail.b,
},
"ramp index {pos} did not clamp to the end of the table"
);
}
assert!(green(pick_color(&RainDropStyle::Back, 1_000_000, 1, &ramp)) < 64);
}
#[test]
fn an_empty_ramp_reads_as_the_terminal_default_rather_than_panicking() {
assert_eq!(pick_color(&RainDropStyle::Back, 0, 4, &[]), head_color());
for pos in [1usize, 2, 999] {
assert_eq!(
pick_color(&RainDropStyle::Back, pos, 4, &[]),
style::Color::Reset,
"cell {pos} of a back drop with no ramp did not fall back"
);
}
}
#[test]
fn a_drop_with_no_visible_length_still_produces_a_colour() {
for style in [
RainDropStyle::Gradient,
RainDropStyle::Front,
RainDropStyle::Back,
] {
for pos in 0..4 {
let _ = pick_color(&style, pos, 0, &[]);
}
}
}
}