Cleanup and committal of ansi table

This commit is contained in:
2022-09-27 00:53:42 -07:00
parent 1dd2bbd0e8
commit edf8f302a1
6 changed files with 151 additions and 43 deletions

View File

@@ -3,11 +3,10 @@ package main
import ( import (
"fmt" "fmt"
go_colorhash "github.com/taigrr/go-colorhash" go_colorhash "github.com/taigrr/colorhash"
) )
func main() { func main() {
x := go_colorhash.HashString("asdasd") x := go_colorhash.HashString("asdasd")
fmt.Println(x) fmt.Println(x)
} }

139
colors.go
View File

@@ -1,4 +1,4 @@
package go_colorhash package colorhash
import ( import (
"errors" "errors"
@@ -49,36 +49,53 @@ var ansi = []int{
0xFFFFFF, 0x080808, 0x121212, 0x1C1C1C, 0x262626, 0x303030, 0x3A3A3A, 0xFFFFFF, 0x080808, 0x121212, 0x1C1C1C, 0x262626, 0x303030, 0x3A3A3A,
0x444444, 0x4E4E4E, 0x585858, 0x626262, 0x6C6C6C, 0x767676, 0x808080, 0x444444, 0x4E4E4E, 0x585858, 0x626262, 0x6C6C6C, 0x767676, 0x808080,
0x8A8A8A, 0x949494, 0x9E9E9E, 0xA8A8A8, 0xB2B2B2, 0xBCBCBC, 0xC6C6C6, 0x8A8A8A, 0x949494, 0x9E9E9E, 0xA8A8A8, 0xB2B2B2, 0xBCBCBC, 0xC6C6C6,
0xD0D0D0, 0xDADADA, 0xE4E4E4, 0xEEEEEE} 0xD0D0D0, 0xDADADA, 0xE4E4E4, 0xEEEEEE,
}
type Color struct { 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 { 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 { func (c Color) GetRed() int {
return c.Value >> 16 & 0xFF return c.Hue >> 16 & 0xFF
} }
func (c Color) GetGreen() int { func (c Color) GetGreen() int {
return c.Value >> 8 & 0xFF return c.Hue >> 8 & 0xFF
} }
func (c Color) GetBlue() int { func (c Color) GetBlue() int {
return c.Value & 0xFF return c.Hue & 0xFF
} }
func CreateColor(x int) Color { func CreateColor(x int) Color {
return Color{Value: x % TotalHexColorspace} return Color{Hue: x % TotalHexColorspace}
} }
func (c Color) ToHex() string { func (c Color) ToHex() string {
return "#" + fmt.Sprintf("%06X", c.Value) return "#" + fmt.Sprintf("%06X", c.Hue)
} }
func (c Color) ToShortHex() string { func (c Color) ToShortHex() string {
value := c.Value >> 16 & 0xF value := c.Hue >> 16 & 0xF
value += c.Value >> 8 & 0xF value += c.Hue >> 8 & 0xF
value += c.Value & 0xF value += c.Hue & 0xF
return "#" + fmt.Sprintf("%06X", value) return "#" + fmt.Sprintf("%06X", value)
} }
@@ -96,17 +113,17 @@ func FromHex(h string) (c Color, err error) {
return return
} }
fmt.Println(i) 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) i, err = strconv.ParseInt(string(h[1])+string(h[1]), 16, 64)
if err != nil { if err != nil {
return return
} }
c.Value += int(i << 8) c.Hue += int(i << 8)
i, err = strconv.ParseInt(string(h[2])+string(h[2]), 16, 64) i, err = strconv.ParseInt(string(h[2])+string(h[2]), 16, 64)
if err != nil { if err != nil {
return return
} }
c.Value += int(i) c.Hue += int(i)
case 6: case 6:
var i int64 var i int64
i, err = strconv.ParseInt(string(h[0:2]), 16, 64) i, err = strconv.ParseInt(string(h[0:2]), 16, 64)
@@ -114,20 +131,104 @@ func FromHex(h string) (c Color, err error) {
return return
} }
fmt.Println(i) fmt.Println(i)
c.Value += int(i << 16) c.Hue += int(i << 16)
i, err = strconv.ParseInt(string(h[2:4]), 16, 64) i, err = strconv.ParseInt(string(h[2:4]), 16, 64)
if err != nil { if err != nil {
return return
} }
c.Value += int(i << 8) c.Hue += int(i << 8)
i, err = strconv.ParseInt(string(h[4:6]), 16, 64) i, err = strconv.ParseInt(string(h[4:6]), 16, 64)
if err != nil { if err != nil {
return return
} }
c.Value += int(i) c.Hue += int(i)
default: default:
err = errors.New("Invalid hex code length") err = errors.New("invalid hex code length")
} }
return 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
}

View File

