Add theme generator

This commit is contained in:
Lonami Exo 2017-01-31 11:59:08 +01:00
parent 00b4c74585
commit c72e260a23
2 changed files with 110 additions and 20 deletions

View file

@ -0,0 +1,85 @@
#!/usr/bin/env python3
# Generates a theme from `theme.svg`
import re
import os
import subprocess
color_re = \
re.compile('<g\s+id="export_(\w+)"\s+style=".*?fill:#([0-9a-f]+)[\S\s]*?')
template = '''{{
"name": "{name}",
"price": {price},
"colors": {{
"background": "{background}",
"buttons": [
"{button_0}",
"{button_1}",
"{button_2}",
"{button_3}"
],
"empty_cell": "{empty_cell}",
"cells": [
"{cell_0}", "{cell_1}", "{cell_2}",
"{cell_3}", "{cell_4}", "{cell_5}", "{cell_6}",
"{cell_7}", "{cell_8}"
],
"current_score": "{current_score}",
"high_score": "{high_score}"
}},
"cell_texture": "{cell_tex}"
}}
'''
def price_ok(price):
try:
price = int(price)
if price < 0:
raise ValueError('Price must be ≥ 0.')
except:
print('Invalid price detected. Using 0.')
return False
return True
def main():
if not os.path.isfile('in.svg'):
print('Error: No "in.svg" found. Aborting.')
return
print('Reading "in.svg"')
with open('in.svg', 'r', encoding='utf-8') as f:
xml = f.read().replace('\n', '')
print('Finding used colors…')
replacements = {}
for m in color_re.finditer(xml):
# Append 'ff' because the themes require the alpha to be set
replacements[m.group(1)] = m.group(2)+'ff'
print('Almost done, we only need some more information.')
replacements['name'] = input('Enter theme name: ')
replacements['price'] = input('Enter theme price: ')
replacements['cell_tex'] = \
input('Enter cell texture (default "basic.png"): ')
if not replacements['price'] or not price_ok(replacements['price']):
print('Invalid price detected. Using 0.')
replacements['price'] = 0
if not replacements['cell_tex']:
print('No texture specified. Using default "basic.png" texture.')
replacements['cell_tex'] = 'basic.png'
print('Generating theme…')
with open('out.theme', 'w', encoding='utf-8') as f:
f.write(template.format_map(replacements))
print('Done!')
if __name__ == '__main__':
main()

View file

@ -25,9 +25,9 @@
borderopacity="1.0" borderopacity="1.0"
inkscape:pageopacity="0.0" inkscape:pageopacity="0.0"
inkscape:pageshadow="2" inkscape:pageshadow="2"
inkscape:zoom="0.7" inkscape:zoom="0.49497474"
inkscape:cx="484.58357" inkscape:cx="517.48669"
inkscape:cy="357.4122" inkscape:cy="678.08613"
inkscape:document-units="px" inkscape:document-units="px"
inkscape:current-layer="layer1" inkscape:current-layer="layer1"
showgrid="false" showgrid="false"
@ -36,7 +36,8 @@
inkscape:window-x="0" inkscape:window-x="0"
inkscape:window-y="0" inkscape:window-y="0"
inkscape:window-maximized="1" inkscape:window-maximized="1"
units="px" /> units="px"
showguides="false" />
<metadata <metadata
id="metadata7"> id="metadata7">
<rdf:RDF> <rdf:RDF>
@ -45,7 +46,7 @@
<dc:format>image/svg+xml</dc:format> <dc:format>image/svg+xml</dc:format>
<dc:type <dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title> <dc:title />
</cc:Work> </cc:Work>
</rdf:RDF> </rdf:RDF>
</metadata> </metadata>
@ -169,27 +170,31 @@
</g> </g>
<text <text
xml:space="preserve" xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#f9f9f9;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;" style="font-style:normal;font-weight:normal;font-size:40px;line-height:125%;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#f9f9f9;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
x="20" x="-10.304577"
y="309.27737" y="289.07431"
id="text4167" id="text4167"
sodipodi:linespacing="125%"><tspan sodipodi:linespacing="125%"><tspan
sodipodi:role="line" sodipodi:role="line"
id="tspan4169" x="-10.304577"
x="20" y="289.07431"
y="309.27737">Modify the colors from these screens</tspan><tspan id="tspan4175">To easily create a custom theme, copy</tspan><tspan
sodipodi:role="line" sodipodi:role="line"
x="20" x="-10.304577"
y="359.27737" y="339.07431"
id="tspan4171">and then run `generate_theme.py`</tspan><tspan id="tspan4183">this file and rename it as &quot;in.svg&quot;. Place</tspan><tspan
sodipodi:role="line" sodipodi:role="line"
x="20" x="-10.304577"
y="409.27737" y="389.07431"
id="tspan4173">to create a `generated.theme`, ready</tspan><tspan id="tspan4185">it under the same folder, and make your</tspan><tspan
sodipodi:role="line" sodipodi:role="line"
x="20" x="-10.304577"
y="459.27737" y="439.07431"
id="tspan4175">to be dropped under `assets/themes`</tspan></text> id="tspan4187">changes on the new file. Then simply run</tspan><tspan
sodipodi:role="line"
x="-10.304577"
y="489.07431"
id="tspan4189">&quot;generate_theme.py&quot; to create &quot;out.theme&quot;</tspan></text>
<g <g
id="export_background" id="export_background"
style="fill:#e5e5e5;fill-opacity:1"> style="fill:#e5e5e5;fill-opacity:1">

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 13 KiB