-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
61 lines (61 loc) · 1.39 KB
/
main.go
File metadata and controls
61 lines (61 loc) · 1.39 KB
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
package main
import (
"fmt"
"os"
"image"
"flag"
"path/filepath"
_ "image/png"
_ "image/jpeg"
color "github.com/gookit/color"
ico "github.com/mat/besticon/ico"
color_extractor "github.com/marekm4/color-extractor"
)
func main() {
var SkipCommon bool
flag.BoolVar(&SkipCommon, "C", false, "Skip black, white and gray colors")
flag.Parse()
pic, where_is_file := os.Open(flag.Arg(0))
if where_is_file != nil {
fmt.Println("I did not find file. Run picdoc with filename.")
os.Exit(1)
}
var dec image.Image
ext := filepath.Ext(flag.Arg(0))
if ext == ".jpg" || ext == ".png" || ext == ".jpeg"{
dec,_,_ = image.Decode(pic)
} else if ext == ".ico" {
dec,_ = ico.Decode(pic)
} else {
fmt.Println("I can't recognize file. Is it picture (PNG, JPG, JPEG, ICO)?")
os.Exit(1)
}
colors := color_extractor.ExtractColors(dec)
nearest := colors[0]
R, G, B, _ := nearest.RGBA()
R >>= 8
G >>= 8
B >>= 8
if R == G && R == B && G == B && SkipCommon {
nearest = colors[1]
R, G, B, _ = nearest.RGBA()
R >>= 8
G >>= 8
B >>= 8
}
var SR, SG, SB string
SR = fmt.Sprintf("%X", R)
SG = fmt.Sprintf("%X", G)
SB = fmt.Sprintf("%X", B)
if len(SR) < 2 {
SR = "0" + SR
}
if len(SG) < 2 {
SG = "0" + SG
}
if len(SB) < 2 {
SB = "0" + SB
}
color.Printf(`<bg=%s%s%s> </> <fg=ffff00>(%d, %d, %d = #%s%s%s)</> %s`,SR,SG,SB,R,G,B,SR,SG,SB,flag.Arg(0))
fmt.Println()
}