Skip to content

Commit 8a9de35

Browse files
committed
Version 1
0 parents  commit 8a9de35

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# TXT and JSON to Mac CLR converter
2+
3+
This script can be used convert TXT and JSON files to an Apple Mac .clr color palette file.
4+
5+
## Examples
6+
7+
### TXT
8+
9+
10+
* [Html2Clr](https://github.com/ramonpoca/ColorTools)
11+
12+
```
13+
#0ff aqua
14+
#8B008B darkmagenta
15+
#0003 Black 20%
16+
```
17+
18+
### JSON
19+
20+
Compatible with:
21+
22+
* [eip/JSON To CLR.applescript.js](https://gist.github.com/eip/3837dfdaeb0326d44cf60c65f59e74c2)
23+
* [IORoot/macos__json2clr--convert](https://github.com/IORoot/macos__json2clr--convert)
24+
25+
```json
26+
{
27+
"aqua": "#0ff",
28+
"darkmagenta": "#8B008B",
29+
"Black 20%": "#0003"
30+
}
31+
```

any2clr.applescript.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
ObjC.import('AppKit');
2+
3+
var app = Application.currentApplication()
4+
app.includeStandardAdditions = true
5+
6+
var input = app.chooseFile({ofType: ["txt", "json"]})
7+
var name = input.toString().match(/.+\/([^.]+)/)[1]
8+
var output = app.chooseFileName({ defaultName: name + '.clr' })
9+
var contents = app.read(input)
10+
var nsclrlist = $.NSColorList.alloc.initWithName(name);
11+
var colors = []
12+
13+
if (contents[0] == "{") {
14+
colors = JSON.parse(contents)
15+
colors = Object.keys(colors).map(s => ({ name: s, color: colors[s] }))
16+
} else {
17+
colors = contents.split("\n")
18+
colors = colors.map(str => str.match(/(\S+)\s+(.+)/))
19+
colors = colors.filter(match => match)
20+
colors = colors.map(match => ({ name: match[2], color: match[1] }))
21+
}
22+
colors.forEach(o => {
23+
if (o.color.startsWith("#"))
24+
o.color = o.color.substring(1)
25+
if (o.color.length < 6)
26+
o.color = o.color.split('').reduce((str, hex) => str + hex + hex, '')
27+
o.color += 'ff'
28+
; [o.r, o.g, o.b, o.a] = o.color.match(/../g).map(hex => parseInt(hex, 16) / 255)
29+
})
30+
31+
colors.forEach((o, i) => o.nscolor = $.NSColor.colorWithCalibratedRedGreenBlueAlpha(o.r, o.g, o.b, o.a))
32+
colors.forEach((o, i) => nsclrlist.insertColorKeyAtIndex(o.nscolor, o.name, i))
33+
nsclrlist.writeToFile(output.toString());

0 commit comments

Comments
 (0)