github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/tboxc/color_stuff.go (about)

     1  // Package tboxc contains comfort functions
     2  // for go terminal box (termbox-go).
     3  //
     4  package tboxc
     5  
     6  import "github.com/pbberlin/termbox-go"
     7  
     8  var fallbackBG = termbox.ColorBlue // "default"
     9  var brushBG = termbox.ColorBlue
    10  var fgc = termbox.ColorWhite | termbox.AttrBold
    11  
    12  var excludeColorIdxs = map[int]bool{
    13  	0:               true, // termbox.ColorDefault
    14  	1:               true, // black
    15  	int(fallbackBG): true, // 5, blue, 5+8 / 5+512 is allowed (lightblue)
    16  	16:              true, // white, equiv. to 8+512 (grey+bold)
    17  }
    18  
    19  const minColIdx = 2
    20  const maxColIdx = 16
    21  const brightThreshold = 8
    22  
    23  var revolving = minColIdx
    24  
    25  var ArticleIDsToForegrounds = map[int]termbox.Attribute{}
    26  
    27  func RevolveValidIdx() int {
    28  	revolving++
    29  	if revolving > maxColIdx {
    30  		revolving = minColIdx
    31  	}
    32  	for {
    33  		if excludeColorIdxs[revolving] {
    34  			revolving++
    35  		} else {
    36  			break
    37  		}
    38  	}
    39  	return revolving
    40  }
    41  
    42  //
    43  func ColorByInt(idx int, exclude bool) termbox.Attribute {
    44  	var newCol termbox.Attribute
    45  
    46  	cntr := 0
    47  	if exclude {
    48  		for {
    49  			remainder := idx % (maxColIdx + 1)
    50  			if excludeColorIdxs[remainder] {
    51  				increment := 1         // leads to accumulation of the first non-excluded color
    52  				increment = idx%11 + 1 // more variation, still deterministic
    53  				idx += increment
    54  			} else {
    55  				break
    56  			}
    57  			cntr++
    58  			if cntr > 100 {
    59  				panic("colors forever...")
    60  			}
    61  		}
    62  	}
    63  
    64  	idx1 := idx % (maxColIdx + 1)
    65  	idx2 := uint16(idx1)
    66  	// if idx2 >= brightThreshold{
    67  	// 	fmt.Printf("c%v++|", idx2)
    68  	// }
    69  
    70  	switch {
    71  	case idx2 <= brightThreshold:
    72  		newCol = termbox.Attribute(idx2)
    73  	default:
    74  		newCol = termbox.Attribute(idx2 - brightThreshold)
    75  		newCol = newCol | termbox.AttrBold
    76  	}
    77  	return newCol
    78  }
    79  
    80  func BrushReset() {
    81  	brushBG = fallbackBG
    82  	fgc = termbox.ColorWhite | termbox.AttrBold
    83  }
    84  
    85  func BrushByArticleID(ArticleId int) (termbox.Attribute, termbox.Attribute) {
    86  	brushBG = ColorByInt(ArticleId, true)
    87  	if establFGC, ok := ArticleIDsToForegrounds[ArticleId]; ok {
    88  		fgc = establFGC
    89  	} else {
    90  		fgc = shiftingComplement(brushBG)
    91  		ArticleIDsToForegrounds[ArticleId] = fgc
    92  	}
    93  	fgc = termbox.ColorBlack // all black
    94  	return fgc, brushBG
    95  }
    96  
    97  // Since we dont have enough background colors to differentiate
    98  // we also vary the foreground color.
    99  func shiftingComplement(bgc termbox.Attribute) (compl termbox.Attribute) {
   100  	idx := RevolveValidIdx()
   101  	if idx == int(bgc) || idx == int(bgc)-int(termbox.AttrBold) {
   102  		revolving++ // neccessary to jump over ranges of several exclusions?
   103  		idx = RevolveValidIdx()
   104  	}
   105  	compl = ColorByInt(idx, false)
   106  	return
   107  }