...

Source file src/gonum.org/v1/plot/plotter/palettethumbnailer.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/color"
     9  
    10  	"gonum.org/v1/plot"
    11  	"gonum.org/v1/plot/palette"
    12  	"gonum.org/v1/plot/vg"
    13  	"gonum.org/v1/plot/vg/draw"
    14  )
    15  
    16  // PaletteThumbnailers creates a slice of plot.Thumbnailers that can be used to
    17  // add legend entries for the colors in a color palette.
    18  func PaletteThumbnailers(p palette.Palette) []plot.Thumbnailer {
    19  	colors := p.Colors()
    20  	thumbnailers := make([]plot.Thumbnailer, len(colors))
    21  	for i, c := range colors {
    22  		thumbnailers[i] = paletteThumbnailer{color: c}
    23  	}
    24  	return thumbnailers
    25  }
    26  
    27  // paletteThumbnailer implements the Thumbnailer interface
    28  // for color palettes.
    29  type paletteThumbnailer struct {
    30  	color color.Color
    31  }
    32  
    33  // Thumbnail satisfies the plot.Thumbnailer interface.
    34  func (t paletteThumbnailer) Thumbnail(c *draw.Canvas) {
    35  	pts := []vg.Point{
    36  		{X: c.Min.X, Y: c.Min.Y},
    37  		{X: c.Min.X, Y: c.Max.Y},
    38  		{X: c.Max.X, Y: c.Max.Y},
    39  		{X: c.Max.X, Y: c.Min.Y},
    40  	}
    41  	poly := c.ClipPolygonY(pts)
    42  	c.FillPolygon(t.color, poly)
    43  }
    44  

View as plain text