github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/colorwheel/colorwheel.go (about)

     1  package colorwheel
     2  
     3  import "github.com/henvic/wedeploycli/color"
     4  
     5  // Wheel for sequentially repeatable colors for coloring messages related
     6  // grouping by a given id - color relation
     7  type Wheel struct {
     8  	palette [][]color.Attribute
     9  	hm      map[string][]color.Attribute
    10  	next    int
    11  }
    12  
    13  // New create a color Wheel
    14  func New(palette [][]color.Attribute) Wheel {
    15  	return Wheel{
    16  		palette: palette,
    17  	}
    18  }
    19  
    20  // Get a color for a given id
    21  func (w *Wheel) Get(id string) []color.Attribute {
    22  	if w.hm == nil {
    23  		w.hm = map[string][]color.Attribute{}
    24  	}
    25  
    26  	if c, ok := w.hm[id]; ok {
    27  		return c
    28  	}
    29  
    30  	w.hm[id] = w.palette[w.next]
    31  	w.nextColor()
    32  
    33  	return w.hm[id]
    34  }
    35  
    36  func (w *Wheel) nextColor() {
    37  	if w.next == len(w.palette)-1 {
    38  		w.next = 0
    39  	} else {
    40  		w.next++
    41  	}
    42  }