|
| 1 | +// Forked from https://git.dim13.org/itermcolors.git/tree/main.go |
| 2 | +// Inspired by https://github.com/F1LT3R/itermcolors-to-hex/blob/master/index.js |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "math" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "text/template" |
| 14 | + |
| 15 | + "howett.net/plist" |
| 16 | +) |
| 17 | + |
| 18 | +type Scheme struct { |
| 19 | + Name string |
| 20 | + Ansi0 Color `plist:"Ansi 0 Color"` |
| 21 | + Ansi1 Color `plist:"Ansi 1 Color"` |
| 22 | + Ansi2 Color `plist:"Ansi 2 Color"` |
| 23 | + Ansi3 Color `plist:"Ansi 3 Color"` |
| 24 | + Ansi4 Color `plist:"Ansi 4 Color"` |
| 25 | + Ansi5 Color `plist:"Ansi 5 Color"` |
| 26 | + Ansi6 Color `plist:"Ansi 6 Color"` |
| 27 | + Ansi7 Color `plist:"Ansi 7 Color"` |
| 28 | + Ansi8 Color `plist:"Ansi 8 Color"` |
| 29 | + Ansi9 Color `plist:"Ansi 9 Color"` |
| 30 | + Ansi10 Color `plist:"Ansi 10 Color"` |
| 31 | + Ansi11 Color `plist:"Ansi 11 Color"` |
| 32 | + Ansi12 Color `plist:"Ansi 12 Color"` |
| 33 | + Ansi13 Color `plist:"Ansi 13 Color"` |
| 34 | + Ansi14 Color `plist:"Ansi 14 Color"` |
| 35 | + Ansi15 Color `plist:"Ansi 15 Color"` |
| 36 | + Background Color `plist:"Background Color"` |
| 37 | + Badge Color `plist:"Badge Color"` |
| 38 | + Bold Color `plist:"Bold Color"` |
| 39 | + Cursor Color `plist:"Cursor Color"` |
| 40 | + CursorGuide Color `plist:"Cursor Guide Color"` |
| 41 | + CursorText Color `plist:"Cursor Text Color"` |
| 42 | + Foreground Color `plist:"Foreground Color"` |
| 43 | + Link Color `plist:"Link Color"` |
| 44 | + SelectedText Color `plist:"Selected Text Color"` |
| 45 | + Selection Color `plist:"Selection Color"` |
| 46 | +} |
| 47 | + |
| 48 | +type Color struct { |
| 49 | + Alpha float64 `plist:"Alpha Component"` |
| 50 | + Red float64 `plist:"Red Component"` |
| 51 | + Green float64 `plist:"Green Component"` |
| 52 | + Blue float64 `plist:"Blue Component"` |
| 53 | + Space string `plist:"Color Space"` |
| 54 | +} |
| 55 | + |
| 56 | +func (c Color) String() string { |
| 57 | + r := int(math.MaxUint8 * c.Red) |
| 58 | + g := int(math.MaxUint8 * c.Green) |
| 59 | + b := int(math.MaxUint8 * c.Blue) |
| 60 | + return fmt.Sprintf("#%02x%02x%02x", r, g, b) |
| 61 | +} |
| 62 | + |
| 63 | +func name(s string) string { |
| 64 | + return strings.TrimSuffix(filepath.Base(s), filepath.Ext(s)) |
| 65 | +} |
| 66 | + |
| 67 | +func exitErr(code int, err error) { |
| 68 | + fmt.Println(err) |
| 69 | + os.Exit(code) |
| 70 | +} |
| 71 | + |
| 72 | +func main() { |
| 73 | + file := flag.String("file", "", "Color scheme .itemcolors") |
| 74 | + tmpl := flag.String("tmpl", "scheme.tmpl", "Xresources template") |
| 75 | + out := flag.String("out", "-", "output") |
| 76 | + flag.Parse() |
| 77 | + |
| 78 | + body, err := ioutil.ReadFile(*file) |
| 79 | + if err != nil { |
| 80 | + exitErr(1, err) |
| 81 | + } |
| 82 | + |
| 83 | + var s Scheme |
| 84 | + if _, err := plist.Unmarshal(body, &s); err != nil { |
| 85 | + exitErr(1, err) |
| 86 | + } |
| 87 | + s.Name = name(*file) |
| 88 | + fd := os.Stdout |
| 89 | + |
| 90 | + if *out != "-" { |
| 91 | + fd, err = os.Create(*out) |
| 92 | + if err != nil { |
| 93 | + exitErr(1, err) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + t, err := template.ParseFiles(*tmpl) |
| 98 | + if err != nil { |
| 99 | + exitErr(1, err) |
| 100 | + } |
| 101 | + |
| 102 | + if err := t.Execute(fd, s); err != nil { |
| 103 | + exitErr(1, err) |
| 104 | + } |
| 105 | +} |
0 commit comments