Skip to content

Commit 5def9c3

Browse files
committed
Adding background support for non-svgs
1 parent ab6a7a0 commit 5def9c3

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/kenshaw/iv
33
go 1.20
44

55
require (
6-
github.com/kenshaw/colors v0.1.2
6+
github.com/kenshaw/colors v0.1.4
77
github.com/kenshaw/rasterm v0.1.10
88
github.com/spf13/cobra v1.8.0
99
github.com/xo/resvg v0.4.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
22
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
33
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
4-
github.com/kenshaw/colors v0.1.2 h1:qy1V9XzxSbUkFW3WyeTf3+ma3604Qks/S9iWDi3cyqE=
5-
github.com/kenshaw/colors v0.1.2/go.mod h1:m8LcSiaLgIxtzCHQqxReKkJPP5TfXUZZdQvFdCuQGyY=
4+
github.com/kenshaw/colors v0.1.4 h1:2IbZLfJJlLuUyHN6/vgMK+1b4g+9GcD6u2pBx9GwviE=
5+
github.com/kenshaw/colors v0.1.4/go.mod h1:m8LcSiaLgIxtzCHQqxReKkJPP5TfXUZZdQvFdCuQGyY=
66
github.com/kenshaw/rasterm v0.1.10 h1:cMCTpBHfqmftt/VqeT6B+9Td+mYi+ZtziN+XBdrTQfA=
77
github.com/kenshaw/rasterm v0.1.10/go.mod h1:kL4DCN+wOlQ4BPBCxA+itiVwiObRAj0Hkze7SbCyYaw=
88
github.com/kenshaw/snaker v0.2.0 h1:DPlxCtAv9mw1wSsvIN1khUAPJUIbFJUckMIDWSQ7TC8=

main.go

+24-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"image"
99
"image/color"
10+
"image/draw"
1011
"io"
1112
"os"
1213
"path/filepath"
@@ -111,7 +112,7 @@ func do(w io.Writer, bg color.Color, args []string) error {
111112
}
112113
files = append(files, v...)
113114
}
114-
return render(w, files)
115+
return render(w, bg, files)
115116
}
116117

117118
func open(name string) ([]string, error) {
@@ -138,36 +139,49 @@ func open(name string) ([]string, error) {
138139

139140
var extRE = regexp.MustCompile(`(?i)\.(jpe?g|gif|png|svg|bmp|bitmap|tiff?|hei[vc]|webp)$`)
140141

141-
func render(w io.Writer, files []string) error {
142+
func render(w io.Writer, bg color.Color, files []string) error {
143+
var c color.Color
144+
if !colors.Is(bg, colors.Transparent) {
145+
c = color.NRGBAModel.Convert(bg).(color.NRGBA)
146+
}
142147
for i := 0; i < len(files); i++ {
143-
if err := renderFile(w, files[i]); err != nil {
148+
if err := renderFile(w, c, files[i]); err != nil {
144149
fmt.Fprintf(w, "error: unable to render arg %d: %v\n", i, err)
145150
}
146151
}
147152
return nil
148153
}
149154

150155
// doFile renders the specified file to w.
151-
func renderFile(w io.Writer, file string) error {
156+
func renderFile(w io.Writer, bg color.Color, file string) error {
152157
fmt.Fprintln(w, file+":")
153158
f, err := os.OpenFile(file, os.O_RDONLY, 0)
154159
if err != nil {
155160
return fmt.Errorf("can't open %s: %w", file, err)
156161
}
157-
img, _, err := image.Decode(f)
162+
img, typ, err := image.Decode(f)
158163
if err != nil {
159164
defer f.Close()
160165
return fmt.Errorf("can't decode %s: %w", file, err)
161166
}
162167
if err := f.Close(); err != nil {
163168
return fmt.Errorf("can't close %s: %w", file, err)
164169
}
170+
if typ != "svg" && bg != nil {
171+
img = addBackground(bg.(color.NRGBA), img)
172+
}
165173
return rasterm.Encode(w, img)
166174
}
167175

168-
/*
169-
func init() {
170-
_ "github.com/jcbritobr/pnm"
171-
image.RegisterFormat("pbm", "P?", Decode, DecodeConfig)
176+
// addBackground adds a background for the image.
177+
func addBackground(bg color.NRGBA, fg image.Image) image.Image {
178+
b := fg.Bounds()
179+
img := image.NewNRGBA(b)
180+
for i := 0; i < b.Dx(); i++ {
181+
for j := 0; j < b.Dy(); j++ {
182+
img.SetNRGBA(i, j, bg)
183+
}
184+
}
185+
draw.Draw(img, b, fg, image.Point{}, draw.Over)
186+
return img
172187
}
173-
*/

0 commit comments

Comments
 (0)