-
Notifications
You must be signed in to change notification settings - Fork 0
/
color-swifter.py
109 lines (83 loc) · 2.63 KB
/
color-swifter.py
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
from bs4 import BeautifulSoup
import requests
lowerMe = lambda s: s[:1].lower() + s[1:] if s else ''
'''
downloadSource
--------------
Scrapes https://material.google.com/style/color.html#color-color-palette
'''
def downloadSource():
r = requests.get('https://material.google.com/style/color.html#color-color-palette')
return r.text
'''
parsePage
---------
Parses page to create Dictionary of Colors
Ex: {mainColor: {'shade': 'hex'}, ... }
'''
def parsePage(page):
colorDict = {}
uiColorsArray = []
xmlArray = []
mainColor = ""
shadesArray = []
hexArray = []
soup = BeautifulSoup(page, "html.parser")
colorGroups = soup.find_all("section", class_="color-group")
for group in colorGroups:
# Get Key
name = group.find_all("span", class_="name")
color = group.find_all("span", class_="shade")
hexValue= group.find_all("span", class_="hex")
for n in name:
mainColor = str(n.text.strip())
for c in color:
shadesArray.append(str(c.text.strip()))
for h in hexValue:
hexArray.append(str(h.text.strip()))
colorDict[mainColor] = dict(zip(shadesArray, hexArray))
for mainColor in colorDict:
for shade in colorDict[mainColor]:
shadeName = mainColor.replace(" ", "") + shade
shadeName = lowerMe(shadeName)
hex = colorDict[mainColor][shade]
r = round(float(int(hex[1:3], 16)) / 255, 2)
g = round(float(int(hex[3:5], 16)) / 255, 2)
b = round(float(int(hex[5:7], 16)) / 255, 2)
uicolor = '{}: UIColor {{return UIColor(red:{}, green:{}, blue:{}, alpha:1.0)}} // {}'\
'\n'.format(shadeName, r, g, b, hex)
xml = '{}">{} </color>\n'.format(shadeName, hex)
uiColorsArray.append(uicolor)
xmlArray.append(xml)
uiColorsArray = sorted(uiColorsArray)
xmlArray = sorted(xmlArray)
for i, (uicolor, xml) in enumerate(zip(uiColorsArray, xmlArray)):
uiColorsArray[i] = " public class var " + uicolor
xmlArray[i] = ' <color name="' + xml
return [uiColorsArray,xmlArray]
'''
createFile
---------
Creates MaterialColors.swift and colors.xml, saves to directory where script is located
'''
def createFile(colorsArray):
# Create swift file
file = open("MaterialColors.swift", "w")
file.write("import UIKit\n\nextension UIColor {\n")
for item in colorsArray[0]:
file.write(item)
file.write("}")
# Create xml file
file = open("colors.xml", "w")
file.write('<?xml version="1.0" encoding="utf-8"?>\n<resources>\n')
for item in colorsArray[1]:
file.write(item)
file.write("</resources>")
def main():
page = downloadSource()
# key value of all colors {color: {uicolor: "", Hex: ""}}
colors = parsePage(page)
createFile(colors)
if __name__ == "__main__":
# execute only if run as a script
main()