changeset 4556:04eba9dc917d iso-areas

Use colors from configuration instead of random rgb values.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 01 Oct 2019 17:37:54 +0200
parents 1c5c6ffab886
children 17cba8b447a6
files cmd/isoareas/main.go pkg/models/colors.go
diffstat 2 files changed, 35 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/cmd/isoareas/main.go	Tue Oct 01 17:08:37 2019 +0200
+++ b/cmd/isoareas/main.go	Tue Oct 01 17:37:54 2019 +0200
@@ -31,6 +31,7 @@
 	svg "github.com/ajstarks/svgo"
 	"github.com/fogleman/contourmap"
 
+	"gemma.intevation.de/gemma/pkg/models"
 	"gemma.intevation.de/gemma/pkg/octree"
 )
 
@@ -99,6 +100,8 @@
 
 func dumpContoursSVG(
 	w io.Writer,
+	heights []float64,
+	colors models.ColorValues,
 	contours [][]contourmap.Contour,
 ) (err error) {
 
@@ -143,14 +146,12 @@
 	canvas := svg.New(out)
 	canvas.Start(width, height)
 
-	rnd := rand.New(rand.NewSource(42))
-
-	for _, c := range contours {
+	for j, c := range contours {
 
-		r := byte(rnd.Intn(256))
-		g := byte(rnd.Intn(256))
-		b := byte(rnd.Intn(256))
-		style := fmt.Sprintf("fill:#%02x%02x%02x", r, g, b)
+		h := heights[j]
+		col := colors.Clip(h)
+
+		style := fmt.Sprintf("fill:#%02x%02x%02x", col.R, col.G, col.B)
 
 		for _, r := range c {
 			x := make([]int, len(r))
@@ -284,10 +285,12 @@
 
 	flag.Parse()
 
-	heights, err := octree.ParseClassBreaks(classBreaks)
+	colors, err := models.ParseColorValues(classBreaks)
 	check(err)
+
+	heights := colors.Heights()
+
 	log.Printf("num classes: %d\n", len(heights))
-
 	start := time.Now()
 	xyz, err := loadXYZ(os.Stdin)
 	check(err)
@@ -423,7 +426,7 @@
 	wg.Wait()
 
 	log.Printf("Calculating contours took %v.\n", time.Since(start))
-	check(dumpContoursSVG(os.Stdout, contours))
+	check(dumpContoursSVG(os.Stdout, heights, colors, contours))
 
 	/*
 
--- a/pkg/models/colors.go	Tue Oct 01 17:08:37 2019 +0200
+++ b/pkg/models/colors.go	Tue Oct 01 17:37:54 2019 +0200
@@ -71,6 +71,28 @@
 	return cbs
 }
 
+func (cc ColorValues) Heights() []float64 {
+	heights := make([]float64, len(cc))
+	for i := range cc {
+		heights[i] = cc[i].Value
+	}
+	return heights
+}
+
+func (cc ColorValues) Clip(v float64) color.RGBA {
+	if len(cc) == 0 {
+		return color.RGBA{}
+	}
+	if v < cc[0].Value {
+		return cc[0].Color
+	}
+	if v > cc[len(cc)-1].Value {
+		return cc[len(cc)-1].Color
+	}
+	c, _ := cc.Interpolate(v)
+	return c
+}
+
 func (cc ColorValues) Interpolate(v float64) (color.RGBA, bool) {
 	if len(cc) == 0 || v < cc[0].Value || v > cc[len(cc)-1].Value {
 		return color.RGBA{}, false