Animationen für die Pixeldecke
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. use std::env::args;
  2. use std::io::stdout;
  3. use std::io::Write;
  4. use std::iter::repeat;
  5. use std::thread::sleep;
  6. use std::time::Duration;
  7. use pixelfoo::color::Color;
  8. type Frame = Vec<Vec<Color>>;
  9. fn send<T: Write>(w: &mut T, f: &Frame) -> std::io::Result<()> {
  10. for l in f {
  11. for c in l {
  12. w.write_all(&c.rgb())?;
  13. }
  14. }
  15. w.flush()
  16. }
  17. const DEFAULT_LOOP_TIME: usize = 120;
  18. const MIN_LOOP_TIME: usize = 20;
  19. fn main() -> std::io::Result<()> {
  20. let args = args().collect::<Vec<_>>();
  21. eprintln!("executing {}", args[0]);
  22. let x_size = args[1].parse::<usize>().unwrap();
  23. let y_size = args[2].parse::<usize>().unwrap();
  24. let loop_time = args[3]
  25. .parse::<usize>()
  26. .unwrap_or(DEFAULT_LOOP_TIME)
  27. .max(MIN_LOOP_TIME);
  28. eprintln!(
  29. "screen size {}x{}, loop time {:?}s",
  30. x_size, y_size, loop_time
  31. );
  32. let mut border = 0;
  33. while (x_size - 2 * border) * (y_size - 2 * border) > x_size * y_size / 2 {
  34. border += 1;
  35. }
  36. let t_frame = 0.040; // s
  37. let delay = Duration::new(0, (1_000_000_000.0 * t_frame) as u32);
  38. let mut previous_color = Color::blue();
  39. let colors = vec![
  40. Color::red(),
  41. Color::yellow(),
  42. Color::green(),
  43. Color::cyan(),
  44. Color::blue(),
  45. Color::magenta(),
  46. ];
  47. let mut color_index = 0;
  48. let mut next_color = colors[color_index];
  49. // time to interpolate from one color to the next
  50. let t_interpolate = (loop_time as f64) / (colors.len() as f64); // s
  51. let delta_a = t_frame / t_interpolate;
  52. let mut a = 0.0;
  53. eprint!("delta_a {}", delta_a);
  54. loop {
  55. let c0 = previous_color.interpolate(next_color, a);
  56. let c1 = c0.complement();
  57. let mut frame = repeat(repeat(c0).take(x_size).collect::<Vec<_>>())
  58. .take(y_size)
  59. .collect::<Vec<_>>();
  60. for x in border..(x_size - border) {
  61. for y in border..(y_size - border) {
  62. frame[y][x] = c1
  63. }
  64. }
  65. a += delta_a;
  66. if a >= 1.0 {
  67. a = 0.0;
  68. color_index += 1;
  69. if color_index >= colors.len() {
  70. color_index = 0;
  71. }
  72. previous_color = next_color;
  73. next_color = colors[color_index];
  74. }
  75. let mut buf = Vec::with_capacity(x_size * y_size * 3);
  76. send(&mut buf, &frame)?;
  77. stdout().write_all(&buf)?;
  78. stdout().flush()?;
  79. sleep(delay);
  80. }
  81. }