diff options
author | Michael Muré <batolettre@gmail.com> | 2019-05-22 20:56:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-22 20:56:37 +0200 |
commit | 6e20bf0e73862854aea0cd759a880a6b5f473880 (patch) | |
tree | 54c7b0f20fa8588f79f7bd9c121630d502b5e1be /bug | |
parent | 485ca5900486b7fc3ea71cbcbb39b87272ae09fd (diff) | |
parent | aa6247ce870075922a1309718e8fafee321ef51d (diff) | |
download | git-bug-6e20bf0e73862854aea0cd759a880a6b5f473880.tar.gz |
Merge pull request #113 from ludovicm67/patch-colors
bug: add label color directly in the core
Diffstat (limited to 'bug')
-rw-r--r-- | bug/label.go | 45 | ||||
-rw-r--r-- | bug/label_test.go | 36 |
2 files changed, 68 insertions, 13 deletions
diff --git a/bug/label.go b/bug/label.go index 0ad84f22..0d6d4142 100644 --- a/bug/label.go +++ b/bug/label.go @@ -1,8 +1,9 @@ package bug import ( + "crypto/sha1" "fmt" - "io" + "image/color" "strings" "github.com/MichaelMure/git-bug/util/text" @@ -14,21 +15,39 @@ func (l Label) String() string { return string(l) } -// UnmarshalGQL implements the graphql.Unmarshaler interface -func (l *Label) UnmarshalGQL(v interface{}) error { - _, ok := v.(string) - if !ok { - return fmt.Errorf("labels must be strings") - } +// RGBA from a Label computed in a deterministic way +func (l Label) RGBA() color.RGBA { + id := 0 + hash := sha1.Sum([]byte(l)) - *l = v.(Label) + // 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 + } - return nil -} + for _, char := range hash { + id = (id + int(char)) % len(colors) + } -// MarshalGQL implements the graphql.Marshaler interface -func (l Label) MarshalGQL(w io.Writer) { - _, _ = w.Write([]byte(`"` + l.String() + `"`)) + return colors[id] } func (l Label) Validate() error { diff --git a/bug/label_test.go b/bug/label_test.go new file mode 100644 index 00000000..f87c7411 --- /dev/null +++ b/bug/label_test.go @@ -0,0 +1,36 @@ +package bug + +import ( + "image/color" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestLabelRGBA(t *testing.T) { + rgba := Label("test").RGBA() + expected := color.RGBA{R: 255, G: 87, B: 34, A: 255} + + require.Equal(t, expected, rgba) +} + +func TestLabelRGBASimilar(t *testing.T) { + rgba := Label("test1").RGBA() + expected := color.RGBA{R: 0, G: 188, B: 212, A: 255} + + require.Equal(t, expected, rgba) +} + +func TestLabelRGBAReverse(t *testing.T) { + rgba := Label("tset").RGBA() + expected := color.RGBA{R: 233, G: 30, B: 99, A: 255} + + require.Equal(t, expected, rgba) +} + +func TestLabelRGBAEqual(t *testing.T) { + color1 := Label("test").RGBA() + color2 := Label("test").RGBA() + + require.Equal(t, color1, color2) +} |