commit 290e776624b30555d1496bbe0f2a8f95283f6213
parent a56327b484f5adf139e9f6c8f09a79959d3a11af
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Thu, 17 Nov 2022 13:14:20 -0500
optimization
Diffstat:
2 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/cmd/art/main.go b/cmd/art/main.go
@@ -69,7 +69,7 @@ FoEVD2av5BES9MvnPsQulj9bU2lUokhBjM1+LERxbqfVfZ2ddAYRIMGF`
h := len(lines)
classPrefix := "gpg_"
- classIdx := 0
+ prevClass := ""
colorsMap := make(map[string]string)
colorsArr := [][]string{}
for y := 0; y < h; y++ {
@@ -79,10 +79,10 @@ FoEVD2av5BES9MvnPsQulj9bU2lUokhBjM1+LERxbqfVfZ2ddAYRIMGF`
if _, ok := colorsMap[hex]; ok {
continue
}
- className := classPrefix + getClassName(classIdx)
+ prevClass = getNextClassNameStr(prevClass)
+ className := classPrefix + prevClass
colorsMap[hex] = className
colorsArr = append(colorsArr, []string{className, hex})
- classIdx++
}
}
sort.Slice(colorsArr, func(i, j int) bool {
@@ -98,7 +98,7 @@ FoEVD2av5BES9MvnPsQulj9bU2lUokhBjM1+LERxbqfVfZ2ddAYRIMGF`
out += "<div style=\"font-family: monospace; background-color: #222;\">\n"
out += "<pre>\n"
spanOpen := false
- prevClass := ""
+ prevClass = ""
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
c := im.At(x, y)
@@ -136,6 +136,10 @@ func HexFromRgbColor(color color.NRGBA) string {
const firstRune = 'a'
const lastRune = 'z'
+func getNextClassNameStr(in string) string {
+ return string(getNextClassName([]rune(in)))
+}
+
func getNextClassName(in []rune) (out []rune) {
out = in
for i := len(out) - 1; i >= 0; i-- {
diff --git a/cmd/art/main_test.go b/cmd/art/main_test.go
@@ -33,3 +33,8 @@ func TestGetClassName(t *testing.T) {
assert.Equal(t, "bzz", getClassName(2053))
assert.Equal(t, "caa", getClassName(2054))
}
+
+func TestGetNextClassNameStr(t *testing.T) {
+ assert.Equal(t, "a", getNextClassNameStr(""))
+ assert.Equal(t, "b", getNextClassNameStr("a"))
+}