Skip to content

Instantly share code, notes, and snippets.

@farbodsz
Last active August 3, 2017 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farbodsz/371b916d16af46741c313a04a6708d4d to your computer and use it in GitHub Desktop.
Save farbodsz/371b916d16af46741c313a04a6708d4d to your computer and use it in GitHub Desktop.
Python script to convert color resource values in an Android project's `colors.xml` so they instead reference values from my MaterialDesignUtils library - see https://github.com/FarbodSalamat-Zadeh/MaterialDesignUtils

MDU File Converter

This Python program can be used to convert colors in your Android project's values.xml file, so that they reference values from the MaterialDesignUtils library.

For example, your file may contain <color name="theme_primary">#F44336</color>, and this will be replaced with <color name="theme_primary">@color/mdu_red_500</color>. This makes it much easier to read what color you have set to theme_primary.

The MDU File Converter program is particularly useful if you have added or are using the MaterialDesignUtils library, as your color values will be referencing that library instead of lots of hex values.

Copyright

Copyright 2016 Farbod Salamat-Zadeh

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
# coding utf-8
# Copyright 2016 Farbod Salamat-Zadeh
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Reads a color xml file and replaces Material Design colors with references to colors from the MaterialDesignUtils
library for Android.
The color xml file is located by its full path from user input, and the new file is created and written in the same
directory, except with the name 'colors_new.xml'
"""
import os.path
def log(message):
"""Prints a log message"""
# print("LOG : " + message)
pass
def convert(path):
"""Reads the specified color XML file and writes a new one, replacing Material Design colors with references to
colors from the MaterialDesignUtils library for Android.
:param path: The full path of the file to be 'converted'
"""
list_file = open("colors_list.csv", "r")
color_list = {}
for line in list_file:
values = line.split(",")
val_hex = "#" + str(values[2].strip("\n")) # 3rd column of csv
new_name = "@color/mdu_" + values[0] + "_" + values[1]
color_list[val_hex] = new_name
log("Created dictionary from CSV file")
old_file = open(path, "r+")
path_pair = os.path.split(path)
new_file = open(path_pair[0] + "\colors_new.xml", "a")
small_tab = " " * 2
tab = " " * 4
description = "Generated by the MaterialDesignUtils File Converter library, from your previous color XML file"
new_file.write('<?xml version="1.0" encoding="utf-8"?>\n')
new_file.write("<!--\n" + small_tab + description + "\n" + small_tab + "-->")
new_file.write("\n<resources>\n")
log("Generated start tags")
for line in old_file:
has_written = False
log("Processing: " + line.strip("\n"))
if "<color name=" in line: # checks for color tag
for key in color_list: # iterates through all colors
if key in line: # if line contains a hex from the dictionary
parts = line.split("#")
new_file.write(parts[0] + color_list[key] + "</color>\n")
log(tab + "Replaced color with MDU ref: " + color_list[key])
has_written = True
if not has_written: # if contains color, but is not material design color
new_file.write(line)
else:
if "<?xml" in line \
or "<resources>" in line \
or "</resources>" in line:
log(tab + "Cannot rewrite this line")
else:
new_file.write(line)
new_file.write("</resources>")
log("Generated end tag")
print("\nThe new file has been created!")
while True:
f_path = input("\nEnter the full path and name of the XML file to be converted: ")
if not f_path.endswith(".xml"):
f_path += ".xml"
if os.path.isfile(f_path):
print("\nConverting...")
convert(f_path)
break
else:
print("This file does not exist!")
color_group color_variant hex
red 50 FFEBEE
red 100 FFCDD2
red 200 EF9A9A
red 300 E57373
red 400 EF5350
red 500 F44336
red 600 E53935
red 700 D32F2F
red 800 C62828
red 900 B71C1C
red a100 FF8A80
red a200 FF5252
red a400 FF1744
red a700 D50000
pink 50 FCE4EC
pink 100 F8BBD0
pink 200 F48FB1
pink 300 F06292
pink 400 EC407A
pink 500 E91E63
pink 600 D81B60
pink 700 C2185B
pink 800 AD1457
pink 900 880E4F
pink a100 FF80AB
pink a200 FF4081
pink a400 F50057
pink a700 C51162
purple 50 F3E5F5
purple 100 E1BEE7
purple 200 CE93D8
purple 300 BA68C8
purple 400 AB47BC
purple 500 9C27B0
purple 600 8E24AA
purple 700 7B1FA2
purple 800 6A1B9A
purple 900 4A148C
purple a100 EA80FC
purple a200 E040FB
purple a400 D500F9
purple a700 AA00FF
deep_purple 50 EDE7F6
deep_purple 100 D1C4E9
deep_purple 200 B39DDB
deep_purple 300 9575CD
deep_purple 400 7E57C2
deep_purple 500 673AB7
deep_purple 600 5E35B1
deep_purple 700 512DA8
deep_purple 800 4527A0
deep_purple 900 311B92
deep_purple a100 B388FF
deep_purple a200 7C4DFF
deep_purple a400 651FFF
deep_purple a700 6200EA
indigo 50 E8EAF6
indigo 100 C5CAE9
indigo 200 9FA8DA
indigo 300 7986CB
indigo 400 5C6BC0
indigo 500 3F51B5
indigo 600 3949AB
indigo 700 303F9F
indigo 800 283593
indigo 900 1A237E
indigo a100 8C9EFF
indigo a200 536DFE
indigo a400 3D5AFE
indigo a700 304FFE
blue 50 E3F2FD
blue 100 BBDEFB
blue 200 90CAF9
blue 300 64B5F6
blue 400 42A5F5
blue 500 2196F3
blue 600 1E88E5
blue 700 1976D2
blue 800 1565C0
blue 900 0D47A1
blue a100 82B1FF
blue a200 448AFF
blue a400 2979FF
blue a700 2962FF
light_blue 50 E1F5FE
light_blue 100 B3E5FC
light_blue 200 81D4FA
light_blue 300 4FC3F7
light_blue 400 29B6F6
light_blue 500 03A9F4
light_blue 600 039BE5
light_blue 700 0288D1
light_blue 800 0277BD
light_blue 900 01579B
light_blue a100 80D8FF
light_blue a200 40C4FF
light_blue a400 00B0FF
light_blue a700 0091EA
cyan 50 E0F7FA
cyan 100 B2EBF2
cyan 200 80DEEA
cyan 300 4DD0E1
cyan 400 26C6DA
cyan 500 00BCD4
cyan 600 00ACC1
cyan 700 0097A7
cyan 800 00838F
cyan 900 006064
cyan a100 84FFFF
cyan a200 18FFFF
cyan a400 00E5FF
cyan a700 00B8D4
teal 50 E0F2F1
teal 100 B2DFDB
teal 200 80CBC4
teal 300 4DB6AC
teal 400 26A69A
teal 500 009688
teal 600 00897B
teal 700 00796B
teal 800 00695C
teal 900 004D40
teal a100 A7FFEB
teal a200 64FFDA
teal a400 1DE9B6
teal a700 00BFA5
green 50 E8F5E9
green 100 C8E6C9
green 200 A5D6A7
green 300 81C784
green 400 66BB6A
green 500 4CAF50
green 600 43A047
green 700 388E3C
green 800 2E7D32
green 900 1B5E20
green a100 B9F6CA
green a200 69F0AE
green a400 00E676
green a700 00C853
light_green 50 F1F8E9
light_green 100 DCEDC8
light_green 200 C5E1A5
light_green 300 AED581
light_green 400 9CCC65
light_green 500 8BC34A
light_green 600 7CB342
light_green 700 689F38
light_green 800 558B2F
light_green 900 33691E
light_green a100 CCFF90
light_green a200 B2FF59
light_green a400 76FF03
light_green a700 64DD17
lime 50 F9FBE7
lime 100 F0F4C3
lime 200 E6EE9C
lime 300 DCE775
lime 400 D4E157
lime 500 CDDC39
lime 600 C0CA33
lime 700 AFB42B
lime 800 9E9D24
lime 900 827717
lime a100 F4FF81
lime a200 EEFF41
lime a400 C6FF00
lime a700 AEEA00
yellow 50 FFFDE7
yellow 100 FFF9C4
yellow 200 FFF59D
yellow 300 FFF176
yellow 400 FFEE58
yellow 500 FFEB3B
yellow 600 FDD835
yellow 700 FBC02D
yellow 800 F9A825
yellow 900 F57F17
yellow a100 FFFF8D
yellow a200 FFFF00
yellow a400 FFEA00
yellow a700 FFD600
amber 50 FFF8E1
amber 100 FFECB3
amber 200 FFE082
amber 300 FFD54F
amber 400 FFCA28
amber 500 FFC107
amber 600 FFB300
amber 700 FFA000
amber 800 FF8F00
amber 900 FF6F00
amber a100 FFE57F
amber a200 FFD740
amber a400 FFC400
amber a700 FFAB00
orange 50 FFF3E0
orange 100 FFE0B2
orange 200 FFCC80
orange 300 FFB74D
orange 400 FFA726
orange 500 FF9800
orange 600 FB8C00
orange 700 F57C00
orange 800 EF6C00
orange 900 E65100
orange a100 FFD180
orange a200 FFAB40
orange a400 FF9100
orange a700 FF6D00
deep_orange 50 FBE9E7
deep_orange 100 FFCCBC
deep_orange 200 FFAB91
deep_orange 300 FF8A65
deep_orange 400 FF7043
deep_orange 500 FF5722
deep_orange 600 F4511E
deep_orange 700 E64A19
deep_orange 800 D84315
deep_orange 900 BF360C
deep_orange a100 FF9E80
deep_orange a200 FF6E40
deep_orange a400 FF3D00
deep_orange a700 DD2C00
brown 50 EFEBE9
brown 100 D7CCC8
brown 200 BCAAA4
brown 300 A1887F
brown 400 8D6E63
brown 500 795548
brown 600 6D4C41
brown 700 5D4037
brown 800 4E342E
brown 900 3E2723
grey 50 FAFAFA
grey 100 F5F5F5
grey 200 EEEEEE
grey 300 E0E0E0
grey 400 BDBDBD
grey 500 9E9E9E
grey 600 757575
grey 700 616161
grey 800 424242
grey 900 212121
blue_grey 50 ECEFF1
blue_grey 100 CFD8DC
blue_grey 200 B0BEC5
blue_grey 300 90A4AE
blue_grey 400 78909C
blue_grey 500 607D8B
blue_grey 600 546E7A
blue_grey 700 455A64
blue_grey 800 37474F
blue_grey 900 263238
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment