...

Source file src/gonum.org/v1/plot/plotter/colorbar.go

Documentation: gonum.org/v1/plot/plotter

     1  // Copyright ©2017 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package plotter
     6  
     7  import (
     8  	"image"
     9  
    10  	"gonum.org/v1/plot"
    11  	"gonum.org/v1/plot/palette"
    12  	"gonum.org/v1/plot/vg/draw"
    13  )
    14  
    15  // ColorBar is a plot.Plotter that draws a color bar legend for a ColorMap.
    16  type ColorBar struct {
    17  	ColorMap palette.ColorMap
    18  
    19  	// Vertical determines wether the legend will be
    20  	// plotted vertically or horizontally.
    21  	// The default is false (horizontal).
    22  	Vertical bool
    23  
    24  	// Colors specifies the number of colors to be
    25  	// shown in the legend. If Colors is not specified,
    26  	// a default will be used.
    27  	Colors int
    28  }
    29  
    30  // colors returns the number of colors to be shown
    31  // in the legend, substituting invalid values
    32  // with the default of one color per point.
    33  func (l *ColorBar) colors(c draw.Canvas) int {
    34  	if l.Colors > 0 {
    35  		return l.Colors
    36  	}
    37  	if l.Vertical {
    38  		return int(c.Max.Y - c.Min.Y)
    39  	}
    40  	return int(c.Max.X - c.Min.X)
    41  }
    42  
    43  // check determines whether the ColorBar is
    44  // valid in its current configuration.
    45  func (l *ColorBar) check() {
    46  	if l.ColorMap == nil {
    47  		panic("plotter: nil ColorMap in ColorBar")
    48  	}
    49  	if l.ColorMap.Max() == l.ColorMap.Min() {
    50  		panic("plotter: ColorMap Max==Min")
    51  	}
    52  }
    53  
    54  // Plot implements the Plot method of the plot.Plotter interface.
    55  func (l *ColorBar) Plot(c draw.Canvas, p *plot.Plot) {
    56  	l.check()
    57  	colors := l.colors(c)
    58  	var pImg *Image
    59  	delta := (l.ColorMap.Max() - l.ColorMap.Min()) / float64(colors)
    60  	if l.Vertical {
    61  		img := image.NewNRGBA64(image.Rectangle{
    62  			Min: image.Point{X: 0, Y: 0},
    63  			Max: image.Point{X: 1, Y: colors},
    64  		})
    65  		for i := 0; i < colors; i++ {
    66  			color, err := l.ColorMap.At(l.ColorMap.Min() + delta*float64(i))
    67  			if err != nil {
    68  				panic(err)
    69  			}
    70  			img.Set(0, colors-1-i, color)
    71  		}
    72  		pImg = NewImage(img, 0, l.ColorMap.Min(), 1, l.ColorMap.Max())
    73  	} else {
    74  		img := image.NewNRGBA64(image.Rectangle{
    75  			Min: image.Point{X: 0, Y: 0},
    76  			Max: image.Point{X: colors, Y: 1},
    77  		})
    78  		for i := 0; i < colors; i++ {
    79  			color, err := l.ColorMap.At(l.ColorMap.Min() + delta*float64(i))
    80  			if err != nil {
    81  				panic(err)
    82  			}
    83  			img.Set(i, 0, color)
    84  		}
    85  		pImg = NewImage(img, l.ColorMap.Min(), 0, l.ColorMap.Max(), 1)
    86  	}
    87  	pImg.Plot(c, p)
    88  }
    89  
    90  // DataRange implements the DataRange method
    91  // of the plot.DataRanger interface.
    92  func (l *ColorBar) DataRange() (xmin, xmax, ymin, ymax float64) {
    93  	l.check()
    94  	if l.Vertical {
    95  		return 0, 1, l.ColorMap.Min(), l.ColorMap.Max()
    96  	}
    97  	return l.ColorMap.Min(), l.ColorMap.Max(), 0, 1
    98  }
    99  

View as plain text