@@ -1,4 +1,4 @@
package go_colorhash package colorhash
import ( import (
"errors" "errors"
@@ -10,20 +10,21 @@ func TestCreateColor(t *testing.T) {
ID string ID string
Value int Value int
Result 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: "WraparoundBlack", Value: TotalHexColorspace, Result: 0},
{ID: "black", Value: 0, Result: 0}, {ID: "black", Value: 0, Result: 0},
} }
for _, c := range tc { for _, c := range tc {
t.Run(c.ID, func(t *testing.T) { t.Run(c.ID, func(t *testing.T) {
color := CreateColor(c.Value) color := CreateColor(c.Value)
if color.Value != c.Result { if color.Hue != c.Result {
t.Errorf("Expected Value %d, but got %d", c.Result, color.Value) t.Errorf("Expected Value %d, but got %d", c.Result, color.Hue)
} }
}) })
} }
} }
func TestToHex(t *testing.T) { func TestToHex(t *testing.T) {
tc := []struct { tc := []struct {
ID string ID string
@@ -31,7 +32,8 @@ func TestToHex(t *testing.T) {
G int G int
B int B int
Result string 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"}, {ID: "black", R: 0, G: 0, B: 0, Result: "#000000"},
} }
for _, c := range tc { 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()) t.Errorf("Expected Value %s, but got %s", c.Result, color.ToHex())
} }
}) })
} }
} }
func TestRGB(t *testing.T) { func TestRGB(t *testing.T) {
tc := []struct { tc := []struct {
ID string ID string
@@ -51,26 +53,28 @@ func TestRGB(t *testing.T) {
G int G int
B int B int
Result 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}, {ID: "black", R: 0, G: 0, B: 0, Result: 0},
} }
for _, c := range tc { for _, c := range tc {
t.Run(c.ID, func(t *testing.T) { t.Run(c.ID, func(t *testing.T) {
color := RGB(c.R, c.G, c.B) color := RGB(c.R, c.G, c.B)
if color.Value != c.Result { if color.Hue != c.Result {
t.Errorf("Expected Value %d, but got %d", c.Result, color.Value) t.Errorf("Expected Value %d, but got %d", c.Result, color.Hue)
} }
}) })
} }
} }
func TestFromHex(t *testing.T) { func TestFromHex(t *testing.T) {
tc := []struct { tc := []struct {
ID string ID string
Input string Input string
Result string Result string
Error error 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}, {ID: "red", Input: "#F00000", Result: "#F00000", Error: nil},
} }
for _, c := range tc { 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()) t.Errorf("Expected Value %s, but got %s", c.Result, color.ToHex())
} }
}) })
} }
} }

2
go.mod
View File

@@ -1,3 +1,3 @@
module github.com/taigrr/go-colorhash module github.com/taigrr/colorhash
go 1.18 go 1.18

12
hash.go
View File

@@ -1,14 +1,15 @@
package go_colorhash package colorhash
import ( import (
"crypto/md5"
"encoding/binary" "encoding/binary"
"hash/fnv" "hash/fnv"
"io" "io"
) )
const MaxUint = ^uint(0) const (
const MaxInt = int(MaxUint >> 1) MaxUint = ^uint(0)
MaxInt = int(MaxUint >> 1)
)
func HashString(s string) int { func HashString(s string) int {
h := fnv.New64() h := fnv.New64()
@@ -22,8 +23,9 @@ func HashString(s string) int {
} }
return sint return sint
} }
func HashBytes(r io.Reader) int { func HashBytes(r io.Reader) int {
h := md5.New() h := fnv.New64()
io.Copy(h, r) io.Copy(h, r)
hashb := h.Sum(nil) hashb := h.Sum(nil)
hashb = hashb[len(hashb)-8:] hashb = hashb[len(hashb)-8:]

View File

@@ -1,4 +1,4 @@
package go_colorhash package colorhash
import ( import (
"testing" "testing"
@@ -10,15 +10,18 @@ func TestHashBytes(t *testing.T) {
}{} }{}
_ = testBytes _ = testBytes
} }
func TestHashString(t *testing.T) { func TestHashString(t *testing.T) {
testStrings := []struct { testStrings := []struct {
String string String string
Value int Value int
ID string ID string
}{{String: "", Value: 5472609002491880228, ID: "Empty string"}, }{
{String: "", Value: 5472609002491880228, ID: "Empty string"},
{String: "123", Value: 6449148174219763898, ID: "123"}, {String: "123", Value: 6449148174219763898, ID: "123"},
{String: "it's as easy as", Value: 5908178111834329190, ID: "easy"}, {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 { for _, tc := range testStrings {
t.Run(tc.ID, func(t *testing.T) { t.Run(tc.ID, func(t *testing.T) {
hash := HashString(tc.String) hash := HashString(tc.String)