| 1 |
package palgen |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/lucasb-eyer/go-colorful" |
| 5 |
"image/color" |
| 6 |
"sort" |
| 7 |
) |
| 8 |
|
| 9 |
// HCLSortablePalette is a slice of color.Color that can be sorted with sort.Sort, by h, c and l values |
| 10 |
type HCLSortablePalette []color.Color |
| 11 |
|
| 12 |
func (a HCLSortablePalette) Len() int { return len(a) } |
| 13 |
func (a HCLSortablePalette) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 14 |
|
| 15 |
func (a HCLSortablePalette) Less(i, j int) bool { |
| 16 |
// Create colorful.Color structs |
| 17 |
ci, _ := colorful.MakeColor(a[i]) |
| 18 |
cj, _ := colorful.MakeColor(a[j]) |
| 19 |
// Convert the colors to HCL |
| 20 |
h1, c1, l1 := ci.Hcl() |
| 21 |
h2, c2, l2 := cj.Hcl() |
| 22 |
// Compare the H, C and L values |
| 23 |
if h1 == h2 { |
| 24 |
if c1 == c2 { |
| 25 |
return l1 < l2 |
| 26 |
} |
| 27 |
return c1 < c2 |
| 28 |
} |
| 29 |
return h1 < h2 |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
// Sort the palette by luminance, hue and then chroma |
| 34 |
func Sort(pal color.Palette) { |
| 35 |
//tmp := HCLSortablePalette(pal) |
| 36 |
sort.Sort(HCLSortablePalette(pal)) |
| 37 |
//return color.Palette(tmp) |
| 38 |
} |