Skip to content

Commit

Permalink
fix(ansi): return opaque colors
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 14, 2024
1 parent 0126bdc commit fb066ab
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions ansi/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,11 @@ const (
func (c BasicColor) RGBA() (uint32, uint32, uint32, uint32) {
ansi := uint32(c)
if ansi > 15 {
return 0, 0, 0, 0xff
return 0, 0, 0, 0xffff
}

r, g, b := ansiToRGB(ansi)
r |= r << 8
g |= g << 8
b |= b << 8
return r, g, b, 0xff00
return toRGBA(r, g, b)
}

// ExtendedColor is an ANSI 256 (8-bit) color with a value from 0 to 255.
Expand All @@ -116,10 +113,7 @@ var _ Color = ExtendedColor(0)
// satisfies the color.Color interface.
func (c ExtendedColor) RGBA() (uint32, uint32, uint32, uint32) {
r, g, b := ansiToRGB(uint32(c))
r |= r << 8
g |= g << 8
b |= b << 8
return r, g, b, 0xff00
return toRGBA(r, g, b)
}

// TrueColor is a 24-bit color that can be used in the terminal.
Expand All @@ -136,10 +130,7 @@ var _ Color = TrueColor(0)
// satisfies the color.Color interface.
func (c TrueColor) RGBA() (uint32, uint32, uint32, uint32) {
r, g, b := hexToRGB(uint32(c))
r |= r << 8
g |= g << 8
b |= b << 8
return r, g, b, 0xff00
return toRGBA(r, g, b)
}

// ansiToRGB converts an ANSI color to a 24-bit RGB color.
Expand Down Expand Up @@ -189,3 +180,17 @@ func ansiToRGB(ansi uint32) (uint32, uint32, uint32) {
func hexToRGB(hex uint32) (uint32, uint32, uint32) {
return hex >> 16, hex >> 8 & 0xff, hex & 0xff
}

// toRGBA converts an RGB 8-bit color values to 32-bit color values suitable
// for color.Color.
//
// color.Color requires 16-bit color values, so we duplicate the 8-bit values
// to fill the 16-bit values.
//
// This always returns 0xffff (opaque) for the alpha channel.
func toRGBA(r, g, b uint32) (uint32, uint32, uint32, uint32) {
r |= r << 8
g |= g << 8
b |= b << 8
return r, g, b, 0xffff
}

0 comments on commit fb066ab

Please sign in to comment.