Skip to content

Commit

Permalink
add palette_sort
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Feb 20, 2025
1 parent 3653b43 commit f1a551f
Show file tree
Hide file tree
Showing 2 changed files with 571 additions and 8 deletions.
29 changes: 21 additions & 8 deletions ansi/sixel/palette.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"image"
"image/color"
"math"
"sort"
)

// sixelPalette is a palette of up to 256 colors that lists the colors that will be used by
Expand Down Expand Up @@ -185,18 +184,32 @@ func (p *sixelPalette) quantize(uniqueColors []sixelColor, pixelCounts map[sixel

// Sort the colors in the bucket's range along the cube's longest color axis
// TODO: Use slices.SortFunc in the future
sort.SliceStable(uniqueColors[cubeToSplit.startIndex:cubeToSplit.startIndex+cubeToSplit.length],
func(i, j int) bool {
slice := uniqueColors[cubeToSplit.startIndex:cubeToSplit.startIndex+cubeToSplit.length]
// sort.SliceStable(uniqueColors[cubeToSplit.startIndex:cubeToSplit.startIndex+cubeToSplit.length],
// func(i, j int) bool {
// slice := uniqueColors[cubeToSplit.startIndex:cubeToSplit.startIndex+cubeToSplit.length]
// switch cubeToSplit.sliceChannel {
// case quantizationRed:
// return slice[i].Red < slice[j].Red
// case quantizationGreen:
// return slice[i].Green < slice[j].Green
// case quantizationBlue:
// return slice[i].Blue < slice[j].Blue
// default:
// return slice[i].Alpha < slice[j].Alpha
// }
// })

sortFunc(uniqueColors[cubeToSplit.startIndex:cubeToSplit.startIndex+cubeToSplit.length],
func(left sixelColor, right sixelColor) int {
switch cubeToSplit.sliceChannel {
case quantizationRed:
return slice[i].Red < slice[j].Red
return compare(left.Red, right.Red)
case quantizationGreen:
return slice[i].Green < slice[j].Green
return compare(left.Green, right.Green)
case quantizationBlue:
return slice[i].Blue < slice[j].Blue
return compare(left.Blue, right.Blue)
default:
return slice[i].Alpha < slice[j].Alpha
return compare(left.Alpha, right.Alpha)
}
})

Expand Down
Loading

0 comments on commit f1a551f

Please sign in to comment.