fatih/color

Arbitrary RGB color support

quackduck opened this issue · 10 comments

Can we add truecolor support? The ability to specify any R, G and B value?

Let me know what you think, @fatih!

Can we please have orange? @fatih

This library can make it easy to implement: https://github.com/jwalton/gchalk (it also auto-detects terminal support levels)

I can make a PR that uses this library, but I'm not sure whether @fatih would be okay with using another color library.

fatih commented

I like the idea. @quackduck can you write how you want to tackle this? Do you plan to introduce a new API? or just change the underlying implementation? Before we go down this road, I want to first understand what the implications are, and whether it's worth adding it, and if we add, how I want to add it (I don't want to break existing code, because use the color package now for years based on the assumption that it uses ANSI escape codes).

I think a new API would probably be the best way to do this since changing the underlying implementation could break stuff. Not sure if I can make a PR in the near future, though.

24-bit true color with ANSI escape sequences can be used for encoding arbitrary colors. Refer:https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797.
For example, if you want to obtain an orange color, you can use the code color.New(38, 2, 255, 128, 0), where the first two parameters are according to ANSI standards, and the last three parameters are the corresponding RGB values of the color.

@Kevin-Rudy thanks for the tip. It never occurred to me to use the RGB colors that way, but this is nice. We could easily document this in our README, and I'll think if we can simplify it to some new functions, i.e: color.NewRGBFg(255, 128, 0) for foreground, and color.NewRGBBg for background. I don't like the name though, but I'll think about it. Meanwhile if anyone has better ideas, please let me know.

Oh here is how it looks using the code:

package main

import (
	"github.com/fatih/color"
)

func main() {
	a := color.New(38, 2, 255, 128, 0)
	a.Println("This is orange")
	a.Println("what's up")

	b := color.New(48, 2, 255, 128, 0)
	b.Println("This is orange")
	b.Println("what's up")
}

ScreenShot-2024-01-23-12 08 47@2x

I'll think if we can simplify it to some new functions, i.e: color.NewRGBFg(255, 128, 0) for foreground, and color.NewRGBBg for background.

Hey Fatih, is there any way I can help with implementing this new API? I think it can be very useful considering how widely used this package is 👍🏼

Hi @bschaatsbergen I created a PR, could you try it out and share any feedback you have? Thank you: #225