From 5eb85db261e074008c5e4934240f06bf04dd6325 Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Sat, 3 Feb 2024 13:18:16 +0700 Subject: [PATCH] Changing to new color package --- go.mod | 3 ++- go.sum | 6 ++++-- main.go | 47 ++++++----------------------------------------- 3 files changed, 12 insertions(+), 44 deletions(-) diff --git a/go.mod b/go.mod index a14b9dc..b6f0abe 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,12 @@ module github.com/kenshaw/fv go 1.20 require ( + github.com/kenshaw/colors v0.1.0 github.com/kenshaw/rasterm v0.1.10 github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.5 github.com/tdewolff/canvas v0.0.0-20240115005846-3d69968abe72 golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 - gopkg.in/go-playground/colors.v1 v1.2.0 ) require ( @@ -20,6 +20,7 @@ require ( github.com/go-text/typesetting v0.1.0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/kenshaw/snaker v0.2.0 // indirect github.com/mattn/go-sixel v0.0.5 // indirect github.com/soniakeys/quant v1.0.0 // indirect github.com/tdewolff/minify/v2 v2.20.14 // indirect diff --git a/go.sum b/go.sum index 557674f..a948124 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,12 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF0 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kenshaw/colors v0.1.0 h1:L2Ll4qhm/0XDNcEcIbkM1hl1zzswJBHBjkcssvgl9Os= +github.com/kenshaw/colors v0.1.0/go.mod h1:m8LcSiaLgIxtzCHQqxReKkJPP5TfXUZZdQvFdCuQGyY= github.com/kenshaw/rasterm v0.1.10 h1:cMCTpBHfqmftt/VqeT6B+9Td+mYi+ZtziN+XBdrTQfA= github.com/kenshaw/rasterm v0.1.10/go.mod h1:kL4DCN+wOlQ4BPBCxA+itiVwiObRAj0Hkze7SbCyYaw= +github.com/kenshaw/snaker v0.2.0 h1:DPlxCtAv9mw1wSsvIN1khUAPJUIbFJUckMIDWSQ7TC8= +github.com/kenshaw/snaker v0.2.0/go.mod h1:DNyRUqHMZ18/zioxr6R7m4kSxxf2+QmB0BXoORsXRaY= github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/mattn/go-sixel v0.0.5 h1:55w2FR5ncuhKhXrM5ly1eiqMQfZsnAHIpYNGZX03Cv8= @@ -71,8 +75,6 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gonum.org/v1/plot v0.14.0 h1:+LBDVFYwFe4LHhdP8coW6296MBEY4nQ+Y4vuUpJopcE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/go-playground/colors.v1 v1.2.0 h1:SPweMUve+ywPrfwao+UvfD5Ah78aOLUkT5RlJiZn52c= -gopkg.in/go-playground/colors.v1 v1.2.0/go.mod h1:AvbqcMpNXVl5gBrM20jBm3VjjKBbH/kI5UnqjU7lxFI= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= star-tex.org/x/tex v0.4.0 h1:AXUwgpnHLCxZUWW3qrmjv6ezNhH3PjUVBuLLejz2cgU= star-tex.org/x/tex v0.4.0/go.mod h1:w91ycsU/DkkCr7GWr60GPWqp3gn2U+6VX71T0o8k8qE= diff --git a/main.go b/main.go index 95619f5..b1c0aad 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( "fmt" "image/color" "io" - "math" "os" "path/filepath" "regexp" @@ -22,6 +21,7 @@ import ( "text/template" "unicode" + "github.com/kenshaw/colors" "github.com/kenshaw/rasterm" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -29,7 +29,6 @@ import ( fontpkg "github.com/tdewolff/canvas/font" "github.com/tdewolff/canvas/renderers/rasterizer" "golang.org/x/exp/maps" - "gopkg.in/go-playground/colors.v1" ) var ( @@ -46,8 +45,7 @@ func main() { func run(ctx context.Context, appName, appVersion string, cliargs []string) error { var all, list, match bool - var fg colors.Color = colors.FromStdColor(color.Black) - var bg colors.Color = colors.FromStdColor(color.RGBA{R: 255, G: 255, B: 255}) + fg, bg := colors.FromColor(color.Black), colors.FromColor(color.White) var size, margin, dpi int style, variant := canvas.FontRegular, canvas.FontNormal var text string @@ -77,7 +75,6 @@ func run(ctx context.Context, appName, appVersion string, cliargs []string) erro if err != nil { return err } - fgColor, bgColor := convColor(fg), convColor(bg) f := do switch { case all: @@ -88,8 +85,8 @@ func run(ctx context.Context, appName, appVersion string, cliargs []string) erro f = doMatch } return f(os.Stdout, sysfonts, &Params{ - FG: fgColor, - BG: bgColor, + FG: fg, + BG: bg, Size: size, DPI: dpi, Margin: margin, @@ -103,8 +100,8 @@ func run(ctx context.Context, appName, appVersion string, cliargs []string) erro c.Flags().BoolVar(&all, "all", false, "show all system fonts") c.Flags().BoolVar(&list, "list", false, "list system fonts") c.Flags().BoolVar(&match, "match", false, "match system fonts") - c.Flags().Var(NewColor(&fg), "fg", "foreground color") - c.Flags().Var(NewColor(&bg), "bg", "background color") + c.Flags().Var(fg.Pflag(), "fg", "foreground color") + c.Flags().Var(bg.Pflag(), "bg", "background color") c.Flags().IntVar(&size, "size", 48, "font size") c.Flags().IntVar(&margin, "margin", 5, "margin") c.Flags().IntVar(&dpi, "dpi", 100, "dpi") @@ -454,33 +451,6 @@ func peek(r []rune, i int) rune { return 0 } -type Color struct { - c *colors.Color -} - -func NewColor(c *colors.Color) pflag.Value { - return Color{ - c: c, - } -} - -func (c Color) String() string { - return (*c.c).String() -} - -func (c Color) Set(s string) error { - clr, err := colors.Parse(s) - if err != nil { - return colors.ErrBadColor - } - *c.c = clr - return nil -} - -func (c Color) Type() string { - return "color" -} - type Style struct { v *canvas.FontStyle } @@ -568,10 +538,5 @@ func (v Variant) Type() string { return "font-variant" } -func convColor(c colors.Color) color.Color { - clr := c.ToRGBA() - return color.RGBA{R: clr.R, G: clr.G, B: clr.B, A: uint8(math.Round(255 * clr.A))} -} - //go:embed text.tpl var textTpl []byte