Skip to content

Commit

Permalink
Version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
adrium committed Dec 19, 2022
0 parents commit 8a9de35
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# TXT and JSON to Mac CLR converter

This script can be used convert TXT and JSON files to an Apple Mac .clr color palette file.

## Examples

### TXT


* [Html2Clr](https://github.com/ramonpoca/ColorTools)

```
#0ff aqua
#8B008B darkmagenta
#0003 Black 20%
```

### JSON

Compatible with:

* [eip/JSON To CLR.applescript.js](https://gist.github.com/eip/3837dfdaeb0326d44cf60c65f59e74c2)
* [IORoot/macos__json2clr--convert](https://github.com/IORoot/macos__json2clr--convert)

```json
{
"aqua": "#0ff",
"darkmagenta": "#8B008B",
"Black 20%": "#0003"
}
```
33 changes: 33 additions & 0 deletions any2clr.applescript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
ObjC.import('AppKit');

var app = Application.currentApplication()
app.includeStandardAdditions = true

var input = app.chooseFile({ofType: ["txt", "json"]})
var name = input.toString().match(/.+\/([^.]+)/)[1]
var output = app.chooseFileName({ defaultName: name + '.clr' })
var contents = app.read(input)
var nsclrlist = $.NSColorList.alloc.initWithName(name);
var colors = []

if (contents[0] == "{") {
colors = JSON.parse(contents)
colors = Object.keys(colors).map(s => ({ name: s, color: colors[s] }))
} else {
colors = contents.split("\n")
colors = colors.map(str => str.match(/(\S+)\s+(.+)/))
colors = colors.filter(match => match)
colors = colors.map(match => ({ name: match[2], color: match[1] }))
}
colors.forEach(o => {
if (o.color.startsWith("#"))
o.color = o.color.substring(1)
if (o.color.length < 6)
o.color = o.color.split('').reduce((str, hex) => str + hex + hex, '')
o.color += 'ff'
; [o.r, o.g, o.b, o.a] = o.color.match(/../g).map(hex => parseInt(hex, 16) / 255)
})

colors.forEach((o, i) => o.nscolor = $.NSColor.colorWithCalibratedRedGreenBlueAlpha(o.r, o.g, o.b, o.a))
colors.forEach((o, i) => nsclrlist.insertColorKeyAtIndex(o.nscolor, o.name, i))
nsclrlist.writeToFile(output.toString());

0 comments on commit 8a9de35

Please sign in to comment.