Skip to content

Instantly share code, notes, and snippets.

@rBrenick
Last active July 13, 2020 12:40
Show Gist options
  • Save rBrenick/63a4fe4cb372b538f51ab763a8f8288b to your computer and use it in GitHub Desktop.
Save rBrenick/63a4fe4cb372b538f51ab763a8f8288b to your computer and use it in GitHub Desktop.
__author__ = "rBrenick - [email protected]"
__created__ = "2020-07-13"
import os
import re
from Qt import QtGui
def hue_shift_stylesheet(base_sheet_path, new_sheet_path=None, hue_shift=160):
"""
Read stylesheet file, hue-shift all hex colors, write new file with modified values.
:param base_sheet_path:
:param new_sheet_path:
:param hue_shift:
:return:
"""
if new_sheet_path is None:
new_sheet_path = base_sheet_path.replace(".stylesheet", "_hue_shifted_{}.stylesheet".format(hue_shift))
# color_re = re.compile("#(.*);") # I tried, but regex is difficult
color_re = re.compile("(\w*)")
new_stylesheet_lines = []
with open(base_sheet_path, "r") as fp:
for i, line in enumerate(fp.readlines()):
if "#" not in line:
new_stylesheet_lines.append(line)
continue
new_hue_hexes = {}
for color_str in line.split("#")[1:]:
color_re_search = color_re.search(color_str)
base_color_hex = color_re_search.group(1)
if not base_color_hex:
continue
col = QtGui.QColor()
col.setNamedColor("#" + base_color_hex)
new_hue = col.hue() + hue_shift
col.setHsv(new_hue, col.saturation(), col.value(), col.alpha())
new_hue_hex = col.name(QtGui.QColor.NameFormat.HexRgb)
new_hue_hexes[base_color_hex] = new_hue_hex[1:]
for base_color_hex, new_hue_hex in new_hue_hexes.items():
line = line.replace(base_color_hex, new_hue_hex)
new_stylesheet_lines.append(line)
with open(new_sheet_path, "w") as fp:
fp.writelines(new_stylesheet_lines)
return new_sheet_path
if __name__ == '__main__':
base_stylesheet_path = os.path.join(os.path.dirname(__file__), "stylesheets", "darkorange.stylesheet")
output_stylesheet_path = os.path.join(os.path.dirname(__file__), "stylesheets", "darkblue.stylesheet")
hue_shift_stylesheet(base_stylesheet_path, output_stylesheet_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment