-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaintToTarget.go
More file actions
57 lines (51 loc) · 983 Bytes
/
paintToTarget.go
File metadata and controls
57 lines (51 loc) · 983 Bytes
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
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
)
// Negative takes pointer f to image file (PNG encoded) and output inverted PNG with fname
func PaintTo(f *os.File, fname *string, to *string) {
ri, err := png.Decode(f)
if err != nil {
fmt.Println(err)
}
or := ri.Bounds()
size := or.Size()
ni := image.NewRGBA(or)
for x := 0; x < size.X; x++ {
for y := 0; y < size.Y; y++ {
r, g, b, _ := ri.At(x, y).RGBA()
if *to == "red" {
g = 0
b = 0
} else if *to == "green" {
r = 0
b = 0
} else if *to == "blue" {
r = 0
g = 0
} else {
fmt.Println("Please provide one of the following colors to paint with -to flag\n\tred, green, blue")
return
}
ni.Set(x, y, color.RGBA{
R: uint8(r),
G: uint8(g),
B: uint8(b),
A: 255,
})
}
}
nf, err := os.Create(*fname)
if err != nil {
fmt.Println(err)
}
defer nf.Close()
err = png.Encode(nf, ni)
if err != nil {
fmt.Println(err)
}
}