Skip to content

Commit

Permalink
fix(term): ansi: scale colors in RGBA
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Mar 15, 2024
1 parent c963b35 commit 89409c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 12 additions & 3 deletions exp/term/ansi/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ func (c BasicColor) RGBA() (uint32, uint32, uint32, uint32) {
}

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

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

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

// ansiToRGB converts an ANSI color to a 24-bit RGB color.
Expand Down
5 changes: 4 additions & 1 deletion exp/term/ansi/color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ func TestRGBAToHex(t *testing.T) {

for _, c := range cases {
gotR, gotG, gotB, _ := TrueColor(c.want).RGBA()
gotR /= 256
gotG /= 256
gotB /= 256
if gotR != c.r || gotG != c.g || gotB != c.b {
t.Errorf("RGBA() of TrueColor(%v): got (%v, %v, %v), want (%v, %v, %v)",
t.Errorf("RGBA() of TrueColor(%06x): got (%v, %v, %v), want (%v, %v, %v)",
c.want, gotR, gotG, gotB, c.r, c.g, c.b)
}
}
Expand Down

0 comments on commit 89409c7

Please sign in to comment.