Simplified and documented examples

This commit is contained in:
Christian Muehlhaeuser
2018-05-26 07:51:03 +02:00
parent f3003012a2
commit cd51a89da5
2 changed files with 25 additions and 28 deletions

View File

@@ -31,42 +31,42 @@ var (
) )
func main() { func main() {
// Create data points in the CIE L*a*b color space
d := []kmeans.Point{} d := []kmeans.Point{}
// for l := 0; l < 255; l += 16 {
for a := 0; a < 255; a += 4 { // l for lightness channel
for b := 0; b < 255; b += 4 { // a, b for color channels
d = append(d, kmeans.Point{ for l := 30; l < 255; l += 16 {
// float64(l) / 255.0, for a := 0; a < 255; a += 16 {
float64(a) / 255.0, for b := 0; b < 255; b += 16 {
float64(b) / 255.0, d = append(d, kmeans.Point{
}) float64(l) / 255.0,
float64(a) / 255.0,
float64(b) / 255.0,
})
}
} }
} }
// }
fmt.Printf("%d data points\n", len(d))
km, err := kmeans.NewWithOptions(0.01, true)
if err != nil {
panic(err)
}
clusters, err := km.Run(d, 16)
if err != nil {
panic(err)
}
// Write HTML header
buffer := bytes.NewBuffer([]byte{}) buffer := bytes.NewBuffer([]byte{})
buffer.Write([]byte(header)) buffer.Write([]byte(header))
// Enable graph generation (.png files) for each iteration
km, _ := kmeans.NewWithOptions(0.01, true)
// Partition the color space into 16 clusters (palette colors)
clusters, _ := km.Run(d, 16)
for i, c := range clusters { for i, c := range clusters {
fmt.Printf("Cluster: %d %+v\n", i, c.Center) fmt.Printf("Cluster: %d %+v\n", i, c.Center)
col := colorful.Lab(80, -0.9+(c.Center[0]*1.8), -0.9+(c.Center[1]*1.8)) col := colorful.Lab(c.Center[0]*1.0, -0.9+(c.Center[1]*1.8), -0.9+(c.Center[2]*1.8))
// LAB: col := colorful.Lab(c.Center[0]*1.0, -0.9+(c.Center[1]*1.8), -0.9+(c.Center[2]*1.8)) fmt.Println("Color as Hex:", col.Hex())
// RGB: col := colorful.Color{R: c.Center[0], G: c.Center[1], B: c.Center[2]}
fmt.Println("Hex:", col.Hex())
buffer.Write([]byte(fmt.Sprintf(cell, col.Hex()))) buffer.Write([]byte(fmt.Sprintf(cell, col.Hex())))
} }
// Write HTML footer and generate palette.html
buffer.Write([]byte(footer)) buffer.Write([]byte(footer))
if err := ioutil.WriteFile("palette.html", buffer.Bytes(), 0644); err != nil { if err := ioutil.WriteFile("palette.html", buffer.Bytes(), 0644); err != nil {
panic(err) panic(err)

View File

@@ -21,13 +21,10 @@ func main() {
fmt.Printf("%d data points\n", len(d)) fmt.Printf("%d data points\n", len(d))
// Partition the data points into 4 clusters // Partition the data points into 4 clusters
clusters, err := km.Run(d, 4) clusters, _ := km.Run(d, 4)
if err != nil {
panic(err)
}
for i, c := range clusters { for i, c := range clusters {
fmt.Printf("Cluster: %d\n", i) fmt.Printf("Cluster: %d\n", i)
fmt.Printf("Center: X: %.2f Y: %.2f\n\n", c.Center[0]*255.0, c.Center[1]*255.0) fmt.Printf("Centered at x: %.2f y: %.2f\n", c.Center[0]*255.0, c.Center[1]*255.0)
} }
} }