in-progress commit for hash functionality extension

This commit is contained in:
2022-10-11 13:21:12 -07:00
parent 5c6cb8e0c9
commit 50823c20b8
5 changed files with 62 additions and 9 deletions

View File

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

View File

@@ -2,6 +2,9 @@ package colorhash
import (
"fmt"
"image/color"
"github.com/taigrr/simplecolorpalettes/simplecolor"
)
var (
@@ -80,10 +83,62 @@ var (
OnIWhite = ColorString("\033[0;107m%s\033[0m")
)
func ColorString(colorString string) func(...interface{}) string {
type ColorSet interface {
ToPalette() color.Palette
Get(int) color.Color
Len() int
}
type StringerPalette []ColorStringer
func createStringerPalette(backgroundFillMode, disableSmartMode bool, c ...ColorSet) StringerPalette {
palette := StringerPalette{}
for _, colorSet := range c {
for i := 0; i < colorSet.Len(); i++ {
palette = append(palette, trueColorString(colorSet.Get(i), backgroundFillMode, disableSmartMode))
}
}
return palette
}
// TBD not yet complete
func trueColorString(color color.Color, backgroundFillMode, disableSmartMode bool) ColorStringer {
fgEsc, bgEsc := 48, 38
sprint := func(args ...interface{}) string {
r, g, b, _ := color.RGBA()
if !disableSmartMode {
return fmt.Sprintf("\033[;2;%d;%d;%d;m%s\033[0m\u001B[39m",
r, g, b,
fmt.Sprint(args...))
}
esc := fgEsc
if backgroundFillMode {
esc = bgEsc
}
return fmt.Sprintf("\033[%d;2;%d;%d;%d;m%s\033[0m\u001B[%dm",
esc,
r, g, b,
fmt.Sprint(args...),
esc+1)
}
return sprint
}
type ColorStringer func(...interface{}) string
func ColorString(colorString string) ColorStringer {
sprint := func(args ...interface{}) string {
return fmt.Sprintf(colorString,
fmt.Sprint(args...))
}
return sprint
}
func GetBackgroundColor(c color.Color) color.Color {
red, green, blue, _ := c.RGBA()
if (float32(red)*0.299 + float32(green)*0.587 + float32(blue)*0.114) > 150.0 {
return simplecolor.FromRGBA(0, 0, 0, 0)
}
return simplecolor.FromRGBA(255, 255, 255, 0)
}

2
go.mod
View File

@@ -2,4 +2,4 @@ module github.com/taigrr/colorhash
go 1.18
require github.com/taigrr/simplecolorpalettes v0.9.2
require github.com/taigrr/simplecolorpalettes v0.9.5

4
go.sum
View File

@@ -1,2 +1,2 @@
github.com/taigrr/simplecolorpalettes v0.9.2 h1:ofnMYZWyg00EzaHkoo51bReptkctOmgZDOn3aD1Dcgs=
github.com/taigrr/simplecolorpalettes v0.9.2/go.mod h1:MFLQqI3JOfSc+8GiO3amYfzBiozxITaQi+F1iEV8XpQ=
github.com/taigrr/simplecolorpalettes v0.9.5 h1:XPyRYwCHh+0ra/7Qw5c9yQf/O4yeLkuqx2X1tVuBE2U=
github.com/taigrr/simplecolorpalettes v0.9.5/go.mod h1:MFLQqI3JOfSc+8GiO3amYfzBiozxITaQi+F1iEV8XpQ=

View File

@@ -5,9 +5,7 @@ import (
)
func TestHashBytes(t *testing.T) {
testBytes := []struct {
runAsUser bool
}{}
testBytes := []struct{}{}
_ = testBytes
}