From 9089d9fd88833d066f24959fba7d90a5d2d35eb3 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Wed, 14 Jun 2017 00:43:03 -0700 Subject: [PATCH] Cleaner colors example --- examples/colors.rs | 47 ++++++++++++++++++++++++++++++---------------- src/backend/blt.rs | 2 +- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/examples/colors.rs b/examples/colors.rs index 7985421..8b2e834 100644 --- a/examples/colors.rs +++ b/examples/colors.rs @@ -1,6 +1,6 @@ extern crate cursive; -use cursive::Cursive; +use cursive::{Cursive, Printer}; use cursive::theme::{ColorStyle, Color}; use cursive::view::Boxable; use cursive::views::Canvas; @@ -8,23 +8,38 @@ use cursive::views::Canvas; fn main() { let mut siv = Cursive::new(); - siv.add_layer(Canvas::new(()) - .with_draw(|printer, _| for x in 0..20 { - for y in 0..10 { - printer.with_color(ColorStyle::Custom { - front: Color::Rgb(x * 12, - y * 25, - (x + 2 * y) * 6), - back: Color::Rgb(255 - x * 12, - 255 - y * 25, - 128 + (40 - x - 2 * y) * 3), - }, - |printer| { printer.print((x, y), "+"); }); - } - }) - .fixed_size((20, 10))); + siv.add_layer(Canvas::new(()).with_draw(draw).fixed_size((20, 10))); siv.add_global_callback('q', |s| s.quit()); siv.run(); } + +fn front_color(x: u8, y: u8, x_max: u8, y_max: u8) -> Color { + Color::Rgb(x * (255 / x_max), + y * (255 / y_max), + (x + 2 * y) * (255 / (x_max + 2 * y_max))) +} + +fn back_color(x: u8, y: u8, x_max: u8, y_max: u8) -> Color { + + Color::Rgb(128 + (2 * y_max + x - 2 * y) * (128 / (x_max + 2 * y_max)), + 255 - y * (255 / y_max), + 255 - x * (255 / x_max)) +} + +fn draw(p: &Printer, _: &()) { + let x_max = p.size.x as u8; + let y_max = p.size.y as u8; + + for x in 0..x_max { + for y in 0..y_max { + let style = ColorStyle::Custom { + front: front_color(x, y, x_max, y_max), + back: back_color(x, y, x_max, y_max), + }; + + p.with_color(style, |printer| { printer.print((x, y), "+"); }); + } + } +} diff --git a/src/backend/blt.rs b/src/backend/blt.rs index a097d88..28dd1df 100644 --- a/src/backend/blt.rs +++ b/src/backend/blt.rs @@ -241,7 +241,7 @@ fn blt_keycode_to_char(kc: KeyCode, shift: bool) -> char { KeyCode::Num9 => '9', KeyCode::Num0 => '0', _ => { - println_stderr!("{:?}", kc); + println_stderr!("Found unknown input: {:?}", kc); unreachable!() } }