aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorludovicm67 <ludovicmuller1@gmail.com>2019-03-28 20:28:53 +0100
committerQuentin Gliech <quentingliech@gmail.com>2019-05-22 20:22:35 +0200
commit9839d8bc005f78208ad3deb70a2a27028519c526 (patch)
tree6383b1bffce0026af56ba8c37a1cd1475168648c
parente3ce535705ce8c1271ce0cfd44209184b9f9f238 (diff)
downloadgit-bug-9839d8bc005f78208ad3deb70a2a27028519c526.tar.gz
core: rename Color to RGBColor
-rw-r--r--bug/label.go55
-rw-r--r--bug/label_test.go77
2 files changed, 54 insertions, 78 deletions
diff --git a/bug/label.go b/bug/label.go
index accfdfbf..f658d0b2 100644
--- a/bug/label.go
+++ b/bug/label.go
@@ -1,6 +1,7 @@
package bug
import (
+ "crypto/sha1"
"fmt"
"io"
"strings"
@@ -8,8 +9,8 @@ import (
"github.com/MichaelMure/git-bug/util/text"
)
-// Color in the form red, green, blue
-type Color struct {
+// RGBColor is a color type in the form red, green, blue
+type RGBColor struct {
red uint8
green uint8
blue uint8
@@ -21,36 +22,36 @@ func (l Label) String() string {
return string(l)
}
-// Color from a Label in a deterministic way
-func (l Label) Color() Color {
- label := string(l)
+// RGBColor from a Label computed in a deterministic way
+func (l Label) RGBColor() RGBColor {
id := 0
+ hash := sha1.Sum([]byte(l))
// colors from: https://material-ui.com/style/color/
- colors := []Color{
- Color{red: 244, green: 67, blue: 54}, // red
- Color{red: 233, green: 30, blue: 99}, // pink
- Color{red: 156, green: 39, blue: 176}, // purple
- Color{red: 103, green: 58, blue: 183}, // deepPurple
- Color{red: 63, green: 81, blue: 181}, // indigo
- Color{red: 33, green: 150, blue: 243}, // blue
- Color{red: 3, green: 169, blue: 244}, // lightBlue
- Color{red: 0, green: 188, blue: 212}, // cyan
- Color{red: 0, green: 150, blue: 136}, // teal
- Color{red: 76, green: 175, blue: 80}, // green
- Color{red: 139, green: 195, blue: 74}, // lightGreen
- Color{red: 205, green: 220, blue: 57}, // lime
- Color{red: 255, green: 235, blue: 59}, // yellow
- Color{red: 255, green: 193, blue: 7}, // amber
- Color{red: 255, green: 152, blue: 0}, // orange
- Color{red: 255, green: 87, blue: 34}, // deepOrange
- Color{red: 121, green: 85, blue: 72}, // brown
- Color{red: 158, green: 158, blue: 158}, // grey
- Color{red: 96, green: 125, blue: 139}, // blueGrey
+ colors := []RGBColor{
+ RGBColor{red: 244, green: 67, blue: 54}, // red
+ RGBColor{red: 233, green: 30, blue: 99}, // pink
+ RGBColor{red: 156, green: 39, blue: 176}, // purple
+ RGBColor{red: 103, green: 58, blue: 183}, // deepPurple
+ RGBColor{red: 63, green: 81, blue: 181}, // indigo
+ RGBColor{red: 33, green: 150, blue: 243}, // blue
+ RGBColor{red: 3, green: 169, blue: 244}, // lightBlue
+ RGBColor{red: 0, green: 188, blue: 212}, // cyan
+ RGBColor{red: 0, green: 150, blue: 136}, // teal
+ RGBColor{red: 76, green: 175, blue: 80}, // green
+ RGBColor{red: 139, green: 195, blue: 74}, // lightGreen
+ RGBColor{red: 205, green: 220, blue: 57}, // lime
+ RGBColor{red: 255, green: 235, blue: 59}, // yellow
+ RGBColor{red: 255, green: 193, blue: 7}, // amber
+ RGBColor{red: 255, green: 152, blue: 0}, // orange
+ RGBColor{red: 255, green: 87, blue: 34}, // deepOrange
+ RGBColor{red: 121, green: 85, blue: 72}, // brown
+ RGBColor{red: 158, green: 158, blue: 158}, // grey
+ RGBColor{red: 96, green: 125, blue: 139}, // blueGrey
}
- for pos, char := range label {
- id = ((pos+1)*(id+1) + int(char)) % len(colors)
+ for _, char := range hash {
+ id = (id + int(char)) % len(colors)
}
return colors[id]
diff --git a/bug/label_test.go b/bug/label_test.go
index d22f7337..2fb080c0 100644
--- a/bug/label_test.go
+++ b/bug/label_test.go
@@ -1,60 +1,35 @@
package bug
-import "testing"
-
-func TestLabelColorClassic(t *testing.T) {
- label := Label("test")
- color := label.Color()
- expected := Color{red: 244, green: 67, blue: 54}
-
- if color != expected {
- t.Errorf(
- "Got (R=%d, G=%d, B=%d) instead of (R=%d, G=%d, B=%d).",
- color.red, color.green, color.blue,
- expected.red, expected.green, expected.blue,
- )
- }
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestLabelRGBColor(t *testing.T) {
+ color := Label("test").RGBColor()
+ expected := RGBColor{red: 255, green: 87, blue: 34}
+
+ require.Equal(t, expected, color)
}
-func TestLabelColorSimilar(t *testing.T) {
- label := Label("test1")
- color := label.Color()
- expected := Color{red: 121, green: 85, blue: 72}
-
- if color != expected {
- t.Errorf(
- "Got (R=%d, G=%d, B=%d) instead of (R=%d, G=%d, B=%d).",
- color.red, color.green, color.blue,
- expected.red, expected.green, expected.blue,
- )
- }
+func TestLabelRGBColorSimilar(t *testing.T) {
+ color := Label("test1").RGBColor()
+ expected := RGBColor{red: 0, green: 188, blue: 212}
+
+ require.Equal(t, expected, color)
}
-func TestLabelColorReverse(t *testing.T) {
- label := Label("tset")
- color := label.Color()
- expected := Color{red: 158, green: 158, blue: 158}
-
- if color != expected {
- t.Errorf(
- "Got (R=%d, G=%d, B=%d) instead of (R=%d, G=%d, B=%d).",
- color.red, color.green, color.blue,
- expected.red, expected.green, expected.blue,
- )
- }
+func TestLabelRGBColorReverse(t *testing.T) {
+ color := Label("tset").RGBColor()
+ expected := RGBColor{red: 233, green: 30, blue: 99}
+
+ require.Equal(t, expected, color)
}
-func TestLabelColorEqual(t *testing.T) {
- label1 := Label("test")
- color1 := label1.Color()
- label2 := Label("test")
- color2 := label2.Color()
-
- if color1 != color2 {
- t.Errorf(
- "(R=%d, G=%d, B=%d) should be equal to (R=%d, G=%d, B=%d).",
- color1.red, color1.green, color1.blue,
- color2.red, color2.green, color2.blue,
- )
- }
+func TestLabelRGBColorEqual(t *testing.T) {
+ color1 := Label("test").RGBColor()
+ color2 := Label("test").RGBColor()
+
+ require.Equal(t, color1, color2)
}