minicolor formater with hex
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user