jam1garner/owo-colors

Add support for CSS color names

mainrs opened this issue ยท 5 comments

I just saw that you already added Xterm colors. Would be cool to also have CSS color name support. Here is a complete list of all colors: https://www.w3schools.com/cssref/css_colors.asp

If you would help me out on what I'd have to add where, I'd me happy to open a PR :)

I'd definitely accept such a PR!

Some prerequisite knowledge: ANSI escape sequences pretty much come in 3 varieties (each of which becomes less and less widely supported):

  1. ANSI colors - 8 really simple colors in normal and bright variants. (Black, Red, Green, Yellow, Blue, Magenta, Cyan, White).
  2. XTerm colors - 256 different colors. in all likelihood all you'll ever need, and is decently well supported
  3. RGB colors - 48 bit color, not that widely supported.

So CSS colors (since they are specific hex codes) will need to be RGB colors. This means it's probably best to include a note in the documentation for them that they are not supported by all terminal emulators.

Anyways, onto implementation.

I think there's two main things to look at here:

  1. src/colors/xterm.rs - the xterm_colors macro is likely what you should steal most of your code from and then adapt it. it even already has (unused?) parsing for rgb values
  2. src/colors/custom.rs - since min_const_generics is going to be stabilized soon, I plan on having custom colors not be gated behind a feature anymore. All of the code there should work on next stable release

The reason custom colors matters is because it will make your implementation easier. I highly recommend you use CustomColor<const R: u8, const G: u8, const B: u8>.

I'm thinking something like, for every color:

  1. Take the name + RGB values and make a type alias like
type AliceBlue = CustomColor<240, 248, 255>;

these type aliases should likely be in a css submodule of the colors module.

  1. Create an item in an enum called CssColors which should pretty much function identically to the XtermColors enum (i.e. derive Copy, Clone, Debug, PartialEq and implement the DynColor trait)

the way Xterm implements DynColor looks something like:

XtermColors::$name => concat!("\x1b[38;5;", stringify!($xterm_num), "m"),

however you can probably just defer to something like CustomColor::<$r, $g, $b>::ANSI_FG as that should be the exact equivalent of what the concat above produces.

also minor note, but if you can I'd recommend testing against the Rust beta channel + make sure you have the custom feature enabled.

Please @ me here if you have any questions and I'll try my best to respond quickly.

I'll try to tackle it down next weekend :)

Sounds good, take your time, if anything it'll time out better if you get it done closer to min_const_generics release :P

It's worth noting that now const generics are stabilized, so my approach mentioned above should be good @sirwindfield, in the latest minor release (1.4.0) I made it no longer behind the "custom" feature

Finally had some time :) Thanks for helping me out and giving guidance for the implementation ๐Ÿš€