-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: ANSI background colors not rendered correctly #153
base: main
Are you sure you want to change the base?
Conversation
c06049c
to
24065a7
Compare
case 100, 101, 102, 103, 104, 105, 106, 107: | ||
p.beginBackground(ansiPalette[v]) | ||
case 40, 41, 42, 43, 44, 45, 46, 47, 100, 101, 102, 103, 104, 105, 106, 107: | ||
p.beginBackground(ansiPalette[v-10]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, why do we need the -10
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe to account for the color indices in https://github.com/charmbracelet/freeze/blob/main/ansi.go#L190. Since 40s and 100s are the same ANSI16 colors but for background, we need to -10
to get the correct colors from ansiPalette
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep it's because the background and foreground colors are the same, but the background codes are shifted up by 10.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we pull out this magic number, i.e const backgroundColorOffset = -10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also normalize the ansiPalette
to use indices from 0-15 which imo makes more sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally, that's way clearer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that idea! Or maybe something like this?
Add a separate ansiPaletteBright
:
var ansiPalette = [8]string{
"#282a2e", // black
"#D74E6F", // red
"#31BB71", // green
"#D3E561", // yellow
"#8056FF", // blue
"#ED61D7", // magenta
"#04D7D7", // cyan
"#C5C8C6", // white
}
var ansiPaletteBright = [8]string{
"#4B4B4B", // bright black
"#FE5F86", // bright red
"#00D787", // bright green
"#EBFF71", // bright yellow
"#8F69FF", // bright blue
"#FF7AEA", // bright magenta
"#00FEFE", // bright cyan
"#FFFFFF", // bright white
}
For foreground colors:
case 30, 31, 32, 33, 34, 35, 36, 37:
span.CreateAttr("fill", ansiPalette[v%10])
p.lines[p.row].AddChild(span)
case 90, 91, 92, 93, 94, 95, 96, 97:
span.CreateAttr("fill", ansiPaletteBright[v%10])
p.lines[p.row].AddChild(span)
For background colors:
case 40, 41, 42, 43, 44, 45, 46, 47:
p.beginBackground(ansiPalette[v%10])
case 100, 101, 102, 103, 104, 105, 106, 107:
p.beginBackground(ansiPaletteBright[v%10])
I have verified this still renders the correct colors.
(Defining these as arrays is obviously unnecessary, but I opted to do that to make it clear that these only support 8 colors. I'd be fine with just using []string
if that's what you all prefer)
Currently, ANSI background colors are rendered as either gray or black since the fill is not correctly selected. This PR makes the correct color get chosen.
Before:
After:
I generated the previews with gabe565/print-xterm256-go if you want to test this change.