Skip to content

Commit 5838de1

Browse files
committed
Assign random color per cluster through random created color-palette
1 parent 2e57baf commit 5838de1

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
vendor/
3+
image-clustering

cluster.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ import (
55
"github.com/lucasb-eyer/go-colorful"
66
)
77

8-
func clusterImage(k int, img image.Image) image.Image {
8+
func clusterImage(k int, img image.Image) image.Image{
99
cls := getClusters(k, img)
10-
11-
// assign each cluster a random color
12-
for _, c := range cls {
13-
c.centroid = colorful.WarmColor() // save color in centroid
14-
}
10+
assignColors(cls)
1511

1612
// create new image
1713
newImg := image.NewRGBA(img.Bounds())
@@ -22,3 +18,11 @@ func clusterImage(k int, img image.Image) image.Image {
2218
}
2319
return newImg
2420
}
21+
22+
// assign each cluster a random color
23+
func assignColors(cls []*cluster) {
24+
pal := colorful.FastWarmPalette(len(cls))
25+
for i, c := range cls {
26+
c.centroid = pal[i]
27+
}
28+
}

0 commit comments

Comments
 (0)