aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 5f2d4195929b471d17e96dccf92e1e9cc9971cce (plain) (blame)
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
package main

import (
	"flag"
	"fmt"
	"image/color"
	"image/png"
	"log"
	"os"
	"strings"
)

var (
	threshold     *int
	output        *string
	input         *string
	minBrightness *int
	maxBrightness *int
	imageout      *string
	imageWidth    *int
	imageHeight   *int
)

func usage() {
	fmt.Println("Usage: schemer2 [FLAGS] -in=[FORMAT]:[FILENAME] (-out=[FORMAT] | -outputImage=[FILENAME])")
	flag.PrintDefaults()
	os.Exit(2)
}

func main() {
	inSupport := "Format and filename of input file (eg \"xfce:~/.config/xfce4/terminal/terminalrc\"). Currently supported: \n"
	outSupport := "Format to output colors as. Currently supported: \n"
	for _, f := range formats {
		if f.output != nil {
			outSupport += strings.Join([]string{"    ", f.friendlyName, ":", f.flagName, "\n"}, " ")
		}

		if f.input != nil {
			inSupport += strings.Join([]string{"    ", f.friendlyName, ":", f.flagName, "\n"}, " ")
		}
	}

	threshold = flag.Int("t", 50, "Threshold for minimum color difference (image input only)")
	output = flag.String("out", "", outSupport)
	input = flag.String("in", "", inSupport)
	minBrightness = flag.Int("minBright", 0, "Minimum brightness for colors (image input only)")
	maxBrightness = flag.Int("maxBright", 200, "Maximum brightness for colors (image input only)")
	imageout = flag.String("outputImage", "", "Create image from colors, and save to this file")
	imageHeight = flag.Int("h", 1080, "Height of output image")
	imageWidth = flag.Int("w", 1920, "Width of output image")

	flag.Usage = usage
	flag.Parse()
	if *input == "" {
		usage()
		os.Exit(2)
	}
	if *minBrightness > 255 || *maxBrightness > 255 {
		fmt.Print("Minimum and maximum brightness must be an integer between 0 and 255.\n")
		os.Exit(2)
	}
	if *threshold > 255 {
		fmt.Print("Threshold should be an integer between 0 and 255.\n")
		os.Exit(2)
	}

	if *imageWidth < 100 || *imageHeight < 100 {
		log.Fatal("Minimum resolution of image output is 100x100")
	}

	// Determine format and filename
	// And get colors from file using specified format
	format := strings.SplitN(*input, ":", 2)[0]
	filename := strings.SplitN(*input, ":", 2)[1]

	formatInMatch := false
	var colors []color.Color
	var err error
	for _, f := range formats {
		if format == f.flagName {
			if f.input == nil {
				fmt.Printf("Unrecognised input format: %v \n", format)
				return
			}
			colors, err = f.input(filename)
			if err != nil {
				fmt.Print(err, "\n")
				return
			}
			formatInMatch = true
			break
		}
	}
	if !formatInMatch {
		fmt.Printf("Did not recognise format %v. \n", *input)
		return
	}

	// Ensure there are 16 colors
	if len(colors) > 16 {
		colors = colors[:16]
	} else if len(colors) < 16 {
		// TODO: Should this just be a warning (for cases where only 8 colors are defined?)
		log.Fatal("Less than 16 colors. Aborting.")
	}

	// Output the configuration specified
	if !(*output == "") {
		formatOutMatch := false
		for _, f := range formats {
			if *output == f.flagName {
				if f.output == nil {
					fmt.Printf("Unrecognised output format: %v \n", format)
					return
				}
				fmt.Print(f.output(colors))
				formatOutMatch = true
				break
			}
		}
		if !formatOutMatch {
			fmt.Printf("Did not recognise format %v. \n", *output)
		}
	}

	if *imageout != "" {
		file, err := os.OpenFile(*imageout, os.O_CREATE|os.O_WRONLY, 0666)
		if err != nil {
			log.Fatal(err)
		}
		defer file.Close()

		img := imageFromColors(colors, *imageWidth, *imageHeight) // TODO

		png.Encode(file, img)
	}
}