summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2026-06-10 07:07:38 -0700
committerRose Hogenson <rosehogenson@posteo.net>2026-06-10 07:07:38 -0700
commit08056e02cc5332b8c2ed464e19df9ab67757cd0c (patch)
tree3a150d95f22556dde66dfa037f534d969e474173
parentfc87933442e1f8fc1621c63f12d9f2ac4e693e3c (diff)
downloadsixel-08056e02cc5332b8c2ed464e19df9ab67757cd0c.tar.zst
Round for greater color accuracy
-rw-r--r--sixel.go10
1 files changed, 7 insertions, 3 deletions
diff --git a/sixel.go b/sixel.go
index 180d28c..04c500b 100644
--- a/sixel.go
+++ b/sixel.go
@@ -13,12 +13,16 @@ import (
"slices"
)
+func divRound(n, d uint32) uint32 {
+ return (n + d/2) / d
+}
+
type sixelRGB struct {
r, g, b int8
}
func (c sixelRGB) RGBA() (r, g, b, a uint32) {
- return uint32(c.r) * 0xffff / 100, uint32(c.g) * 0xffff / 100, uint32(c.b) * 0xffff / 100, 0xffff
+ return divRound(uint32(c.r)*0xffff, 100), divRound(uint32(c.g)*0xffff, 100), divRound(uint32(c.b)*0xffff, 100), 0xffff
}
var defaultSixelPalette color.Palette
@@ -27,7 +31,7 @@ func init() {
defaultSixelPalette = slices.Grow(color.Palette{color.Transparent}, len(palette.WebSafe))
for _, c := range palette.WebSafe {
r, g, b, _ := c.RGBA()
- defaultSixelPalette = append(defaultSixelPalette, sixelRGB{int8(r * 100 / 0xffff), int8(g * 100 / 0xffff), int8(b * 100 / 0xffff)})
+ defaultSixelPalette = append(defaultSixelPalette, sixelRGB{int8(divRound(r*100, 0xffff)), int8(divRound(g*100, 0xffff)), int8(divRound(b*100, 0xffff))})
}
}
@@ -40,7 +44,7 @@ func sixelizePalette(p color.Palette) color.Palette {
includeTransparent = true
continue
}
- result = append(result, sixelRGB{r: int8(r * 100 / 0xffff), g: int8(g * 100 / 0xffff), b: int8(b * 100 / 0xffff)})
+ result = append(result, sixelRGB{r: int8(divRound(r*100, 0xffff)), g: int8(divRound(g*100, 0xffff)), b: int8(divRound(b*100, 0xffff))})
}
if includeTransparent {
result = append(color.Palette{color.Transparent}, result...)