diff --git a/draw/colorscale.go b/draw/colorscale.go index ae0f30e1a..89aac87bd 100644 --- a/draw/colorscale.go +++ b/draw/colorscale.go @@ -28,6 +28,12 @@ func ColorMap(min, max, value float32, colormap ...color.RGBA) color.RGBA { // get two neighboring colors i := int(index) + if i < 0 { + i = 0 + } + if i >= len(colormap)-1 { + i = len(colormap) - 2 + } c1 := colormap[i] c2 := colormap[i+1] diff --git a/engine/render.go b/engine/render.go index 54368be12..ca3e4c13e 100644 --- a/engine/render.go +++ b/engine/render.go @@ -6,6 +6,7 @@ import ( "github.com/mumax/3/draw" "image" "image/jpeg" + "math" "net/http" "sync" ) @@ -110,6 +111,9 @@ func (ren *render) render() { d := ren.imgBuf comp := ren.comp quant := ren.quant + if comp == "" { + normalize(d) + } if comp != "" && quant.NComp() > 1 { // ... if one has been selected by gui d = d.Comp(compstr[comp]) } @@ -123,3 +127,33 @@ func (ren *render) render() { } var compstr = map[string]int{"x": 0, "y": 1, "z": 2} + +func normalize(f *data.Slice) { + a := f.Vectors() + maxnorm := 0. + for i := range a[0] { + for j := range a[0][i] { + for k := range a[0][i][j] { + + x, y, z := a[0][i][j][k], a[1][i][j][k], a[2][i][j][k] + norm := math.Sqrt(float64(x*x + y*y + z*z)) + if norm > maxnorm { + maxnorm = norm + } + + } + } + } + factor := float32(1 / maxnorm) + + for i := range a[0] { + for j := range a[0][i] { + for k := range a[0][i][j] { + a[0][i][j][k] *= factor + a[1][i][j][k] *= factor + a[2][i][j][k] *= factor + + } + } + } +}