Skip to content

Commit

Permalink
Merge pull request #22 from wader/nicer-list-colors
Browse files Browse the repository at this point in the history
Made -listcolorschemes more colorful
  • Loading branch information
wader authored Dec 15, 2023
2 parents 71ffe40 + 16e6d31 commit 8daed6c
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 48 deletions.
66 changes: 66 additions & 0 deletions color/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package color

import (
"fmt"
"regexp"
"strconv"
)

type Color struct {
R, G, B float32
}

var colorRe = regexp.MustCompile(`^#(..)(..)(..)$`)

func NewFromHex(s string) Color {
parts := colorRe.FindStringSubmatch(s)
if parts == nil {
return Color{}
}
f := func(s string) float32 { n, _ := strconv.ParseInt(s, 16, 32); return float32(n) / 255 }
return Color{
R: f(parts[1]),
G: f(parts[2]),
B: f(parts[3]),
}
}

func (c Color) Add(o Color) Color {
clamp := func(n float32) float32 {
if n <= 0 {
return 0
} else if n > 1 {
return 1
}
return n
}
return Color{
R: clamp(c.R + o.R),
G: clamp(c.G + o.G),
B: clamp(c.B + o.B),
}
}

func (c Color) Hex() string {
return fmt.Sprintf("#%.2x%.2x%.2x",
int(c.R*255),
int(c.G*255),
int(c.B*255),
)
}

func (c Color) ANSITriple() string {
return fmt.Sprintf("%d:%d:%d",
int(c.R*255),
int(c.G*255),
int(c.B*255),
)
}

func (c Color) ANSIBG() string {
return fmt.Sprintf("\x1b[48:2:%sm", c.ANSITriple())
}

func (c Color) ANSIFG() string {
return fmt.Sprintf("\x1b[38:2:%sm", c.ANSITriple())
}
42 changes: 42 additions & 0 deletions colorscheme/colorscheme.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// Package colorscheme has defintions for a VSCode color scheme
package colorscheme

import (
"fmt"
"strings"

"github.com/wader/ansisvg/color"
)

type WorkbenchColorCustomizations struct {
Foreground string `json:"terminal.foreground"`
Background string `json:"terminal.background"`
Expand All @@ -27,3 +34,38 @@ type WorkbenchColorCustomizations struct {
type VSCodeColorScheme struct {
WorkbenchColorCustomizations WorkbenchColorCustomizations `json:"workbench.colorCustomizations"`
}

func (w WorkbenchColorCustomizations) ANSIDemo(s string) string {
b := color.NewFromHex(w.Background)
f := color.NewFromHex(w.Foreground)
var sb strings.Builder
for _, c := range []string{
w.ANSIBlack,
w.ANSIBlue,
w.ANSICyan,
w.ANSIGreen,
w.ANSIMagenta,
w.ANSIRed,
w.ANSIWhite,
w.ANSIYellow,
w.ANSIBrightBlack,
w.ANSIBrightBlue,
w.ANSIBrightCyan,
w.ANSIBrightGreen,
w.ANSIBrightMagenta,
w.ANSIBrightRed,
w.ANSIBrightWhite,
w.ANSIBrightYellow,
w.SelectionBackground,
w.CursorForeground,
} {
sb.WriteString(fmt.Sprintf("%s \x1b[0m", color.NewFromHex(c).ANSIBG()))
}

return fmt.Sprintf("%s%s%s%s\x1b[0m",
b.ANSIBG(),
f.ANSIFG(),
s,
sb.String(),
)
}
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ func main() {
flag.Parse()

if *listColorSchemesFlag {
maxNameLen := 0
for _, n := range schemes.Names() {
fmt.Fprintln(os.Stdout, n)
if len(n) > maxNameLen {
maxNameLen = len(n)
}
}
for _, n := range schemes.Names() {
s, _ := schemes.Load(n)
pad := strings.Repeat(" ", maxNameLen+1-len(n))
fmt.Fprintf(os.Stdout, "%s\n", s.ANSIDemo(n+pad))
}
os.Exit(0)
}
Expand Down
50 changes: 3 additions & 47 deletions svgscreen/svgscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ package svgscreen
import (
_ "embed"
"encoding/base64"
"fmt"
"html/template"
"io"
"regexp"
"strconv"
"strings"

"github.com/wader/ansisvg/color"
)

//go:embed template.svg
Expand Down Expand Up @@ -52,49 +51,6 @@ type Screen struct {
Lines []Line
}

type color struct {
R, G, B float32
}

var colorRe = regexp.MustCompile(`^#(..)(..)(..)$`)

func newColorFromHex(s string) color {
parts := colorRe.FindStringSubmatch(s)
if parts == nil {
return color{}
}
f := func(s string) float32 { n, _ := strconv.ParseInt(s, 16, 32); return float32(n) / 255 }
return color{
R: f(parts[1]),
G: f(parts[2]),
B: f(parts[3]),
}
}

func (c color) add(o color) color {
clamp := func(n float32) float32 {
if n <= 0 {
return 0
} else if n > 1 {
return 1
}
return n
}
return color{
R: clamp(c.R + o.R),
G: clamp(c.G + o.G),
B: clamp(c.B + o.B),
}
}

func (c color) hex() string {
return fmt.Sprintf("#%.2x%.2x%.2x",
int(c.R*255),
int(c.G*255),
int(c.B*255),
)
}

func Render(w io.Writer, s Screen) error {
t := template.New("")
t.Funcs(template.FuncMap{
Expand All @@ -103,7 +59,7 @@ func Render(w io.Writer, s Screen) error {
"hasprefix": strings.HasPrefix,
"iswhitespace": func(a string) bool { return strings.TrimSpace(a) == "" },
"coloradd": func(a string, b string) string {
return newColorFromHex(a).add(newColorFromHex(b)).hex()
return color.NewFromHex(a).Add(color.NewFromHex(b)).Hex()
},
"base64": func(bs []byte) string { return base64.RawStdEncoding.EncodeToString(bs) },
})
Expand Down

0 comments on commit 8daed6c

Please sign in to comment.