This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
xibcolor
executable file
·178 lines (160 loc) · 5.99 KB
/
xibcolor
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
#
# Storyboard/xib (xml based) search and replace colors
#
import sys
from xml.dom.minidom import parse
# <color key="titleColor" red="0.30196079609999998" green="0.30196079609999998" blue="0.30196079609999998" alpha="1" colorSpace="calibratedRGB"/>
# <color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
# <color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
debugMode = False
def clamp(x):
return max(0, min(int(round(x)), 255))
def argbtohex(a, r, g, b):
return "#{0:02x}{1:02x}{2:02x}{3:02x}".format(clamp(255.0*a), clamp(255.0*r), clamp(255.0*g), clamp(255.0*b))
def hextoargb(hx):
if hx[0] == '#':
hx = hx[1:]
result = tuple(ord(c)/255.0 for c in hx.decode('hex'))
if len(hx) == 6:
result = (1.0,) + result # Add alpha of 1.0
return result
def listcolors(dom):
allcolors = []
for node in dom.getElementsByTagName('color'): # visit every node <bar />
colsp = node.getAttribute('colorSpace')
customColorSpace = node.getAttribute('customColorSpace')
if debugMode:
print "---- [Debug]"
if colsp == 'calibratedRGB' or colsp == 'deviceRGB':
red = node.getAttribute('red')
green = node.getAttribute('green')
blue = node.getAttribute('blue')
alpha = node.getAttribute('alpha')
if debugMode:
print "Red: " + red
print "Green: " + green
print "Blue: " + blue
print "Alpha: " + alpha
hexv = argbtohex(float(alpha), float(red), float(green), float(blue))
allcolors.append(hexv)
elif colsp == "calibratedWhite":
alpha = node.getAttribute('alpha')
white = node.getAttribute('white')
if debugMode:
print "White: " + white
hexv = argbtohex(float(alpha), float(white), float(white), float(white))
allcolors.append(hexv)
elif colsp == "custom" and customColorSpace == "calibratedWhite":
alpha = node.getAttribute('alpha')
white = node.getAttribute('white')
if debugMode:
print "White: " + white
hexv = argbtohex(float(alpha), float(white), float(white), float(white))
allcolors.append(hexv)
elif colsp == "custom" and customColorSpace == "sRGB":
red = node.getAttribute('red')
green = node.getAttribute('green')
blue = node.getAttribute('blue')
alpha = node.getAttribute('alpha')
if debugMode:
print "Red: " + red
print "Green: " + green
print "Blue: " + blue
print "Alpha: " + alpha
hexv = argbtohex(float(alpha), float(red), float(green), float(blue))
allcolors.append(hexv)
else:
if debugMode:
print "colorSpace: " + colsp
print "colorSpace: " + customColorSpace
print "keys: " + ", ".join(node.attributes.keys())
if debugMode:
print "---- [Extracted Colors]"
for col in set(allcolors):
print col
if debugMode:
print "----"
return allcolors
def replacecolor(dom, fromcol, tocol):
didreplace = False
(toa, tor, tog, tob) = hextoargb(tocol)
for node in dom.getElementsByTagName('color'): # visit every node <bar />
colsp = node.getAttribute('colorSpace')
if colsp == 'calibratedRGB' or colsp == 'deviceRGB':
red = node.getAttribute('red')
green = node.getAttribute('green')
blue = node.getAttribute('blue')
alpha = node.getAttribute('alpha')
hexv = argbtohex(float(alpha), float(red), float(green), float(blue))
if not fromcol:
print hexv
if fromcol and hexv == fromcol.lower():
node.setAttribute('red', str(tor))
node.setAttribute('green', str(tog))
node.setAttribute('blue', str(tob))
node.setAttribute('alpha', str(toa))
didreplace = True
return didreplace
def generateAllColors(inputFile, outputFile):
with open(inputFile) as f:
content = f.readlines()
cleanedContent = [x.strip() for x in content]
# print cleanedContent
outputColors = []
for aFile in cleanedContent:
dom = parse(aFile)
if dom is None:
print "Cannot open %s" % xibOrStoryboard
sys.exit()
outputColors.extend(listcolors(dom))
outputFileHandler = open(outputFile, 'w')
for line in set(outputColors):
outputFileHandler.write("%s\n" % line)
def usage():
print "usage:"
print "xibcolor file [-l | replacements | [\"#fromcolor\" \"#tocolor\"] | -j filelist.txt output.txt]"
print "file a .storyboard or xib in new XML format"
print "-l list colors present in the file"
print "-j export all the colors find in the xibs and storyboard from the filelist et into a single file"
print "replacements a file consisting of pairs of hex-coded from->to colors"
print "#color a pair of colors to replace"
sys.exit(0)
if len(sys.argv) == 1:
usage()
if len(sys.argv) == 2 and sys.argv[1] == '-h':
usage()
if len(sys.argv) == 4 and sys.argv[1] == '-j':
generateAllColors(sys.argv[2], sys.argv[3])
else:
xibOrStoryboard = sys.argv[1]
if debugMode:
print "Filename: " + xibOrStoryboard
dom = parse(xibOrStoryboard)
if dom is None:
print "Cannot open %s" % xibOrStoryboard
usage()
if len(sys.argv) == 3 and sys.argv[2] == '-l':
listcolors(dom)
elif len(sys.argv) == 3 and sys.argv[2] != '-l':
with open(sys.argv[2], 'r') as colors:
countOfReplacements = 0
for line in colors:
if len(line) > 0:
(fcol,tocol,) = line.split()
countOfReplacements += replacecolor(dom, fcol, tocol)
if countOfReplacements > 0:
with open(sys.argv[1], 'w') as ofile:
ofile.write(dom.toxml().encode('utf-8'))
print "%d colors replaced in %s" % (countOfReplacements, xibOrStoryboard)
else:
print "No colors were replaced in %s" % xibOrStoryboard
elif len(sys.argv) == 4:
fromcol = sys.argv[2]
tocol = sys.argv[3]
print "From: %s to: %s" % (fromcol, tocol)
if replacecolor(dom, fromcol, tocol):
with open('out.storyboard', 'w') as ofile:
ofile.write(dom.toxml().encode('utf-8'))
else:
usage()