99
1010import subprocess
1111import os
12+ import re
1213
1314from ..magick_command import MagickCommand
1415from ..res .res import res_path
176177 }
177178}
178179
180+ palettes_for_recolor = [
181+ "black" , "lavender_purple" , "violet_purple" ,"blue" , "teal" ,"yellow_green" ,
182+ "sea_green" ,"light_olive_green" ,"dark_olive_green" ,"lime_green" ,"yellow" ,
183+ "bright_yellow" ,"orange" ,"salmon" ,"sandy_brown" ,"bordeaux_red" ,"bright_red" ,
184+ "magenta" ,"recolor_1" ,"recolor_1_orct2"
185+ ]
186+
187+ def create_remap_tuple (paletteName ):
188+ if not paletteName in palette_colors_details :
189+ return
190+ palette = palette_colors_details [paletteName ]
191+ return tuple ([paletteName , palette ["title" ], palette ["Description" ]])
192+
193+ def create_remap_enumlist (defaultSelection ):
194+ myPaletteColors = palettes_for_recolor .copy ()
195+ myPaletteColors .remove (defaultSelection )
196+ options = [create_remap_tuple (i ) for i in myPaletteColors ]
197+ options .insert (0 ,create_remap_tuple (defaultSelection ))
198+ return options
199+
179200palette_base_path = os .path .join (res_path , "palettes" )
180201palette_groups_path = os .path .join (palette_base_path , "groups" )
181202
203+ class RGBA :
204+ def __init__ (self , hexString , red , green , blue , alpha = 255 ):
205+ self .hex = hexString
206+ self .red = int (red )
207+ self .green = int (green )
208+ self .blue = int (blue )
209+ self .alpha = int (alpha )
210+
182211# Collection of color groups to create a palette from
183212
184213
@@ -188,6 +217,7 @@ def __init__(self, path=None, colors=[]):
188217 self .generated = False
189218 self .invalidated = False
190219 self .path = ""
220+ self .shades = []
191221
192222 if path != None :
193223 self .path = path
@@ -242,3 +272,19 @@ def generate_output(self, renderer, output_path):
242272 self .path = output_path
243273 self .generated = True
244274 self .invalidated = False
275+
276+ # generates a list of hexadecimal colors usable in ImageMagick
277+ def get_shades (self , renderer ):
278+ cmd = MagickCommand ("" )
279+ cmd .pixel_data (self .path )
280+ raw_output = subprocess .check_output (cmd .get_command_string (
281+ renderer .magick_path ), shell = True )
282+ output = raw_output .decode ("utf-8" )
283+ data_rgb = re .findall (" \(([\d,]+)\)" , output )
284+ data_hex = re .findall ("#[\w]+" , output )
285+ if (len (data_rgb ) != len (data_hex )):
286+ print (len (data_rgb ), data_rgb )
287+ print (len (data_hex ), data_hex )
288+ assert (len (data_rgb ) != len (data_hex ))
289+ colors_present = [RGBA (data_hex [i ], * (data_rgb [i ].split ("," ))) for i in range (len (data_rgb ))]
290+ self .shades = [shade .hex for shade in colors_present if shade .alpha == 255 ]
0 commit comments