From edf8f302a15c072a7bf18f30c866280264e01ce5 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 27 Sep 2022 00:53:42 -0700 Subject: [PATCH] Cleanup and committal of ansi table --- cmd/main.go | 3 +- colors.go | 139 ++++++++++++++++++++++++++++++++++++++++++------- colors_test.go | 29 ++++++----- go.mod | 2 +- hash.go | 12 +++-- hash_test.go | 9 ++-- 6 files changed, 151 insertions(+), 43 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 586abd8..b2dec0b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,11 +3,10 @@ package main import ( "fmt" - go_colorhash "github.com/taigrr/go-colorhash" + go_colorhash "github.com/taigrr/colorhash" ) func main() { - x := go_colorhash.HashString("asdasd") fmt.Println(x) } diff --git a/colors.go b/colors.go index 0dfa7e0..6f06226 100644 --- a/colors.go +++ b/colors.go @@ -1,4 +1,4 @@ -package go_colorhash +package colorhash import ( "errors" @@ -49,36 +49,53 @@ var ansi = []int{ 0xFFFFFF, 0x080808, 0x121212, 0x1C1C1C, 0x262626, 0x303030, 0x3A3A3A, 0x444444, 0x4E4E4E, 0x585858, 0x626262, 0x6C6C6C, 0x767676, 0x808080, 0x8A8A8A, 0x949494, 0x9E9E9E, 0xA8A8A8, 0xB2B2B2, 0xBCBCBC, 0xC6C6C6, - 0xD0D0D0, 0xDADADA, 0xE4E4E4, 0xEEEEEE} + 0xD0D0D0, 0xDADADA, 0xE4E4E4, 0xEEEEEE, +} type Color struct { - Value int + Hue int + Alpha int +} + +func (c Color) RGBA() (r, g, b, a uint32) { + return uint32(c.GetRed()), uint32(c.GetGreen()), uint32(c.GetBlue()), uint32(c.Alpha) +} + +func RGBA(r, g, b, a int) (c Color, err error) { + if r > a { + return c, errors.New("r value is greater than a value") + } + return Color{Hue: r<<16 + g<<8 + b}, nil } func RGB(r, g, b int) Color { - return Color{Value: r<<16 + g<<8 + b} + return Color{Hue: r<<16 + g<<8 + b} } + func (c Color) GetRed() int { - return c.Value >> 16 & 0xFF + return c.Hue >> 16 & 0xFF } + func (c Color) GetGreen() int { - return c.Value >> 8 & 0xFF + return c.Hue >> 8 & 0xFF } + func (c Color) GetBlue() int { - return c.Value & 0xFF + return c.Hue & 0xFF } func CreateColor(x int) Color { - return Color{Value: x % TotalHexColorspace} + return Color{Hue: x % TotalHexColorspace} } func (c Color) ToHex() string { - return "#" + fmt.Sprintf("%06X", c.Value) + return "#" + fmt.Sprintf("%06X", c.Hue) } + func (c Color) ToShortHex() string { - value := c.Value >> 16 & 0xF - value += c.Value >> 8 & 0xF - value += c.Value & 0xF + value := c.Hue >> 16 & 0xF + value += c.Hue >> 8 & 0xF + value += c.Hue & 0xF return "#" + fmt.Sprintf("%06X", value) } @@ -96,17 +113,17 @@ func FromHex(h string) (c Color, err error) { return } fmt.Println(i) - c.Value += int(i << 16) + c.Hue += int(i << 16) i, err = strconv.ParseInt(string(h[1])+string(h[1]), 16, 64) if err != nil { return } - c.Value += int(i << 8) + c.Hue += int(i << 8) i, err = strconv.ParseInt(string(h[2])+string(h[2]), 16, 64) if err != nil { return } - c.Value += int(i) + c.Hue += int(i) case 6: var i int64 i, err = strconv.ParseInt(string(h[0:2]), 16, 64) @@ -114,20 +131,104 @@ func FromHex(h string) (c Color, err error) { return } fmt.Println(i) - c.Value += int(i << 16) + c.Hue += int(i << 16) i, err = strconv.ParseInt(string(h[2:4]), 16, 64) if err != nil { return } - c.Value += int(i << 8) + c.Hue += int(i << 8) i, err = strconv.ParseInt(string(h[4:6]), 16, 64) if err != nil { return } - c.Value += int(i) + c.Hue += int(i) default: - err = errors.New("Invalid hex code length") + err = errors.New("invalid hex code length") } return } + +var ( + Info = Teal + Warn = Yellow + Fatal = Red +) + +var ( + Black = ColorString("\033[1;30m%s\033[0m") + Red = ColorString("\033[1;31m%s\033[0m") + Green = ColorString("\033[1;32m%s\033[0m") + Yellow = ColorString("\033[1;33m%s\033[0m") + Purple = ColorString("\033[1;34m%s\033[0m") + Magenta = ColorString("\033[1;35m%s\033[0m") + Teal = ColorString("\033[0;97m%s\033[0m") + White = ColorString("\033[1;37m%s\033[0m") + // Bold + BBlack = ColorString("\033[1;30m%s\033[0m") + BRed = ColorString("\033[1;31m%s\033[0m") + BGreen = ColorString("\033[1;32m%s\033[0m") + BYellow = ColorString("\033[1;33m%s\033[0m") + BBlue = ColorString("\033[1;34m%s\033[0m") + BPurple = ColorString("\033[1;35m%s\033[0m") + BCyan = ColorString("\033[1;36m%s\033[0m") + BWhite = ColorString("\033[1;37m%s\033[0m") + + // Underline + UBlack = ColorString("\033[4;30m%s\033[0m") + URed = ColorString("\033[4;31m%s\033[0m") + UGreen = ColorString("\033[4;32m%s\033[0m") + UYellow = ColorString("\033[4;33m%s\033[0m") + UBlue = ColorString("\033[4;34m%s\033[0m") + UPurple = ColorString("\033[4;35m%s\033[0m") + UCyan = ColorString("\033[4;36m%s\033[0m") + UWhite = ColorString("\033[4;37m%s\033[0m") + + // Background + OnBlack = ColorString("\033[40m%s\033[0m") + OnRed = ColorString("\033[41m%s\033[0m") + OnGreen = ColorString("\033[42m%s\033[0m") + OnYellow = ColorString("\033[43m%s\033[0m") + OnBlue = ColorString("\033[44m%s\033[0m") + OnPurple = ColorString("\033[45m%s\033[0m") + OnCyan = ColorString("\033[46m%s\033[0m") + OnWhite = ColorString("\033[47m%s\033[0m") + + // High Intensty + IBlack = ColorString("\033[0;90m%s\033[0m") + IRed = ColorString("\033[0;91m%s\033[0m") + IGreen = ColorString("\033[0;92m%s\033[0m") + IYellow = ColorString("\033[0;93m%s\033[0m") + IBlue = ColorString("\033[0;94m%s\033[0m") + IPurple = ColorString("\033[0;95m%s\033[0m") + ICyan = ColorString("\033[0;96m%s\033[0m") + IWhite = ColorString("\033[0;97m%s\033[0m") + + // Bold High Intensty + BIBlack = ColorString("\033[1;90m%s\033[0m") + BIRed = ColorString("\033[1;91m%s\033[0m") + BIGreen = ColorString("\033[1;92m%s\033[0m") + BIYellow = ColorString("\033[1;93m%s\033[0m") + BIBlue = ColorString("\033[1;94m%s\033[0m") + BIPurple = ColorString("\033[1;95m%s\033[0m") + BICyan = ColorString("\033[1;96m%s\033[0m") + BIWhite = ColorString("\033[1;97m%s\033[0m") + + // High Intensty backgrounds + OnIBlack = ColorString("\033[0;100m%s\033[0m") + OnIRed = ColorString("\033[0;101m%s\033[0m") + OnIGreen = ColorString("\033[0;102m%s\033[0m") + OnIYellow = ColorString("\033[0;103m%s\033[0m") + OnIBlue = ColorString("\033[0;104m%s\033[0m") + OnIPurple = ColorString("\033[10;95m%s\033[0m") + OnICyan = ColorString("\033[0;106m%s\033[0m") + OnIWhite = ColorString("\033[0;107m%s\033[0m") +) + +func ColorString(colorString string) func(...interface{}) string { + sprint := func(args ...interface{}) string { + return fmt.Sprintf(colorString, + fmt.Sprint(args...)) + } + return sprint +} diff --git a/colors_test.go b/colors_test.go index 64b0e7a..412158d 100644 --- a/colors_test.go +++ b/colors_test.go @@ -1,4 +1,4 @@ -package go_colorhash +package colorhash import ( "errors" @@ -10,20 +10,21 @@ func TestCreateColor(t *testing.T) { ID string Value int Result int - }{{ID: "white", Value: TotalHexColorspace - 1, Result: TotalHexColorspace - 1}, + }{ + {ID: "white", Value: TotalHexColorspace - 1, Result: TotalHexColorspace - 1}, {ID: "WraparoundBlack", Value: TotalHexColorspace, Result: 0}, {ID: "black", Value: 0, Result: 0}, } for _, c := range tc { t.Run(c.ID, func(t *testing.T) { color := CreateColor(c.Value) - if color.Value != c.Result { - t.Errorf("Expected Value %d, but got %d", c.Result, color.Value) + if color.Hue != c.Result { + t.Errorf("Expected Value %d, but got %d", c.Result, color.Hue) } }) - } } + func TestToHex(t *testing.T) { tc := []struct { ID string @@ -31,7 +32,8 @@ func TestToHex(t *testing.T) { G int B int Result string - }{{ID: "red", R: 0xFF, G: 0x00, B: 0x00, Result: "#FF0000"}, + }{ + {ID: "red", R: 0xFF, G: 0x00, B: 0x00, Result: "#FF0000"}, {ID: "black", R: 0, G: 0, B: 0, Result: "#000000"}, } for _, c := range tc { @@ -41,9 +43,9 @@ func TestToHex(t *testing.T) { t.Errorf("Expected Value %s, but got %s", c.Result, color.ToHex()) } }) - } } + func TestRGB(t *testing.T) { tc := []struct { ID string @@ -51,26 +53,28 @@ func TestRGB(t *testing.T) { G int B int Result int - }{{ID: "white", R: 255, G: 255, B: 255, Result: TotalHexColorspace - 1}, + }{ + {ID: "white", R: 255, G: 255, B: 255, Result: TotalHexColorspace - 1}, {ID: "black", R: 0, G: 0, B: 0, Result: 0}, } for _, c := range tc { t.Run(c.ID, func(t *testing.T) { color := RGB(c.R, c.G, c.B) - if color.Value != c.Result { - t.Errorf("Expected Value %d, but got %d", c.Result, color.Value) + if color.Hue != c.Result { + t.Errorf("Expected Value %d, but got %d", c.Result, color.Hue) } }) - } } + func TestFromHex(t *testing.T) { tc := []struct { ID string Input string Result string Error error - }{{ID: "red", Input: "#F00", Result: "#FF0000", Error: nil}, + }{ + {ID: "red", Input: "#F00", Result: "#FF0000", Error: nil}, {ID: "red", Input: "#F00000", Result: "#F00000", Error: nil}, } for _, c := range tc { @@ -83,6 +87,5 @@ func TestFromHex(t *testing.T) { t.Errorf("Expected Value %s, but got %s", c.Result, color.ToHex()) } }) - } } diff --git a/go.mod b/go.mod index f128aa4..5e15704 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/taigrr/go-colorhash +module github.com/taigrr/colorhash go 1.18 diff --git a/hash.go b/hash.go index a32d61d..960f4ea 100644 --- a/hash.go +++ b/hash.go @@ -1,14 +1,15 @@ -package go_colorhash +package colorhash import ( - "crypto/md5" "encoding/binary" "hash/fnv" "io" ) -const MaxUint = ^uint(0) -const MaxInt = int(MaxUint >> 1) +const ( + MaxUint = ^uint(0) + MaxInt = int(MaxUint >> 1) +) func HashString(s string) int { h := fnv.New64() @@ -22,8 +23,9 @@ func HashString(s string) int { } return sint } + func HashBytes(r io.Reader) int { - h := md5.New() + h := fnv.New64() io.Copy(h, r) hashb := h.Sum(nil) hashb = hashb[len(hashb)-8:] diff --git a/hash_test.go b/hash_test.go index b346bc4..f147cf8 100644 --- a/hash_test.go +++ b/hash_test.go @@ -1,4 +1,4 @@ -package go_colorhash +package colorhash import ( "testing" @@ -10,15 +10,18 @@ func TestHashBytes(t *testing.T) { }{} _ = testBytes } + func TestHashString(t *testing.T) { testStrings := []struct { String string Value int ID string - }{{String: "", Value: 5472609002491880228, ID: "Empty string"}, + }{ + {String: "", Value: 5472609002491880228, ID: "Empty string"}, {String: "123", Value: 6449148174219763898, ID: "123"}, {String: "it's as easy as", Value: 5908178111834329190, ID: "easy"}, - {String: "hello colorhash", Value: 893132354324239557, ID: "hello"}} + {String: "hello colorhash", Value: 893132354324239557, ID: "hello"}, + } for _, tc := range testStrings { t.Run(tc.ID, func(t *testing.T) { hash := HashString(tc.String)