Skip to content

Commit

Permalink
Changing to new color package
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Feb 3, 2024
1 parent ae4e796 commit 5eb85db
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 44 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
47 changes: 6 additions & 41 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"image/color"
"io"
"math"
"os"
"path/filepath"
"regexp"
Expand All @@ -22,14 +21,14 @@ import (
"text/template"
"unicode"

"github.com/kenshaw/colors"
"github.com/kenshaw/rasterm"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/tdewolff/canvas"
fontpkg "github.com/tdewolff/canvas/font"
"github.com/tdewolff/canvas/renderers/rasterizer"
"golang.org/x/exp/maps"
"gopkg.in/go-playground/colors.v1"
)

var (
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand 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,
Expand All @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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

0 comments on commit 5eb85db

Please sign in to comment.