mirror of
https://github.com/taigrr/simplecolorpalettes.git
synced 2026-04-01 20:49:11 -07:00
stepping towards api solidification
This commit is contained in:
77
simplecolor/simplecolors.go
Normal file
77
simplecolor/simplecolors.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package simplecolor
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type SimpleColor int
|
||||
type NamedPalette map[string]SimpleColor
|
||||
type SimplePalette []SimpleColor
|
||||
type conversionPalette []color.Color
|
||||
|
||||
func (s SimplePalette) ToPalette() color.Palette {
|
||||
var x color.Palette
|
||||
for _, c := range s {
|
||||
x = append(x, SimpleColor(c))
|
||||
}
|
||||
return color.Palette(x)
|
||||
}
|
||||
|
||||
func (s SimpleColor) RGBA() (r, g, b, a uint32) {
|
||||
return uint32(s) >> 16 & 0xFF, uint32(s) >> 8 & 0xFF, uint32(s) & 0xFF, 0xFF
|
||||
}
|
||||
|
||||
func (input SimpleColor) ToAnsi16() SimpleColor {
|
||||
color := ansi[0:16].ToPalette().Convert(input)
|
||||
r, g, b, _ := color.RGBA()
|
||||
return SimpleColor(uint32(r)<<16 + uint32(g)<<8 + b)
|
||||
|
||||
}
|
||||
func (input SimpleColor) ToExtendedAnsi() SimpleColor {
|
||||
color := ansi.ToPalette().Convert(input)
|
||||
r, g, b, _ := color.RGBA()
|
||||
return SimpleColor(uint32(r)<<16 + uint32(g)<<8 + b)
|
||||
}
|
||||
|
||||
func (p SimplePalette) ToExtendedAnsi() (sp SimplePalette) {
|
||||
used := make(map[SimpleColor]bool)
|
||||
for _, x := range p {
|
||||
clampedColor := x.ToExtendedAnsi()
|
||||
if _, found := used[clampedColor]; found {
|
||||
continue
|
||||
}
|
||||
used[clampedColor] = true
|
||||
sp = append(sp, x.ToExtendedAnsi())
|
||||
}
|
||||
sort.Sort(sp)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e SimplePalette) Len() int {
|
||||
return len(e)
|
||||
}
|
||||
|
||||
func (e SimplePalette) Less(i, j int) bool {
|
||||
return int(e[i]) < int(e[j])
|
||||
}
|
||||
|
||||
func (e SimplePalette) Swap(i, j int) {
|
||||
e[i], e[j] = e[j], e[i]
|
||||
}
|
||||
|
||||
func (p SimplePalette) ToAnsi16() (sp SimplePalette) {
|
||||
used := make(map[SimpleColor]bool)
|
||||
for _, x := range p {
|
||||
clampedColor := x.ToAnsi16()
|
||||
if _, found := used[clampedColor]; found {
|
||||
continue
|
||||
}
|
||||
used[clampedColor] = true
|
||||
sp = append(sp, x.ToExtendedAnsi())
|
||||
}
|
||||
sort.Sort(sp)
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user