-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
colorsort.go
82 lines (74 loc) · 1.73 KB
/
colorsort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// This program generates an example of go-colorful's Sorted function. It
// produces an image with three stripes of color. The first is unsorted.
// The second is sorted in the CIE-L\*C\*h° space, ordered primarily by
// lightness, then by hue angle, and finally by chroma. The third is
// sorted using colorful.Sorted.
package main
import (
"image"
"image/png"
"math/rand"
"os"
"sort"
"github.com/lucasb-eyer/go-colorful"
)
// randomColors produces a slice of random colors.
func randomColors(n int) []colorful.Color {
cs := make([]colorful.Color, n)
for i := range cs {
cs[i] = colorful.Color{
R: rand.Float64(),
G: rand.Float64(),
B: rand.Float64(),
}
}
return cs
}
// drawStripes creates an image with three sets of stripes.
func drawStripes(cs1, cs2, cs3 []colorful.Color, ht, sep int) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, len(cs1), 3*ht+2*sep))
for c := range cs1 {
for r := 0; r < ht; r++ {
img.Set(c, r, cs1[c].Clamped())
img.Set(c, r+ht+sep, cs2[c].Clamped())
img.Set(c, r+(ht+sep)*2, cs3[c].Clamped())
}
}
return img
}
// writeImage writes an image to disk.
func writeImage(fn string, img image.Image) {
w, err := os.Create(fn)
if err != nil {
panic(err)
}
defer w.Close()
png.Encode(w, img)
if err != nil {
panic(err)
}
}
func main() {
n := 512
rand.Seed(8675309)
cs1 := randomColors(n)
cs2 := make([]colorful.Color, n)
copy(cs2, cs1)
sort.Slice(cs2, func(i, j int) bool {
l1, c1, h1 := cs2[i].LuvLCh()
l2, c2, h2 := cs2[j].LuvLCh()
if l1 != l2 {
return l1 < l2
}
if h1 != h2 {
return h1 < h2
}
if c1 != c2 {
return c1 < c2
}
return false
})
cs3 := colorful.Sorted(cs1)
img := drawStripes(cs1, cs2, cs3, 64, 16)
writeImage("colorsort.png", img)
}