Get rid of dependency on go-colorful

closes #84
This commit is contained in:
joeyak 2019-04-14 01:46:24 -04:00
parent 607e61234c
commit 7b5f77a14a
4 changed files with 111 additions and 18 deletions

View File

@ -1,10 +1,12 @@
package common
import (
"errors"
"fmt"
"math/rand"
"regexp"
"strconv"
"strings"
"github.com/lucasb-eyer/go-colorful"
)
// Colors holds all the valid html color names for MovieNight
@ -40,13 +42,13 @@ var Colors = []string{
}
var (
regexColor = regexp.MustCompile(`^#([0-9A-Fa-f]{3}){1,2}$`)
regexColor = regexp.MustCompile(`^([0-9A-Fa-f]{3}){1,2}$`)
)
// IsValidColor takes a string s and compares it against a list of css color names.
// It also accepts hex codes in the form of #RGB and #RRGGBB
func IsValidColor(s string) bool {
s = strings.ToLower(s)
s = strings.TrimLeft(strings.ToLower(s), "#")
for _, c := range Colors {
if s == c {
return true
@ -54,21 +56,77 @@ func IsValidColor(s string) bool {
}
if regexColor.MatchString(s) {
c, err := colorful.Hex(s)
r, g, b, err := hex(s)
if err != nil {
return false
}
total := c.R + c.G + c.B
return total > 0.7 && c.B/total < 0.7
total := float32(r + g + b)
fmt.Println(total)
fmt.Println(float32(b) / total)
return total > 0.7 && float32(b)/total < 0.7
}
return false
}
// RandomColor returns a hex color code
func RandomColor() string {
var color colorful.Color
for !IsValidColor(color.Hex()) {
color = colorful.FastHappyColor()
var color string
for !IsValidColor(color) {
color = ""
for i := 0; i < 3; i++ {
s := strconv.FormatInt(rand.Int63n(255), 16)
if len(s) == 1 {
s = "0" + s
}
color += s
}
}
return color.Hex()
return "#" + color
}
// hex returns R, G, B as values
func hex(s string) (int, int, int, error) {
// Make the string just the base16 numbers
s = strings.TrimLeft(s, "#")
if len(s) == 3 {
var err error
s, err = hexThreeToSix(s)
if err != nil {
return 0, 0, 0, err
}
}
if len(s) == 6 {
R64, err := strconv.ParseInt(s[0:2], 16, 32)
if err != nil {
return 0, 0, 0, err
}
G64, err := strconv.ParseInt(s[2:4], 16, 32)
if err != nil {
return 0, 0, 0, err
}
B64, err := strconv.ParseInt(s[4:6], 16, 32)
if err != nil {
return 0, 0, 0, err
}
return int(R64), int(G64), int(B64), nil
}
return 0, 0, 0, errors.New("incorrect format")
}
func hexThreeToSix(s string) (string, error) {
if len(s) != 3 {
return "", fmt.Errorf("%d is the incorrect length of string for convertsion", len(s))
}
h := ""
for i := 0; i < 3; i++ {
h += string(s[i])
h += string(s[i])
}
return h, nil
}

42
common/colors_test.go Normal file
View File

@ -0,0 +1,42 @@
package common
import (
"testing"
)
func TestColorHexThreeToSix(t *testing.T) {
expected := "RRGGBB"
result, _ := hexThreeToSix("RGB")
if result != expected {
t.Errorf("expected %#v, got %#v", expected, result)
}
}
func TestHex(t *testing.T) {
// The testing data layout is inputer, Expected Red, Exp Green, Exp Blue, expect error
data := [][]interface{}{
[]interface{}{"010203", 1, 2, 3, false},
[]interface{}{"100", 17, 0, 0, false},
[]interface{}{"100", 1, 0, 0, true},
[]interface{}{"1000", 0, 0, 0, true},
[]interface{}{"010203", 1, 2, 4, true},
[]interface{}{"0102GG", 1, 2, 4, true},
}
for i := range data {
input := data[i][0].(string)
r, g, b, err := hex(input)
if err != nil {
if !data[i][4].(bool) {
t.Errorf("with input %#v: %v", input, err)
}
continue
}
rr, rg, rb := data[i][1].(int), data[i][2].(int), data[i][3].(int)
if !data[i][4].(bool) && (r != rr || g != rg || b != rb) {
t.Errorf("expected %d, %d, %d - got %d, %d, %d", r, g, b, rr, rg, rb)
}
}
}

2
go.mod
View File

@ -3,10 +3,8 @@ module github.com/zorchenhimer/MovieNight
go 1.12
require (
github.com/DATA-DOG/go-sqlmock v1.3.3 // indirect
github.com/dennwc/dom v0.3.0
github.com/gorilla/sessions v1.1.3
github.com/gorilla/websocket v1.4.0
github.com/lucasb-eyer/go-colorful v1.0.1
github.com/nareix/joy4 v0.0.0-20181022032202-3ddbc8f9d431
)

5
go.sum
View File

@ -1,7 +1,5 @@
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/DATA-DOG/go-sqlmock v1.3.3 h1:CWUqKXe0s8A2z6qCgkP4Kru7wC11YoAnoupUKFDnH08=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/Microsoft/go-winio v0.4.11 h1:zoIOcVf0xPN1tnMVbTtEdI+P8OofVk3NObnwOQ6nK2Q=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
@ -44,8 +42,6 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGi
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lucasb-eyer/go-colorful v1.0.1 h1:nKJRBvZWPzvkwB4sY8A3U4zgqLf2Y9c02yzPsbXu/5c=
github.com/lucasb-eyer/go-colorful v1.0.1/go.mod h1:tLy1nWSoU0DGtxQyNRrUmb6PUiB7usbds6gd97XTXwA=
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190221075403-6243d8e04c3f h1:B6PQkurxGG1rqEX96oE14gbj8bqvYC5dtks9r5uGmlE=
github.com/mailru/easyjson v0.0.0-20190221075403-6243d8e04c3f/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
@ -80,6 +76,5 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8 h1:YoY1wS6JYVRpIfFngRf2HHo9R9dAne3xbkGOQ5rJXjU=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=