-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocova.go
183 lines (153 loc) · 3.55 KB
/
gocova.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"image"
"image/color"
"image/gif"
"image/jpeg"
"image/png"
"log"
"math"
"os"
"strconv"
"github.com/lucasb-eyer/go-colorful"
"github.com/urfave/cli"
)
var (
flags = []cli.Flag{
cli.StringFlag{
Name: "output, o",
Usage: "output image path base",
Value: "./result",
},
cli.IntFlag{
Name: "pattern, p",
Usage: "number of images to generate",
Value: 10,
},
cli.Float64Flag{
Name: "saturation, s",
Usage: "saturation offset [-100.0 ... 100.0]",
Value: 0,
},
cli.Float64Flag{
Name: "lightness, l",
Usage: "lightness offset [-100.0 ... 100.0]",
Value: 0,
},
}
)
type filter struct {
hue float64
saturation float64
lightness float64
isGrayscale bool
isBitonal bool
}
type imageFile struct {
image image.Image
format string
bounds image.Rectangle
}
func getDstPathBase(pathbase string, ext string, pattern int) string {
return fmt.Sprintf("%s_%%0%dd.%s", pathbase, len(strconv.Itoa(pattern)), ext)
}
func readImage(inputImage string) imageFile {
reader, err := os.Open(inputImage)
if err != nil {
log.Fatal(err)
}
defer reader.Close()
srcImage, format, err := image.Decode(reader)
if err != nil {
log.Fatal(err)
}
return imageFile{
image: srcImage,
format: format,
bounds: srcImage.Bounds(),
}
}
func writeImage(dst imageFile, dstPath string) {
dstFile, err := os.Create(dstPath)
if err != nil {
log.Fatal(err)
}
defer dstFile.Close()
switch dst.format {
case "png":
png.Encode(dstFile, image.Image(dst.image))
break
case "jpeg":
jpeg.Encode(dstFile, image.Image(dst.image), nil)
break
case "gif":
gif.Encode(dstFile, image.Image(dst.image), nil)
break
}
}
func filterImage(src *imageFile, filter filter) imageFile {
filteredImage := image.NewRGBA(src.bounds)
for y := src.bounds.Min.Y; y < src.bounds.Max.Y; y++ {
for x := src.bounds.Min.X; x < src.bounds.Max.X; x++ {
orgColor := src.image.At(x, y)
colorfulColor, _ := colorful.MakeColor(orgColor)
h, s, l := colorfulColor.Hsl()
h = math.Mod(h+float64(filter.hue), 360.0)
s = Clamp01(s + filter.saturation)
l = Clamp01(l + filter.lightness)
if filter.isGrayscale {
s = 0.5
}
if filter.isBitonal {
l = 0.5
}
resultHsv := colorful.Hsl(h, s, l)
r, g, b := resultHsv.RGB255()
if src.format == "png" {
filteredImage.Set(x, y, color.NRGBA{r, g, b, orgColor.(color.NRGBA).A})
} else {
filteredImage.Set(x, y, color.RGBA{r, g, b, 255})
}
}
}
return imageFile{
image: filteredImage,
format: src.format,
bounds: src.bounds,
}
}
func parseFilter(c *cli.Context) (filter, int) {
pattern := c.Int("pattern")
h := float64(360.0 / (pattern + 1))
s := Clamp(c.Float64("saturation"), -100.0, 100.0) / 100.0
l := Clamp(c.Float64("lightness"), -100.0, 100.0) / 100.0
return filter{
hue: h,
saturation: s,
lightness: l,
}, pattern
}
func process(c *cli.Context) {
src := readImage(c.Args().Get(0))
dstPath := c.String("output")
filter, pattern := parseFilter(c)
hueInterval := filter.hue
filter.isBitonal = bitonalDetect(src)
filter.isGrayscale = grayscaleDetect(src)
dstFilePathBase := getDstPathBase(dstPath, src.format, pattern)
for i := 1; i <= pattern; i++ {
dstFilePath := fmt.Sprintf(dstFilePathBase, i)
filter.hue = hueInterval * float64(i)
writeImage(filterImage(&src, filter), dstFilePath)
}
}
func main() {
app := cli.NewApp()
app.Name = "gocova"
app.Usage = "Go color variation, generate images of various colors"
app.Version = "1.4.0"
app.Action = process
app.Flags = flags
app.Run(os.Args)
}