cursive/examples/colors.rs

51 lines
1.2 KiB
Rust
Raw Normal View History

2017-06-14 07:08:58 +00:00
extern crate cursive;
2017-06-14 07:43:03 +00:00
use cursive::{Cursive, Printer};
2017-10-12 23:38:55 +00:00
use cursive::theme::{Color, ColorStyle};
2017-06-14 07:08:58 +00:00
use cursive::view::Boxable;
use cursive::views::Canvas;
fn main() {
let mut siv = Cursive::new();
2017-06-14 07:43:03 +00:00
siv.add_layer(Canvas::new(()).with_draw(draw).fixed_size((20, 10)));
2017-06-14 07:08:58 +00:00
siv.add_global_callback('q', |s| s.quit());
siv.run();
}
2017-06-14 07:43:03 +00:00
fn front_color(x: u8, y: u8, x_max: u8, y_max: u8) -> Color {
2017-10-12 23:38:55 +00:00
Color::Rgb(
x * (255 / x_max),
y * (255 / y_max),
(x + 2 * y) * (255 / (x_max + 2 * y_max)),
)
2017-06-14 07:43:03 +00:00
}
fn back_color(x: u8, y: u8, x_max: u8, y_max: u8) -> Color {
2017-10-12 23:38:55 +00:00
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),
)
2017-06-14 07:43:03 +00:00
}
2017-09-23 20:43:02 +00:00
fn draw(_: &(), p: &Printer) {
2017-06-14 07:43:03 +00:00
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),
};
2017-10-12 23:38:55 +00:00
p.with_color(style, |printer| {
printer.print((x, y), "+");
});
2017-06-14 07:43:03 +00:00
}
}
}