minicolor formater with hex

This commit is contained in:
2026-10-22 16:25:16 +03:00
parent da0fd7ddaf
commit 03a32ba6a8
2 changed files with 55 additions and 6 deletions
+39
View File
@@ -0,0 +1,39 @@
// Package tcolor
package tcolor
import (
"strconv"
"regexp"
"fmt"
)
type TColor struct{
colorRe *regexp.Regexp
}
func (t *TColor) Init(){
colorRe,colorReErr := regexp.Compile(`#[0-9a-fA-F]{6}`)
if colorReErr != nil{
fmt.Errorf("Regexp color err: %s",colorReErr.Error())
}else{
t.colorRe = colorRe
}
}
func (t *TColor) HexToANSI(hex string) string {
if t.colorRe.MatchString(hex) {
hex = hex[1:]
}
if len(hex) != 6 {
return "" // Возвращаем пустую строку при ошибке
}
r, _ := strconv.ParseUint(hex[0:2], 16, 8)
g, _ := strconv.ParseUint(hex[2:4], 16, 8)
b, _ := strconv.ParseUint(hex[4:6], 16, 8)
// Формат TrueColor ESC[38;2;R;G;Bm
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b)
}