Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created January 22, 2024 08:26
Show Gist options
  • Save chadaustin/7046bff2261b0f669d223a88ecad8282 to your computer and use it in GitHub Desktop.
Save chadaustin/7046bff2261b0f669d223a88ecad8282 to your computer and use it in GitHub Desktop.
Tests animating the ANSI color palette
#!/usr/bin/env -S rust-script -t nightly
#![allow(dead_code)]
#![feature(unix_sigpipe)]
use std::io::Write;
fn title(t: &str) {
println!("{}\n{:-<2$}", t, "", t.len())
}
fn reset() {
print!("\x1B[m");
}
fn sgr(p: u32) {
print!("\x1B[{}m", p);
}
fn fg_index_legacy(i: u32) {
print!("\x1B[38;5;{}m", i);
}
fn bg_index_legacy(i: u32) {
print!("\x1B[48;5;{}m", i);
}
fn fg_index_standard(i: u32) {
print!("\x1B[38:5:{}m", i);
}
fn bg_index_standard(i: u32) {
print!("\x1B[48:5:{}m", i);
}
fn fg_rgb_legacy(r: u8, g: u8, b: u8) {
print!("\x1B[38;2;{};{};{}m", r, g, b);
}
fn bg_rgb_legacy(r: u8, g: u8, b: u8) {
print!("\x1B[48;2;{};{};{}m", r, g, b);
}
fn fg_rgb_standard(r: u8, g: u8, b: u8) {
print!("\x1B[38:2::{}:{}:{}m", r, g, b);
}
fn bg_rgb_standard(r: u8, g: u8, b: u8) {
print!("\x1B[48:2::{}:{}:{}m", r, g, b);
}
fn reset_fgbg() {
println!("\x1B[39;49m");
}
#[derive(Copy, Clone)]
struct RGB(u8, u8, u8);
const ANSI_COLORS: [RGB; 8] = [
RGB(0, 0, 0),
RGB(128, 0, 0),
RGB(0, 128, 0),
RGB(128, 128, 0),
RGB(0, 0, 128),
RGB(128, 0, 128),
RGB(0, 128, 128),
RGB(192, 192, 192),
];
const AIX_COLORS: [RGB; 8] = [
RGB(128, 128, 128),
RGB(255, 0, 0),
RGB(0, 255, 0),
RGB(255, 255, 0),
RGB(0, 0, 255),
RGB(255, 0, 255),
RGB(0, 255, 255),
RGB(255, 255, 255),
];
fn setcolor(i: u8, rgb: &RGB) {
print!("\x1B]4;{};rgb:{:02X}/{:02X}/{:02X}\x1B\\", i, rgb.0, rgb.1, rgb.2);
}
fn resetcolors() {
print!("\x1B]104\x1B\\");
}
const fn mk_grayscale_ramp() -> [RGB; 24] {
let mut ramp = [RGB(0, 0, 0); 24];
let mut i = 0u8;
while i < 24 {
let level: u8 = i * 10 + 8;
ramp[i as usize] = RGB(level, level, level);
i += 1;
}
ramp
}
const GRAYSCALE_RAMP: [RGB; 24] = mk_grayscale_ramp();
#[unix_sigpipe = "inherit"]
fn main() -> std::io::Result<()> {
title("Animate basic foreground");
for i in 0..8 {
sgr(30 + i);
print!(" x ");
}
reset();
println!("\n");
title("Animate basic background");
for i in 0..8 {
sgr(40 + i);
print!(" x ");
}
reset();
println!("\n");
title("Animate AIX foreground");
for i in 0..8 {
sgr(90 + i);
print!(" x ");
}
reset();
println!("\n");
title("Animate AIX background");
for i in 0..8 {
sgr(100 + i);
print!(" x ");
}
reset();
println!("\n");
title("Animate xterm-256color grayscale ramp");
for i in 0..24 {
fg_index_legacy(232 + i);
bg_index_legacy(232 + (i + 12) % 24);
print!("x");
}
reset();
println!("\n");
for i in 0..40 {
for j in 0..8 {
setcolor(j, &ANSI_COLORS[((i + (7 - j)) & 7) as usize]);
}
for j in 0..8 {
setcolor(8 + j, &AIX_COLORS[((i + (7 - j)) & 7) as usize]);
}
for j in 0..24 {
setcolor(232 + j, &GRAYSCALE_RAMP[((i + (23 - j)) % 24) as usize]);
}
// xterm doesn't animate the palette changes without output.
// WezTerm needs a new line, it seems.
print!("\0");
std::io::stdout().flush()?;
std::thread::sleep(std::time::Duration::from_millis(100));
}
resetcolors();
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment