pub const HEIGHT: usize = 5;
const ONE: &str = r#" ██
███
██
██
██ "#;
const TWO: &str = r#"██████
██
█████
██
███████ "#;
const THREE: &str = r#"██████
██
█████
██
██████ "#;
const FOUR: &str = r#"██ ██
██ ██
███████
██
██ "#;
const FIVE: &str = r#"███████
██
███████
██
███████ "#;
const SIX: &str = r#" ██████
██
███████
██ ██
██████ "#;
const SEVEN: &str = r#"███████
██
██
██
██ "#;
const EIGHT: &str = r#" █████
██ ██
█████
██ ██
█████ "#;
const NINE: &str = r#" █████
██ ██
██████
██
█████ "#;
const ZERO: &str = r#" ██████
██ ████
██ ██ ██
████ ██
██████ "#;
const QUESTION_MARK: &str = r#"██████
██
▄███
▀▀
██ "#;
const ELLIPSIS: &str = r#"
██ ██ ██ "#;
const SMILEY_FACE: &str = r#" ██
██ ██
██
██ ██
██ "#;
fn number_to_art(number: u64) -> String {
if number == 1337 {
return QUESTION_MARK.to_string();
} else if number == 80085 {
return ELLIPSIS.to_string();
} else if number == 43770 {
return SMILEY_FACE.to_string();
}
let to_art_digit = |number: u64| match number {
0 => ZERO,
1 => ONE,
2 => TWO,
3 => THREE,
4 => FOUR,
5 => FIVE,
6 => SIX,
7 => SEVEN,
8 => EIGHT,
9 => NINE,
_ => unreachable!(),
};
let mut art = String::new();
for i in 0..HEIGHT {
for digit in number
.to_string()
.chars()
.map(|c| {
u64::from(c.to_digit(10).expect(
"couldn't convert character to digit, this should never happen",
))
})
.collect::<Vec<_>>()
{
art.push_str(
to_art_digit(digit)
.lines()
.nth(i)
.expect("invalid line from digit art, this should never happen"),
);
}
art.push('\n');
}
art.pop();
art
}
pub fn number_to_image(number: u64) -> Vec<u8> {
let art = number_to_art(number);
let mut image = vec![];
for pixel in art
.replace(' ', "a")
.replace('█', "b")
.replace('\n', "")
.as_bytes()
{
if pixel == &97 {
image.push(0);
image.push(0);
image.push(0);
image.push(0);
} else if pixel == &98 {
image.push(255u8);
image.push(255u8);
image.push(255u8);
image.push(255u8);
} else {
unreachable!();
}
}
lodepng::encode_memory(
&image,
art.lines().next().unwrap().chars().count(),
HEIGHT,
lodepng::ColorType::RGBA,
8,
)
.unwrap_or_else(|_| panic!("unable to encode digit {number}"))
}