-
Notifications
You must be signed in to change notification settings - Fork 5
/
Generate UIColor Category.sketchplugin
73 lines (56 loc) · 1.71 KB
/
Generate UIColor Category.sketchplugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#import 'sandbox.js'
#import 'objc-config.js'
#import 'common.js'
var swatchLayers = getSwatchLayers();
var methodSignatures = [];
var uicolorCodes = [];
var prefix = [doc askForUserInput:"What is your category prefix?" initialValue:"SKC"];
for (var i=0; i < [swatchLayers count]; i++)
{
var layer = [swatchLayers objectAtIndex:i];
if ([layer isMemberOfClass:[MSShapeGroup class]])
{
var swatchName = layer.name();
if (swatchName != "untitled color")
{
swatchName = makeNiceName(swatchName);
methodSignatures.push(generateMethodSignature(swatchName, prefix));
uicolorCodes.push(generateFillColor(layer));
}
}
}
var mText = commentHeader + mPrefix;
var hText = commentHeader + hPrefix;
for (var i=0; i < methodSignatures.length; i++)
{
mText += methodSignatures[i];
mText += newLine;
mText += "{";
mText += newLine;
mText += "\t" + uicolorCodes[i];
mText += newLine;
mText += "}";
mText += newLine;
mText += newLine;
hText += methodSignatures[i] + ";";
hText += newLine;
}
mText += end;
hText += end;
var mPath = getDocumentPath() + "UIColor+Sketch.m";
var hPath = getDocumentPath() + "UIColor+Sketch.h";
saveFile(mPath, mText);
saveFile(hPath, hText);
function generateMethodSignature(name, prefix)
{
return "+ (UIColor *)" + prefix + name;
}
function generateFillColor(layer)
{
var fill = layer.style().fills().firstObject();
var red = fill.color().red().toFixed(3).toString();
var green = fill.color().green().toFixed(3).toString();
var blue = fill.color().blue().toFixed(3).toString();
var alpha = fill.color().alpha().toFixed(3).toString();
return "return [UIColor colorWithRed:" + red + " green:" + green + " blue:" + blue + " alpha:" + alpha + "];";
}