rustty icon indicating copy to clipboard operation
rustty copied to clipboard

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

Open Syntaf opened this issue 9 years ago • 3 comments

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 avatar Dec 17 '15 19:12 Syntaf

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

Syntaf avatar Dec 17 '15 19:12 Syntaf

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

bestouff avatar May 03 '16 15:05 bestouff

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.

cpjreynolds avatar May 30 '16 19:05 cpjreynolds