#!/usr/bin/env python3 import re, subprocess, simplestyle, os import inkex def calculateCMYK(red, green, blue): C = float() M = float() Y = float() K = float() if 1.00 - red < 1.00 - green: K = 1.00 - red else: K = 1.00 - green if 1.00 - blue < K: K = 1.00 - blue if K != 1.00: C = ( 1.00 - red - K ) / ( 1.00 - K ) M = ( 1.00 - green - K ) / ( 1.00 - K ) Y = ( 1.00 - blue - K ) / ( 1.00 - K ) return [C, M, Y, K] def clean_svg_color_definitions(svg): def change_colors(origin, color_type): for i in range(len(str(origin).split(color_type + ':'))): if str(str(origin).split(color_type + ':')[i].split(';')[0]) in simplestyle.svgcolors.keys(): color_numbers = simplestyle.formatColoria(simplestyle.parseColor(str(str(origin).split(color_type + ':')[i].split(';')[0]))) origin = str(origin).replace(':' + str(str(origin).split(color_type + ':')[i].split(';')[0]) + ';', ':' + color_numbers + ';') return origin colortypes = ['fill', 'stop-color', 'flood-color', 'lighting-color', 'stroke'] for i in range(len(colortypes)): svg = change_colors(svg, colortypes[i]) return svg def removeK(origin): def reset_opacity(value): return str(value.group()).split('opacity:')[0] + "opacity:0;" #return re.sub("#000000;fill-opacity:[0-9.]+;", reset_opacity, re.sub("#000000;stop-opacity:[0-9.]+;", reset_opacity, re.sub("#000000;stroke-opacity:[0-9.]+;", reset_opacity, re.sub("#000000;flood-opacity:[0-9.]+;", reset_opacity, re.sub("#000000;lighting-opacity:[0-9.]+;", reset_opacity, origin))))) return re.sub("#000000;fill-opacity:[0-9.?]+", reset_opacity, re.sub("#000000;stop-opacity:[0-9.?]+", reset_opacity, re.sub("#000000;stroke-opacity:[0-9.?]+", reset_opacity, re.sub("#000000;flood-opacity:[0-9.?]+", reset_opacity, re.sub("#000000;lighting-opacity:[0-9.?]+", reset_opacity, origin))))) def representC(value): # returns CMS color if available if (re.search("icc-color", value.group())): return simplestyle.formatColor3f(float(1.00 - float(re.split(r'[,\)\s]+',value.group())[2])), float(1.00), float(1.00)) else: red = float(simplestyle.parseColor(str(value.group()))[0]/255.00) green = float(simplestyle.parseColor(str(value.group()))[1]/255.00) blue = float(simplestyle.parseColor(str(value.group()))[2]/255.00) return simplestyle.formatColor3f(float(1.00 - calculateCMYK(red, green, blue)[0]), float(1.00), float(1.00)) def representM(value): # returns CMS color if available if ( re.search("icc-color", value.group()) ): return simplestyle.formatColor3f(float(1.00), float(1.00 - float(re.split(r'[,\)\s]+',value.group())[3])), float(1.00)) else: red = float(simplestyle.parseColor(str(value.group()))[0]/255.00) green = float(simplestyle.parseColor(str(value.group()))[1]/255.00) blue = float(simplestyle.parseColor(str(value.group()))[2]/255.00) return simplestyle.formatColor3f(float(1.00), float(1.00 - calculateCMYK(red, green, blue)[1]), float(1.00)) def representY(value): # returns CMS color if available if (re.search("icc-color", value.group()) ): return simplestyle.formatColor3f(float(1.00), float(1.00), float(1.00 - float(re.split(r'[,\)\s]+',value.group())[4]))) else: red = float(simplestyle.parseColor(str(value.group()))[0]/255.00) green = float(simplestyle.parseColor(str(value.group()))[1]/255.00) blue = float(simplestyle.parseColor(str(value.group()))[2]/255.00) return simplestyle.formatColor3f(float(1.00), float(1.00), float(1.00 - calculateCMYK(red, green, blue)[2])) def representK(value): # returns CMS color if available if (re.search("icc-color", value.group()) ): return simplestyle.formatColor3f(float(1.00 - float(re.split(r'[,\)\s]+',value.group())[5])), float(1.00 - float(re.split(r'[,\)\s]+',value.group())[5])), float(1.00 - float(re.split(r'[,\)\s]+',value.group())[5]))) else: red = float(simplestyle.parseColor(str(value.group()))[0]/255.00) green = float(simplestyle.parseColor(str(value.group()))[1]/255.00) blue = float(simplestyle.parseColor(str(value.group()))[2]/255.00) return simplestyle.formatColor3f(float(1.00 - calculateCMYK(red, green, blue)[3]), float(1.00 - calculateCMYK(red, green, blue)[3]), float(1.00 - calculateCMYK(red, green, blue)[3])) def generate_svg_separations(temp_dir, original_source, overblack): svg_ready = clean_svg_color_definitions(original_source) with open(os.path.join(temp_dir, "separationK.svg"), "w") as f: f.write(re.sub(r"#[a-fA-F0-9]{6}( icc-color\(.*?\))?", representK, svg_ready)) if overblack: svg_ready = removeK(svg_ready) with open(os.path.join(temp_dir, "separationC.svg"), "w") as f: f.write(re.sub(r"#[a-fA-F0-9]{6}( icc-color\(.*?\))?", representC, svg_ready)) with open(os.path.join(temp_dir, "separationM.svg"), "w") as f: f.write(re.sub(r"#[a-fA-F0-9]{6}( icc-color\(.*?\))?", representM, svg_ready)) with open(os.path.join(temp_dir, "separationY.svg"), "w") as f: f.write(re.sub(r"#[a-fA-F0-9]{6}( icc-color\(.*?\))?", representY, svg_ready)) def generate_png_separations(temp_dir, area_to_export, resolution, alpha): if alpha: alpha_command = "" else: alpha_command = " --export-background=white " string_inkscape_exec = '' for color in ['C', 'M', 'Y', 'K']: string_inkscape_exec += temp_dir + "separation" + color + ".svg " + area_to_export + ' --export-png=' + temp_dir + "separated" + area_to_export.replace(' ', '') + color + ".png" + alpha_command + ' --export-dpi=' + str(resolution) + "\n" with open(os.path.join(temp_dir, 'test.txt'), 'w') as f: f.write(string_inkscape_exec) inkscape_exec = subprocess.Popen(['inkscape -z --shell'], shell=True, stdout=open(os.devnull, 'w'), stderr=open(os.devnull, 'w'), stdin=subprocess.PIPE) inkscape_exec.communicate(input=string_inkscape_exec.encode('UTF-8'))