tui_color_test.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. use dioxus::prelude::*;
  2. fn main() {
  3. dioxus::tui::launch_cfg(
  4. app,
  5. dioxus::tui::Config {
  6. rendering_mode: dioxus::tui::RenderingMode::Rgb,
  7. },
  8. );
  9. }
  10. #[derive(Props, PartialEq)]
  11. struct BoxProps {
  12. x: i32,
  13. y: i32,
  14. hue: f32,
  15. alpha: f32,
  16. }
  17. fn Box(cx: Scope<BoxProps>) -> Element {
  18. let painted = use_state(&cx, || true);
  19. // use_future(&cx, (), move |_| {
  20. // let count = count.to_owned();
  21. // let update = cx.schedule_update();
  22. // async move {
  23. // loop {
  24. // count.with_mut(|i| *i += 1);
  25. // tokio::time::sleep(std::time::Duration::from_millis(800)).await;
  26. // update();
  27. // }
  28. // }
  29. // });
  30. let x = cx.props.x;
  31. let y = cx.props.y;
  32. let hue = cx.props.hue;
  33. let current_painted = painted.get();
  34. let alpha = cx.props.alpha + if *current_painted { 100.0 } else { 0.0 };
  35. cx.render(rsx! {
  36. div {
  37. left: "{x}px",
  38. top: "{y}px",
  39. width: "100%",
  40. height: "100%",
  41. background_color: "hsl({hue}, 100%, 50%, {alpha}%)",
  42. align_items: "center",
  43. onkeydown: |_| painted.with_mut(|i| *i = !*i),
  44. onmouseenter: |_| painted.with_mut(|i| *i = !*i),
  45. p{" "}
  46. }
  47. })
  48. }
  49. fn app(cx: Scope) -> Element {
  50. let steps = 50;
  51. cx.render(rsx! {
  52. div{
  53. width: "100%",
  54. height: "100%",
  55. flex_direction: "column",
  56. (0..=steps).map(|x|
  57. {
  58. let hue = x as f32*360.0/steps as f32;
  59. cx.render(rsx! {
  60. div{
  61. width: "100%",
  62. height: "100%",
  63. flex_direction: "row",
  64. (0..=steps).map(|y|
  65. {
  66. let alpha = y as f32*100.0/steps as f32;
  67. cx.render(rsx! {
  68. Box{
  69. x: x,
  70. y: y,
  71. alpha: alpha,
  72. hue: hue,
  73. }
  74. })
  75. }
  76. )
  77. }
  78. })
  79. }
  80. )
  81. }
  82. })
  83. }