From 07eff9325a2d3f8afd1d97ebb162b206441cddc0 Mon Sep 17 00:00:00 2001 From: LostQuasar Date: Fri, 25 Nov 2022 02:59:07 +0100 Subject: [PATCH] Add color darkening --- src/main.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main.py b/src/main.py index 9471b5b..5eb863f 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,6 @@ from svgutils.compose import * from cairosvg import svg2png -import toml, os +import toml, os, re asset_path = "../assets/" CONFIG["svg.file_path"] = asset_path @@ -17,10 +17,18 @@ with open("../assets/colors/base.toml", "r") as color_file: #Load the user confi os.makedirs("../output/vector/", exist_ok=True) os.makedirs("../output/" + str(config["raster_size"]) + "/", exist_ok=True) +def DarkenColor(inputColor, percentage): + inputColor = inputColor.replace('0x', '').replace('#', '').upper() + valLen = int(len(inputColor) / 3) + outputColor = "" + for value in list(map(''.join, zip( * [iter(inputColor)] * valLen))): + outputColor += str(hex(int(int(value, 16) * ((100 - int(percentage)) / 100))))[2: ].zfill(valLen).upper() + return outputColor + for key in designs: currentDesign = designs[key] #Select the current design which is a list of parts partsList = [] - colors_out = {} + colorsOut = {} for part in currentDesign: for url in definitions[part]: partsList.append(url) #For each part add each element to the list @@ -30,7 +38,7 @@ for key in designs: if "color" in part: with open(asset_path + part) as transform_file: swaps = toml.loads(transform_file.read()) - colors_out.update(swaps) + colorsOut.update(swaps) else: with open(asset_path + part) as transform_file: transforms = toml.loads(transform_file.read()) @@ -39,15 +47,21 @@ for key in designs: svg_parts.append(SVG(part)) #Add each element of the list of paths to a new list as a SVG element Figure("128","128", *svg_parts, SVG("credits.svg")).save(f"../output/vector/{key}.svg") #Compile all parts into an SVG - with open(f"../output/vector/{key}.svg", "r") as in_file: - in_lines = in_file.read() - for color_key in colors_out: - if base_colors[color_key].lower() in in_lines.lower(): - pattern = re.compile(base_colors[color_key], re.IGNORECASE) - in_lines = pattern.sub(colors_out[color_key].lower(), in_lines) + with open(f"../output/vector/{key}.svg", "r") as inFile: + inLines = inFile.read() + for colorKey in colorsOut: + if base_colors[colorKey].lower() in inLines.lower(): + pattern = re.compile(base_colors[colorKey], re.IGNORECASE) + inLines = pattern.sub(colorsOut[colorKey].lower(), inLines) + if "DARKEN" in inLines: + darkenMatches = re.findall("(#DARKEN\( ?((?:(?:[a-f]|[A-F]|[0-9]){3}){1,2}) ?\, ?(\d{1,2}) ?\))", inLines) + if darkenMatches: + for match in darkenMatches: + newColor = DarkenColor(match[1], match[2]) + inLines = inLines.replace(match[0], "#"+newColor.lower()) with open(f"../output/vector/{key}.svg", "w") as out_file: - out_file.write(in_lines) + out_file.write(inLines) if config["raster_images"]: #Create png of designated size svg2png(