fatih / color

Color package for Go (golang)
https://pkg.go.dev/github.com/fatih/color
MIT License
7.15k stars 607 forks source link

Question: is it possible to create a custom color? #211

Open Integralist opened 8 months ago

Integralist commented 8 months ago

πŸ‘‹πŸ»

I've been through the entire README and I couldn't find any way to create my own color.

Is this possible? If so could someone explain how.

Thanks!

fatih commented 8 months ago

Hi @Integralist

We only support 4-bit ANSI color codes. No 8-bit (256 colors) or RBG (16 million colors) are supported at this time.

See https://notes.burke.libbey.me/ansi-escape-codes/ and https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences for more information.

I doubt I have the time to add support for more colors, though it's something we could pursue in the future.

Integralist commented 8 months ago

Thanks @fatih for the links.

I guess adding more colours is fine if defining your own isn't possible.

I'll see if I can understand the code and what goes into defining your own colour (and if I can figure it out I'll open a PR).

fatih commented 4 months ago

It looks like it's possible to add custom colors with RBG color codes, check this out:

https://github.com/fatih/color/issues/131#issuecomment-1905612989

I plan to add a new API or extend the Readme so people know how to use it.

Integralist commented 4 months ago

Nice!! Thanks for the update ☺️

Integralist commented 3 months ago

πŸ‘‹πŸ»

Sorry, only just circled back around to this.

What exactly are the first two inputs?

The latter three appear to be RGB from my testing but if I modify the 2nd argument at all then nothing works, and if I modify the 1st argument to be anything other than 38 or 48 again nothing works πŸ€”

I mean, it's fine, I've been able to figure out what I need to do...

c := color.New(color.Bold, 38, 2, 100, 0, 255).SprintFunc()
fmt.Printf("1: %s, 2: %s\n", c("prefix text"), "suffix text")

d := color.New(color.Bold, 48, 2, 100, 0, 255).SprintFunc()
fmt.Printf("1: %s, 2: %s\n", d("prefix text"), "suffix text")

...I'm just curious and would like to understand it.

Thanks!

fatih commented 3 months ago

Hi @Integralist

I created a PR that provides ready to use API. Could you try it out and give any feedback you have there? https://github.com/fatih/color/pull/225

Thank you!