1pub mod colorful_text {
2 #[derive(Clone)]
3 pub enum OutputFormat {
4 Ansi, EightBit, }
7
8 pub fn apply_gradient(text: &str, format: OutputFormat) -> String {
9 let mut result = String::new();
10 let hue_step = 360.0 / text.chars().count() as f32;
11
12 for (i, char) in text.chars().enumerate() {
13 let hue = (i as f32) * hue_step;
14 let base = 16; let color_index = base + ((hue / 360.0) * 216.0).floor() as u8;
17
18 let color_code = match format {
20 OutputFormat::Ansi | OutputFormat::EightBit => format!("\x1b[38;5;{}m", color_index),
21 };
22
23 result.push_str(&color_code);
24 result.push(char); }
26 result.push_str("\x1b[0m"); result
28 }
29}