...

Source file src/gonum.org/v1/plot/palette/reverse.go

Documentation: gonum.org/v1/plot/palette

     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 palette
     6  
     7  import (
     8  	"image/color"
     9  )
    10  
    11  // Reverse reverses the direction of ColorMap c.
    12  func Reverse(c ColorMap) ColorMap {
    13  	return reverse{ColorMap: c}
    14  }
    15  
    16  // reverse is a ColorMap that reverses the direction of the ColorMap it
    17  // contains.
    18  type reverse struct {
    19  	ColorMap
    20  }
    21  
    22  // At implements the ColorMap interface for a Reversed ColorMap.
    23  func (r reverse) At(v float64) (color.Color, error) {
    24  	return r.ColorMap.At(r.Max() - (v - r.Min()))
    25  }
    26  
    27  // Palette implements the ColorMap interface for a Reversed ColorMap.
    28  func (r reverse) Palette(colors int) Palette {
    29  	c := r.ColorMap.Palette(colors).Colors()
    30  	c2 := make([]color.Color, len(c))
    31  	for i, j := 0, len(c)-1; i < j; i, j = i+1, j-1 {
    32  		c2[i], c2[j] = c[j], c[i]
    33  	}
    34  	return palette(c2)
    35  }
    36  

View as plain text