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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ func (p *dispatcher) CsiDispatch(s ansi.CsiSequence) {
p.beginBackground(fill)
i += 3
}
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)

}
i++
}
Expand Down
Loading