Gradience/gradience/utils/css.py

68 lines
2 KiB
Python
Raw Normal View History

2022-10-10 19:52:45 +00:00
# css.py
#
# Change the look of Adwaita, with ease
# Copyright (C) 2022 Gradience Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import logging
2022-09-13 17:00:16 +00:00
import cssutils
# Adwaita palette color name dict
2022-09-13 17:00:16 +00:00
COLORS = [
"blue_",
"green_",
"yellow_",
"orange_",
"red_",
"purple_",
"brown_",
"light_",
"dark_",
]
# Override cssutils preferences
cssutils.ser.prefs.minimizeColorHash = False
cssutils.ser.prefs.indentClosingBrace = False
cssutils.ser.prefs.omitLastSemicolon = False
# Set cssutils module logging to level FATAL
cssutils.log.setLevel(logging.FATAL)
2022-09-13 17:00:16 +00:00
2022-09-13 19:14:09 +00:00
def load_preset_from_css(path):
2022-09-13 17:00:16 +00:00
css = ""
variables = {}
palette = {}
for color in COLORS:
palette[color] = {}
with open(path, "r", encoding="utf-8") as f:
sheet = cssutils.parseString(f.read())
for rule in sheet:
css_text = rule.cssText
if rule.type == rule.UNKNOWN_RULE:
if css_text.startswith("@define-color"):
name, color = css_text.split(" ", 1)[1].split(" ", 1)
for color_name in COLORS:
if name.startswith(color_name):
2022-09-13 19:14:09 +00:00
palette[name[:-1]][name[-1:]] = color[:-1]
2022-09-13 17:00:16 +00:00
break
else:
2022-09-13 19:14:09 +00:00
variables[name] = color[:-1]
2022-09-13 17:00:16 +00:00
elif rule.type == rule.STYLE_RULE:
css += f"\n{rule.cssText}\n"
2022-09-13 19:14:09 +00:00
return variables, palette, css