cpjreynolds / rustty

A terminal UI library
https://docs.rs/rustty
MIT License
152 stars 14 forks source link

Color::Default is not an exclusive color, conflicts with one of the colors 0-7 #27

Open Syntaf opened 8 years ago

Syntaf commented 8 years ago

I'm running into a problem in generating a random color for my program. The problem is that when generating a random color, between 0 and 7, I have no way of ensuring this random color is not actually the terminal background because there is no comparison to Color::Default.

say I generate a random color

let color = Range::new(0, 6);
let mut rng = rand::thread_rng();

let value = color.ind_sample(&mut rng);
let rnd_color = Color::Byte(value); // great, right?

Well on a terminal with a black background, 0 cannot be used as it represents black. For a blue background, 4 cannot be used. This could be solved with a trivial comparison to the terminal background color, however there is no current way to do this because Color::Default cannot be compared with.

if rnd_color.as_byte() == Color::Default.as_byte() {  // can't do this! default will panic
    // try again
}

is there any workaround for this or am I simply out of luck in this situation?

Syntaf commented 8 years ago

I guess one workaround would be to draw the terminal with a specific cell that I can set to some standard color, say Black, then generate colors 1-7. That however would not be as ideal as working with default terminal colors

bestouff commented 8 years ago

FWIW on my terminal (gnome-terminal) the background color is a separate color from the 16 "regular" ones.

cpjreynolds commented 8 years ago

I may be misunderstanding the issue, but since Color implements Eq, you shouldn't need to compare colors by calling as_byte, simply use the equality operator.

Also, with regards to @bestouff 's point, I have the same situation with my terminal (and I would assume with most other people would too) which is that the terminal background color is not one of the "regular" ones and there really isn't any way to find out what color that is as it's specified in .Xresources and handled separately.