Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mwinters0 committed Oct 27, 2024
0 parents commit c30fae5
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Michael Winters

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# itermtohex
Convert iTerm colors to hex. (In a single binary!)

## Install
```shell
go install github.com/mwinters0/itermtohex@latest
```

Or see the [releases](https://github.com/mwinters0/itermtohex/releases) page.

## Usage
```shell
itermtohex my-theme.itermcolors # convert to json and output on stdout
itermtohex print my-theme.itermcolors # print the converted color palette (assumes RGB support)
```
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/mwinters0/itermtohex

go 1.23.2
112 changes: 112 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"encoding/json"
"encoding/xml"
"fmt"
"os"
)

type plist struct {
Dict []dict `xml:"dict"`
}

type dict struct {
Key []string `xml:"key"`
Real []float64 `xml:"real"`
Dict []dict `xml:"dict"`
}

type Color struct {
Name string
RFloat float64
GFloat float64
BFloat float64
RInt uint8
GInt uint8
BInt uint8
Hex string
}

func die(y string) {
fmt.Println("error: " + y)
os.Exit(1)
}

func usage() {
fmt.Println(fmt.Sprintf(
"Usage:\n %s myfile.itermcolors\n %s print myfile.itermcolors",
os.Args[0], os.Args[0],
))
os.Exit(1)
}

func convert(filename string) []Color {
contents, err := os.ReadFile(filename)
if err != nil {
die(err.Error())
}
var p plist
err = xml.Unmarshal(contents, &p)
if err != nil {
die(err.Error())
}

var colors []Color
mainDict := p.Dict[0]
for i, colorName := range mainDict.Key {
c := Color{
Name: colorName,
}
c.BFloat = mainDict.Dict[i].Real[0]
c.GFloat = mainDict.Dict[i].Real[1]
c.RFloat = mainDict.Dict[i].Real[2]
c.RInt = uint8(c.RFloat * 255)
c.GInt = uint8(c.GFloat * 255)
c.BInt = uint8(c.BFloat * 255)
c.Hex = fmt.Sprintf("#%02x%02x%02x", c.RInt, c.GInt, c.BInt)
colors = append(colors, c)
}
return colors
}

func main() {
var filename string
var print bool
switch len(os.Args) {
case 2:
filename = os.Args[1]
case 3:
if os.Args[1] != "print" {
usage()
}
filename = os.Args[2]
print = true
default:
usage()
}

colors := convert(filename)
if print {
printColor := func(c Color) {
fmt.Printf(
"%s (%s): \033[48;2;%d;%d;%dm \033[0m\n",
c.Name,
c.Hex,
c.RInt,
c.GInt,
c.BInt,
)
}
for _, c := range colors {
printColor(c)
}
return
}

j, err := json.MarshalIndent(colors, "", " ")
if err != nil {
die(err.Error())
}
fmt.Println(string(j))
}

0 comments on commit c30fae5

Please sign in to comment.