summaryrefslogtreecommitdiff
path: root/main.go
blob: 8de90a952f4a74e7951e1a521d2127d0607fa493 (plain)
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
package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"math"
	"os"
	"path/filepath"
	"strings"
	"text/template"

	plist "github.com/DHowett/go-plist"
)

const t = `! Color scheme: {{.Name}}
*foreground:  {{.Foreground}}
*background:  {{.Background}}
*cursorColor: {{.Cursor}}
! Black
*color0:      {{.Ansi0}}
*color8:      {{.Ansi8}}
! Red
*color1:      {{.Ansi1}}
*color9:      {{.Ansi9}}
! Green
*color2:      {{.Ansi2}}
*color10:     {{.Ansi10}}
! Yellow
*color3:      {{.Ansi3}}
*color11:     {{.Ansi11}}
! Blue
*color4:      {{.Ansi4}}
*color12:     {{.Ansi12}}
! Magenta
*color5:      {{.Ansi5}}
*color13:     {{.Ansi13}}
! Cyan
*color6:      {{.Ansi6}}
*color14:     {{.Ansi14}}
! White
*color7:      {{.Ansi7}}
*color15:     {{.Ansi15}}
! Bold
*colorBD:     {{.Bold}}
!*colorIT:
!*colorUL:
`

type Scheme struct {
	Name         string
	Ansi0        Color `plist:"Ansi 0 Color"`
	Ansi1        Color `plist:"Ansi 1 Color"`
	Ansi2        Color `plist:"Ansi 2 Color"`
	Ansi3        Color `plist:"Ansi 3 Color"`
	Ansi4        Color `plist:"Ansi 4 Color"`
	Ansi5        Color `plist:"Ansi 5 Color"`
	Ansi6        Color `plist:"Ansi 6 Color"`
	Ansi7        Color `plist:"Ansi 7 Color"`
	Ansi8        Color `plist:"Ansi 8 Color"`
	Ansi9        Color `plist:"Ansi 9 Color"`
	Ansi10       Color `plist:"Ansi 10 Color"`
	Ansi11       Color `plist:"Ansi 11 Color"`
	Ansi12       Color `plist:"Ansi 12 Color"`
	Ansi13       Color `plist:"Ansi 13 Color"`
	Ansi14       Color `plist:"Ansi 14 Color"`
	Ansi15       Color `plist:"Ansi 15 Color"`
	Background   Color `plist:"Background Color"`
	Badge        Color `plist:"Badge Color"`
	Bold         Color `plist:"Bold Color"`
	Cursor       Color `plist:"Cursor Color"`
	CursorGuide  Color `plist:"Cursor Guide Color"`
	CursorText   Color `plist:"Cursor Text Color"`
	Foreground   Color `plist:"Foreground Color"`
	Link         Color `plist:"Link Color"`
	SelectedText Color `plist:"Selected Text Color"`
	Selection    Color `plist:"Selection Color"`
}

type Color struct {
	Alpha float64 `plist:"Alpha Component"`
	Red   float64 `plist:"Red Component"`
	Green float64 `plist:"Green Component"`
	Blue  float64 `plist:"Blue Component"`
	Space string  `plist:"Color Space"`
}

func (c Color) String() string {
	r := int(math.MaxUint8 * c.Red)
	g := int(math.MaxUint8 * c.Green)
	b := int(math.MaxUint8 * c.Blue)
	return fmt.Sprintf("#%02x%02x%02x", r, g, b)
}

func name(s string) string {
	return strings.TrimSuffix(filepath.Base(s), filepath.Ext(s))
}

func main() {
	file := flag.String("file", "", "Color scheme .itemcolors")
	flag.Parse()
	body, err := ioutil.ReadFile(*file)
	if err != nil {
		log.Fatal(err)
	}
	var s Scheme
	if _, err := plist.Unmarshal(body, &s); err != nil {
		log.Fatal(err)
	}
	tmpl, err := template.New("xrdb").Parse(t)
	if err != nil {
		log.Fatal(err)
	}
	s.Name = name(*file)
	tmpl.Execute(os.Stdout, s)
}