Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gabe565
Copy link

@gabe565 gabe565 commented Nov 21, 2024

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:
Example of current behavior

After:
Example with fixed background colors

I generated the previews with gabe565/print-xterm256-go if you want to test this change.

@gabe565 gabe565 changed the title fix: ANSI background colors always rendered as gray fix: ANSI background colors not rendered correctly Nov 21, 2024
@gabe565 gabe565 force-pushed the fix-ansi-background branch from c06049c to 24065a7 Compare November 21, 2024 07:41
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])
Copy link
Member

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?

Copy link
Member

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

Copy link
Author

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.

Copy link
Member

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

Copy link
Member

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

Copy link
Member

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

Copy link
Author

@gabe565 gabe565 Dec 6, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants