def run(self, edit): """ Run the command. Arguments: - edit - an edit object. """ settings = Settings( sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)) formats = [ value for value in sorted(settings.regex_compiler.formats.keys()) ] color_converter = ColorConverter(formats) for (region, color, format_name) in _get_colors(self.view, settings, formats, color_converter): index = formats.index(format_name) - 1 if index == -1: index = len(formats) - 1 new_format = formats[index] new_color = color_converter.from_color((color, new_format)) if settings.debug: print(( "ColorHighlighter: action=run_command name=color_highlighter_previous_color region=%s " + "format=%s color=%s new_format=%s new_color=%s") % (str(region.region()), format_name, color, new_format, new_color)) self.view.replace(edit, region.region(), new_color)
def _open_color_picker(self): _init_color_picker() settings = Settings( sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)) formats = [ value for value in sorted(settings.regex_compiler.formats.keys()) ] color_converter = ColorConverter(formats) colors = [ value for value in _get_colors(self.view, settings, formats, color_converter) ] replace_colors = len(colors) > 0 if replace_colors: initial_color = colors[0][1][1:] else: initial_color = "FFFFFFFF" popen = subprocess.Popen( [path.color_picker_file(path.ABSOLUTE), initial_color], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) output, error = popen.communicate() output = output.decode("utf-8") error = error.decode("utf-8") if error is not None and error: print("Color Picker error:\n" + error) if output == "CANCEL": if settings.debug: print( "ColorHighlighter: action=run_command name=color_highlighter_pick_color result=canceled" ) return replace_data = [] if replace_colors: for (region, _, format_name) in colors: new_color = color_converter.from_color((output, format_name)) if settings.debug: print(( "ColorHighlighter: action=run_command name=color_highlighter_pick_color result=replace " + "region=%s format=%s color=%s") % (region.region(), format_name, new_color)) replace_data.append((region.region(), new_color)) else: for region in self.view.sel(): if settings.debug: print(( "ColorHighlighter: action=run_command name=color_highlighter_pick_color result=insert " + "region=%s color=%s") % (region, output)) replace_data.append((region, output)) self.view.run_command("color_highlighter_impl_replace_color", {"replace_data": str(replace_data)})
def _any_colors_selected(view): settings = Settings(sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)) formats = [ value for value in sorted(settings.regex_compiler.formats.keys()) ] color_converter = ColorConverter(formats) color_searcher = ColorSearcher(compile_regex(settings.regex_compiler), color_converter) for _ in search_colors_in_selection(view, color_searcher): return True return False
def run(self, edit): """ Run the command. Arguments: - edit - an edit object. """ settings = Settings(sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)) formats = [value for value in sorted(settings.regex_compiler.formats.keys())] color_converter = ColorConverter(formats) for (region, color, format_name) in _get_colors(self.view, settings, formats, color_converter): index = formats.index(format_name) - 1 if index == -1: index = len(formats) - 1 new_format = formats[index] new_color = color_converter.from_color((color, new_format)) if settings.debug: print(("ColorHighlighter: action=run_command name=color_highlighter_previous_color region=%s " + "format=%s color=%s new_format=%s new_color=%s") % (str(region.region()), format_name, color, new_format, new_color)) self.view.replace(edit, region.region(), new_color)
def _open_color_picker(self): _init_color_picker() settings = Settings(sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)) formats = [value for value in sorted(settings.regex_compiler.formats.keys())] color_converter = ColorConverter(formats) colors = [value for value in _get_colors(self.view, settings, formats, color_converter)] replace_colors = len(colors) > 0 if replace_colors: initial_color = colors[0][1][1:] else: initial_color = "FFFFFFFF" popen = subprocess.Popen( [path.color_picker_file(path.ABSOLUTE), initial_color], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) output, error = popen.communicate() output = output.decode("utf-8") error = error.decode("utf-8") if error is not None and error: print("Color Picker error:\n" + error) if output == "CANCEL": if settings.debug: print("ColorHighlighter: action=run_command name=color_highlighter_pick_color result=canceled") return replace_data = [] if replace_colors: for (region, _, format_name) in colors: new_color = color_converter.from_color((output, format_name)) if settings.debug: print(("ColorHighlighter: action=run_command name=color_highlighter_pick_color result=replace " + "region=%s format=%s color=%s") % (region.region(), format_name, new_color)) replace_data.append((region.region(), new_color)) else: for region in self.view.sel(): if settings.debug: print(("ColorHighlighter: action=run_command name=color_highlighter_pick_color result=insert " + "region=%s color=%s") % (region, output)) replace_data.append((region, output)) self.view.run_command("color_highlighter_impl_replace_color", {"replace_data": str(replace_data)})
def provide_color_converter(self): """Provide a color converter.""" return ColorConverter(self.provide_formats())
def create_color_converter(self): return ColorConverter(self.img)