mirror of
https://github.com/KevinMidboe/motdGOi.git
synced 2025-10-29 09:40:24 +00:00
add parsers
This commit is contained in:
92
color.go
92
color.go
@@ -1,14 +1,15 @@
|
||||
package figlet4go
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Escape char
|
||||
const escape string = "\x1b"
|
||||
|
||||
// Terminal AnsiColors
|
||||
var (
|
||||
ColorBlack AnsiColor = AnsiColor{30}
|
||||
ColorRed AnsiColor = AnsiColor{31}
|
||||
@@ -20,18 +21,32 @@ var (
|
||||
ColorWhite AnsiColor = AnsiColor{37}
|
||||
)
|
||||
|
||||
// Every color has a pre- and a suffix
|
||||
type Color interface {
|
||||
getPrefix() string
|
||||
getSuffix() string
|
||||
// TrueColor lookalikes for displaying AnsiColor f.e. with the HTML parser
|
||||
// Colors based on http://clrs.cc/
|
||||
// "TrueColorForAnsiColor"
|
||||
var tcfac map[AnsiColor]TrueColor = map[AnsiColor]TrueColor{
|
||||
ColorBlack: TrueColor{0, 0, 0},
|
||||
ColorRed: TrueColor{255, 65, 54},
|
||||
ColorGreen: TrueColor{149, 189, 64},
|
||||
ColorYellow: TrueColor{255, 220, 0},
|
||||
ColorBlue: TrueColor{0, 116, 217},
|
||||
ColorMagenta: TrueColor{177, 13, 201},
|
||||
ColorCyan: TrueColor{105, 206, 245},
|
||||
ColorWhite: TrueColor{255, 255, 255},
|
||||
}
|
||||
|
||||
// Ansi color
|
||||
// Every color has a pre- and a suffix
|
||||
type Color interface {
|
||||
getPrefix(p Parser) string
|
||||
getSuffix(p Parser) string
|
||||
}
|
||||
|
||||
// AnsiColor representation
|
||||
type AnsiColor struct {
|
||||
code int
|
||||
}
|
||||
|
||||
// Truecolor with rgb Attributes
|
||||
// TrueColor with rgb Attributes
|
||||
type TrueColor struct {
|
||||
r int
|
||||
g int
|
||||
@@ -39,16 +54,35 @@ type TrueColor struct {
|
||||
}
|
||||
|
||||
// Prefix for ansi color
|
||||
func (tc TrueColor) getPrefix() string {
|
||||
return fmt.Sprintf("%v[38;2;%d;%d;%dm", escape, tc.r, tc.g, tc.b)
|
||||
func (tc TrueColor) getPrefix(p Parser) string {
|
||||
switch p.Name {
|
||||
|
||||
case "terminal":
|
||||
return fmt.Sprintf("%v[38;2;%d;%d;%dm", escape, tc.r, tc.g, tc.b)
|
||||
|
||||
case "html":
|
||||
return fmt.Sprintf("<span style='color: rgb(%d,%d,%d);'>", tc.r, tc.g, tc.b)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// Suffix for ansi color
|
||||
func (tc TrueColor) getSuffix() string {
|
||||
return fmt.Sprintf("%v[0m", escape)
|
||||
func (tc TrueColor) getSuffix(p Parser) string {
|
||||
switch p.Name {
|
||||
|
||||
case "terminal":
|
||||
return fmt.Sprintf("%v[0m", escape)
|
||||
|
||||
case "html":
|
||||
return "</span>"
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetTrueColorFromHexString(c string) (*TrueColor, error) {
|
||||
// NewTrueColorFromHexString returns a Truecolor object based on a hexadezimal string
|
||||
func NewTrueColorFromHexString(c string) (*TrueColor, error) {
|
||||
rgb, err := hex.DecodeString(c)
|
||||
if err != nil {
|
||||
return nil, errors.New("Invalid color given (" + c + ")")
|
||||
@@ -61,13 +95,35 @@ func GetTrueColorFromHexString(c string) (*TrueColor, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
// Prefix for ansi color
|
||||
func (ac AnsiColor) getPrefix() string {
|
||||
return fmt.Sprintf("%v[0;%dm", escape, ac.code)
|
||||
func (ac AnsiColor) getPrefix(p Parser) string {
|
||||
switch p.Name {
|
||||
|
||||
case "terminal":
|
||||
return fmt.Sprintf("%v[0;%dm", escape, ac.code)
|
||||
|
||||
case "html":
|
||||
// Get the TrueColor for the AnsiColor
|
||||
tc := tcfac[ac]
|
||||
return tc.getPrefix(p)
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
}
|
||||
|
||||
// Suffix for ansi color
|
||||
func (ac AnsiColor) getSuffix() string {
|
||||
return fmt.Sprintf("%v[0m", escape)
|
||||
func (ac AnsiColor) getSuffix(p Parser) string {
|
||||
switch p.Name {
|
||||
|
||||
case "terminal":
|
||||
return fmt.Sprintf("%v[0m", escape)
|
||||
|
||||
case "html":
|
||||
// Get the TrueColor for the AnsiColor
|
||||
tc := tcfac[ac]
|
||||
return tc.getSuffix(p)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user