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() {
// Create data points in the CIE L*a*b color space
d := []kmeans.Point{}
// for l := 0; l < 255; l += 16 {
for a := 0; a < 255; a += 4 {
for b := 0; b < 255; b += 4 {
d = append(d, kmeans.Point{
// float64(l) / 255.0,
float64(a) / 255.0,
float64(b) / 255.0,
})
// l for lightness channel
// a, b for color channels
for l := 30; l < 255; l += 16 {
for a := 0; a < 255; a += 16 {
for b := 0; b < 255; b += 16 {
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.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 {
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))
// LAB: col := colorful.Lab(c.Center[0]*1.0, -0.9+(c.Center[1]*1.8), -0.9+(c.Center[2]*1.8))
// RGB: col := colorful.Color{R: c.Center[0], G: c.Center[1], B: c.Center[2]}
fmt.Println("Hex:", col.Hex())
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())
buffer.Write([]byte(fmt.Sprintf(cell, col.Hex())))
}
// Write HTML footer and generate palette.html
buffer.Write([]byte(footer))
if err := ioutil.WriteFile("palette.html", buffer.Bytes(), 0644); err != nil {
panic(err)

View File

@@ -21,13 +21,10 @@ func main() {
fmt.Printf("%d data points\n", len(d))
// Partition the data points into 4 clusters
clusters, err := km.Run(d, 4)
if err != nil {
panic(err)
}
clusters, _ := km.Run(d, 4)
for i, c := range clusters {
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)
}
}