mirror of
https://github.com/taigrr/colorhash.git
synced 2026-04-01 18:58:45 -07:00
Adds hash_test code
This commit is contained in:
17
README.md
Normal file
17
README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
|
||||
- Take in arbitrary input and return a deterministic color
|
||||
- Color chosen can be limited in several ways:
|
||||
- only visually / noticibly distinct colors to choose from
|
||||
- Color exclusions
|
||||
- dynamic color exclusions (optional terminal context)
|
||||
- colors within different terminal support classes (i.e. term-256)
|
||||
|
||||
- Offer to return Hex codes (6 digits or 3)
|
||||
- Offer to return ascii escape codes
|
||||
- If the input is text, offer to wrap the input text and return the output as a string
|
||||
|
||||
|
||||
1. take input as bytes
|
||||
1. md5 hash the input
|
||||
1. use modulo against the sum to choose the color to return from the subset of colors selected.
|
||||
35
hash.go
Normal file
35
hash.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package go_colorhash
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
const MaxUint = ^uint(0)
|
||||
const MaxInt = int(MaxUint >> 1)
|
||||
|
||||
func HashString(s string) int {
|
||||
h := md5.New()
|
||||
io.WriteString(h, s)
|
||||
hashb := h.Sum(nil)
|
||||
hashb = hashb[len(hashb)-8:]
|
||||
lsb := binary.BigEndian.Uint64(hashb)
|
||||
sint := int(lsb)
|
||||
if sint < 0 {
|
||||
sint = sint + MaxInt
|
||||
}
|
||||
return sint
|
||||
}
|
||||
func HashBytes(r io.Reader) int {
|
||||
h := md5.New()
|
||||
io.Copy(h, r)
|
||||
hashb := h.Sum(nil)
|
||||
hashb = hashb[len(hashb)-8:]
|
||||
lsb := binary.BigEndian.Uint64(hashb)
|
||||
sint := int(lsb)
|
||||
if sint < 0 {
|
||||
sint = sint + MaxInt
|
||||
}
|
||||
return sint
|
||||
}
|
||||
30
hash_test.go
Normal file
30
hash_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package go_colorhash
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHashBytes(t *testing.T) {
|
||||
testBytes := []struct {
|
||||
runAsUser bool
|
||||
}{}
|
||||
_ = testBytes
|
||||
}
|
||||
func TestHashString(t *testing.T) {
|
||||
testStrings := []struct {
|
||||
String string
|
||||
Value int
|
||||
ID string
|
||||
}{{String: "", Value: 7602086723416769149, ID: "Empty string"},
|
||||
{String: "123", Value: 1606385479620709231, ID: "123"},
|
||||
{String: "it's as easy as", Value: 5377981271559288604, ID: "easy"},
|
||||
{String: "hello colorhash", Value: 4155814819593785823, ID: "hello"}}
|
||||
for _, tc := range testStrings {
|
||||
t.Run(tc.ID, func(t *testing.T) {
|
||||
hash := HashString(tc.String)
|
||||
if hash != tc.Value {
|
||||
t.Errorf("%s :: Hash resulted in value %d, but expected value is %d", tc.ID, hash, tc.Value)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user