-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
130 lines (107 loc) · 3.14 KB
/
config.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
package main
import (
"flag"
"fmt"
"image/color"
"os"
"strconv"
"strings"
)
// Config defines application settings.
type Config struct {
Input string // Image file with simulation data to load.
Width int // Display width in pixels.
Height int // Display height in pixels.
Palette Palette // Color palette to use.
Fullscreen bool // Run in fullscreen mode?
}
// parseArgs parses commandline arguments and returns a config struct.
// Exits the program with an error if invalid data was found.
func parseArgs() *Config {
var c Config
c.Width = 1280
c.Height = 600
c.Fullscreen = false
c.Palette.LoadDefault()
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [options] <image>\n", os.Args[0])
flag.PrintDefaults()
}
palEmpty := flag.String("pal-empty", hexStr(c.Palette.Empty), "Color for empty cells.")
palWire := flag.String("pal-wire", hexStr(c.Palette.Wire), "Color for wire cells.")
palHead := flag.String("pal-head", hexStr(c.Palette.Head), "Color for electron head cells.")
palTail := flag.String("pal-tail", hexStr(c.Palette.Tail), "Color for electron tail cells.")
flag.IntVar(&c.Width, "width", c.Width, "Display width in pixels.")
flag.IntVar(&c.Height, "height", c.Height, "Display height in pixels.")
flag.BoolVar(&c.Fullscreen, "fullscreen", c.Fullscreen, "Use a fullscreen display.")
version := flag.Bool("version", false, "Displays version information.")
flag.Parse()
if *version {
fmt.Println(Version())
os.Exit(0)
}
if flag.NArg() == 0 {
fmt.Fprintln(os.Stderr, "missing input image")
flag.Usage()
os.Exit(1)
}
c.Input = flag.Arg(0)
if c.Width <= 0 {
fmt.Fprintf(os.Stderr, "width must be > 0")
flag.Usage()
os.Exit(1)
}
if c.Height <= 0 {
fmt.Fprintf(os.Stderr, "height must be > 0")
flag.Usage()
os.Exit(1)
}
if len(*palEmpty) > 0 {
c.Palette.Empty = parseHex(*palEmpty)
}
if len(*palWire) > 0 {
c.Palette.Wire = parseHex(*palWire)
}
if len(*palHead) > 0 {
c.Palette.Head = parseHex(*palHead)
}
if len(*palTail) > 0 {
c.Palette.Tail = parseHex(*palTail)
}
return &c
}
// hexStr returns color c as a hex string.
// E.g.: [255, 255, 255] -> "ffffff"
// E.g.: [255, 0, 127] -> "ff007f"
func hexStr(clr color.RGBA) string {
return fmt.Sprintf("%02x%02x%02x", clr.R, clr.G, clr.B)
}
// parseHex returns the given hex string as a floating-point RGBA color.
// E.g.: "ffffff" -> [255, 255, 255]
// E.g.: "ff007f" -> [255, 0, 127]
func parseHex(str string) color.RGBA {
str = strings.ToLower(str)
if len(str) != 6 {
fmt.Fprintf(os.Stderr, "invalid color %q; expected form: rrggbb\n", str)
os.Exit(1)
}
sr := str[0:2]
sg := str[2:4]
sb := str[4:6]
r, err := strconv.ParseInt(sr, 16, 32)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid red component in color %q; %v\n", str, err)
os.Exit(1)
}
g, err := strconv.ParseInt(sg, 16, 32)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid green component in color %q; %v\n", str, err)
os.Exit(1)
}
b, err := strconv.ParseInt(sb, 16, 32)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid blue component in color %q; %v\n", str, err)
os.Exit(1)
}
return color.RGBA{byte(r), byte(g), byte(b), 255}
}