bug: rename RGBA to Color

This commit is contained in:
ludovicm67 2019-08-28 21:13:45 +02:00
parent d0d9ea56b9
commit 75004e1298
No known key found for this signature in database
GPG Key ID: 4BAA8E8D43E08DB1
5 changed files with 52 additions and 33 deletions

View File

@ -576,7 +576,7 @@ func (ge *githubExporter) getOrCreateGithubLabelID(ctx context.Context, gc *gith
}
// RGBA to hex color
rgba := label.RGBA()
rgba := label.Color().RGBA()
hexColor := fmt.Sprintf("%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)

View File

@ -15,32 +15,34 @@ func (l Label) String() string {
return string(l)
}
type LabelColor color.RGBA
// RGBA from a Label computed in a deterministic way
func (l Label) RGBA() color.RGBA {
func (l Label) Color() LabelColor {
id := 0
hash := sha1.Sum([]byte(l))
// colors from: https://material-ui.com/style/color/
colors := []color.RGBA{
color.RGBA{R: 244, G: 67, B: 54, A: 255}, // red
color.RGBA{R: 233, G: 30, B: 99, A: 255}, // pink
color.RGBA{R: 156, G: 39, B: 176, A: 255}, // purple
color.RGBA{R: 103, G: 58, B: 183, A: 255}, // deepPurple
color.RGBA{R: 63, G: 81, B: 181, A: 255}, // indigo
color.RGBA{R: 33, G: 150, B: 243, A: 255}, // blue
color.RGBA{R: 3, G: 169, B: 244, A: 255}, // lightBlue
color.RGBA{R: 0, G: 188, B: 212, A: 255}, // cyan
color.RGBA{R: 0, G: 150, B: 136, A: 255}, // teal
color.RGBA{R: 76, G: 175, B: 80, A: 255}, // green
color.RGBA{R: 139, G: 195, B: 74, A: 255}, // lightGreen
color.RGBA{R: 205, G: 220, B: 57, A: 255}, // lime
color.RGBA{R: 255, G: 235, B: 59, A: 255}, // yellow
color.RGBA{R: 255, G: 193, B: 7, A: 255}, // amber
color.RGBA{R: 255, G: 152, B: 0, A: 255}, // orange
color.RGBA{R: 255, G: 87, B: 34, A: 255}, // deepOrange
color.RGBA{R: 121, G: 85, B: 72, A: 255}, // brown
color.RGBA{R: 158, G: 158, B: 158, A: 255}, // grey
color.RGBA{R: 96, G: 125, B: 139, A: 255}, // blueGrey
colors := []LabelColor{
LabelColor{R: 244, G: 67, B: 54, A: 255}, // red
LabelColor{R: 233, G: 30, B: 99, A: 255}, // pink
LabelColor{R: 156, G: 39, B: 176, A: 255}, // purple
LabelColor{R: 103, G: 58, B: 183, A: 255}, // deepPurple
LabelColor{R: 63, G: 81, B: 181, A: 255}, // indigo
LabelColor{R: 33, G: 150, B: 243, A: 255}, // blue
LabelColor{R: 3, G: 169, B: 244, A: 255}, // lightBlue
LabelColor{R: 0, G: 188, B: 212, A: 255}, // cyan
LabelColor{R: 0, G: 150, B: 136, A: 255}, // teal
LabelColor{R: 76, G: 175, B: 80, A: 255}, // green
LabelColor{R: 139, G: 195, B: 74, A: 255}, // lightGreen
LabelColor{R: 205, G: 220, B: 57, A: 255}, // lime
LabelColor{R: 255, G: 235, B: 59, A: 255}, // yellow
LabelColor{R: 255, G: 193, B: 7, A: 255}, // amber
LabelColor{R: 255, G: 152, B: 0, A: 255}, // orange
LabelColor{R: 255, G: 87, B: 34, A: 255}, // deepOrange
LabelColor{R: 121, G: 85, B: 72, A: 255}, // brown
LabelColor{R: 158, G: 158, B: 158, A: 255}, // grey
LabelColor{R: 96, G: 125, B: 139, A: 255}, // blueGrey
}
for _, char := range hash {
@ -50,15 +52,26 @@ func (l Label) RGBA() color.RGBA {
return colors[id]
}
func (l Label) Term256() int {
rgba := l.RGBA()
red := int(rgba.R) * 6 / 256
green := int(rgba.G) * 6 / 256
blue := int(rgba.B) * 6 / 256
func (lc LabelColor) RGBA() color.RGBA {
return color.RGBA(lc)
}
color256 := red*36 + green*6 + blue + 16
type Term256 int
return color256
func (lc LabelColor) Term256() Term256 {
red := Term256(lc.R) * 6 / 256
green := Term256(lc.G) * 6 / 256
blue := Term256(lc.B) * 6 / 256
return red*36 + green*6 + blue + 16
}
func (t Term256) Escape() string {
return fmt.Sprintf("\x1b[38;5;%dm", t)
}
func (t Term256) Unescape() string {
return "\x1b[0m"
}
func (l Label) Validate() error {

View File

@ -19,7 +19,7 @@ func (labelResolver) Name(ctx context.Context, obj *bug.Label) (string, error) {
}
func (labelResolver) Color(ctx context.Context, obj *bug.Label) (*color.RGBA, error) {
rgba := obj.RGBA()
rgba := obj.Color().RGBA()
return &rgba, nil
}

View File

@ -127,7 +127,12 @@ func (ls *labelSelect) layout(g *gocui.Gui) error {
if ls.labelSelect[i] {
selectBox = " [x] "
}
fmt.Fprint(v, selectBox, label)
lc := label.Color()
lc256 := lc.Term256()
labelStr := lc256.Escape() + "◼ " + lc256.Unescape() + label.String()
fmt.Fprint(v, selectBox, labelStr)
y0 += 2
}

View File

@ -429,8 +429,9 @@ func (sb *showBug) renderSidebar(g *gocui.Gui, sideView *gocui.View) error {
labelStr := make([]string, len(snap.Labels))
for i, l := range snap.Labels {
color256 := l.Term256()
labelStr[i] = fmt.Sprintf("\x1b[38;5;%dm◼\x1b[0m %s", color256, string(l))
lc := l.Color()
lc256 := lc.Term256()
labelStr[i] = lc256.Escape() + "◼ " + lc256.Unescape() + l.String()
}
labels := strings.Join(labelStr, "\n")