Skip to content

Instantly share code, notes, and snippets.

@eip
Last active December 15, 2024 04:02
Show Gist options
  • Save eip/ba9e0d1a4cb5e9e4133d0170b9d340b4 to your computer and use it in GitHub Desktop.
Save eip/ba9e0d1a4cb5e9e4133d0170b9d340b4 to your computer and use it in GitHub Desktop.
Convert Apple Color List (.CLR) to JSON
var app;
var clrFilePath;
var colorList = {};
var jsonFilePath;
var nsColorList;
var nsStringColorList;
var paletteName;
function c2h(colorValue) {
return ('0' + Math.round(colorValue * 255).toString(16)).slice(-2);
}
function a2h(alphaValue) {
var result = c2h(alphaValue);
return result === 'ff' ? '' : result;
}
ObjC.import('AppKit');
app = Application.currentApplication();
app.includeStandardAdditions = true;
// get file UIT: app.infoFor(clrFilePath).typeIdentifier;
clrFilePath = app.chooseFile({ withPrompt: 'Select the .clr file', ofType: ['dyn.ah62d4rv4ge80g5dw'] }).toString();
paletteName = clrFilePath.slice(clrFilePath.lastIndexOf('/') + 1);
paletteName = paletteName.slice(0, paletteName.lastIndexOf('.'));
nsColorList = $.NSColorList.alloc.initWithNameFromFile(paletteName, clrFilePath);
nsColorList.allKeys.js.forEach(function _getColor(colorName) {
var nsColor;
var colorValue;
nsColor = nsColorList.colorWithKey(colorName);
if (nsColor.colorSpaceName.js === 'NSCalibratedWhiteColorSpace') {
colorValue = ['#', c2h(nsColor.whiteComponent), c2h(nsColor.whiteComponent), c2h(nsColor.whiteComponent), a2h(nsColor.alphaComponent)].filter(Boolean).join('');
} else {
colorValue = ['#', c2h(nsColor.redComponent), c2h(nsColor.greenComponent), c2h(nsColor.blueComponent), a2h(nsColor.alphaComponent)].filter(Boolean).join('');
}
colorList[colorName.js] = colorValue;
});
nsStringColorList = $.NSString.alloc.initWithUTF8String(JSON.stringify(colorList, null, 2));
jsonFilePath = app.chooseFileName({ withPrompt: 'Select the .json file', defaultName: paletteName + '.json' }).toString();
nsStringColorList.writeToFileAtomically(jsonFilePath, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment