fatih / color

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

Arbitrary RGB color support #131

Open quackduck opened 3 years ago

quackduck commented 3 years ago

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

quackduck commented 3 years ago

Let me know what you think, @fatih!

regnaio commented 3 years ago

Can we please have orange? @fatih

quackduck commented 3 years ago

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 1 year ago

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).

quackduck commented 1 year ago

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.

Kevin-Rudy commented 7 months ago

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.

fatih commented 7 months ago

@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.

fatih commented 7 months ago

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

bschaatsbergen commented 5 months ago

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 👍🏼

fatih commented 5 months ago

Hi @bschaatsbergen I created a PR, could you try it out and share any feedback you have? Thank you: https://github.com/fatih/color/pull/225