polyvariant / colorize-scala

Scala microlibrary for ANSI colored strings.
Other
14 stars 2 forks source link

Support RGB colors #5

Closed iRevive closed 1 year ago

iRevive commented 1 year ago

While RGB coloring can be implemented within ANSI escape sequences, the terminal must satisfy some requirements: https://en.wikipedia.org/wiki/ANSI_escape_code#24-bit.

The syntax could be:

"my string".rgb(12, 258, 36)

Is this functionality viable? If so, I can work on the implementation.

kubukoz commented 1 year ago

Sure, sounds good - let's just make sure it's documented in Scaladoc that this may not work everywhere.

Do you know if it's possible to detect support for this from inside an application? It'd be useful for some sort of fallback logic. I assume this would involve some sort of effect.

btw. it can probably be done in userland even right now, with .overlay.

iRevive commented 1 year ago

I found this https://github.com/termstandard/colors#checking-for-colorterm. Seems it's possible to detect support, but options vary.

kubukoz commented 1 year ago

I think we can use System.getenv(COLORTERM) (a cross-platform version thereof) to implement something like def isTrueColor: Boolean without effects (the environment can't change between calls so I'd call it referentially transparent, although e.g. CE seems to disagree).

Let's do rgb like you've shown in the original post (unconditionally using the RGB escape codes), and we can consider conditionals in a future discussion. Currently thinking of something like this:

val red =
  if(colorize.isTrueColor) colorize.overlay(_.rgb(255,0,0))
  else colorize.overlay(_.red)

"foo".overlay(red)