diff --git a/cmd/main.go b/cmd/main.go index b2dec0b..00866a4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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) } diff --git a/colors.go b/colors.go index e2b6ca1..14e6fd6 100644 --- a/colors.go +++ b/colors.go @@ -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) +} diff --git a/go.mod b/go.mod index 0f1f162..7c2b734 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3a3ee85..90ea533 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/hash_test.go b/hash_test.go index f147cf8..92a6d76 100644 --- a/hash_test.go +++ b/hash_test.go @@ -5,9 +5,7 @@ import ( ) func TestHashBytes(t *testing.T) { - testBytes := []struct { - runAsUser bool - }{} + testBytes := []struct{}{} _ = testBytes }