-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcolor-suppress.go
94 lines (81 loc) · 1.83 KB
/
color-suppress.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
// suppress some gradients to some colors for an image
package main
import (
"flag"
"image"
"image/color"
"image/gif"
"image/jpeg"
"image/png"
"log"
"math"
"os"
"path/filepath"
"strconv"
"strings"
)
func main() {
log.SetFlags(0)
transparent := flag.Bool("transparent", false, "suppress the color by making it transparent")
threshold := flag.Float64("threshold", 10, "threshold")
flag.Parse()
if flag.NArg() < 3 {
log.Fatal("usage: color1 ... colorN <input> <output>")
}
var c []color.RGBA
for i := 0; i < flag.NArg()-2; i++ {
n, err := strconv.ParseInt(flag.Arg(i), 0, 64)
ck(err)
c = append(c, color.RGBA{uint8(n), uint8(n >> 8), uint8(n >> 16), 255})
}
f, err := os.Open(flag.Arg(flag.NArg() - 2))
ck(err)
defer f.Close()
m, _, err := image.Decode(f)
ck(err)
r := m.Bounds()
p := image.NewRGBA(r)
for y := r.Min.Y; y < r.Max.Y; y++ {
for x := r.Min.X; x < r.Max.X; x++ {
w := m.At(x, y)
p.Set(x, y, w)
ar, ag, ab, _ := w.RGBA()
br, bg, bb := uint8(ar>>8), uint8(ag>>8), uint8(ab>>8)
for _, c := range c {
xx := int(c.R) - int(br)
yy := int(c.G) - int(bg)
zz := int(c.B) - int(bb)
diff := math.Sqrt(float64(xx*xx + yy*yy + zz*zz))
if diff <= *threshold {
if *transparent {
p.Set(x, y, color.RGBA{})
} else {
p.Set(x, y, c)
}
break
}
}
}
}
output := flag.Arg(flag.NArg() - 1)
w, err := os.Create(output)
ck(err)
switch strings.ToLower(filepath.Ext(output)) {
case ".jpeg", ".jpg":
err = jpeg.Encode(w, p, nil)
case ".gif":
err = gif.Encode(w, p, &gif.Options{NumColors: 255})
case ".png":
fallthrough
default:
err = png.Encode(w, p)
}
ck(err)
err = w.Close()
ck(err)
}
func ck(err error) {
if err != nil {
log.Fatal(err)
}
}