forked from ramonpoca/ColorTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xibcolor
executable file
·107 lines (91 loc) · 3.36 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
#!/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"/>
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')
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))
allcolors.append(hexv)
for col in set(allcolors):
print col
def replacecolor(dom, fromcol, tocol):
(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))
return True
return False
def usage():
print "usage:"
print "xibcolor file [-l|replacements|[\"#fromcolor\" \"#tocolor\"]]"
print "file a .storyboard or xib in new XML format"
print "-l list colors present in the 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()
xibOrStoryboard = sys.argv[1]
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()