def _generate_terminal_palette_callback( # noqa pylint: disable=too-many-locals cls, hex_palette, template_path, inverse_palette, result_callback, ): gray_colors = get_gray_colors(hex_palette) bright_colors = set(hex_palette) bright_colors.difference_update(gray_colors) bright_colors = list(bright_colors) ACCURACY = 40 # pylint: disable=invalid-name hex_palette += [hex_darker(c, ACCURACY) for c in gray_colors] hex_palette += [hex_darker(c, -ACCURACY) for c in gray_colors] reference_palette = import_xcolors( os.path.join(TERMINAL_TEMPLATE_DIR, template_path)) result_palette = {} if inverse_palette: reference_palette['foreground'], reference_palette['background'] = \ reference_palette['background'], reference_palette['foreground'] is_dark_bg = is_dark(reference_palette['background']) max_possible_lightness = 255 * 3 new_bg_color, _diff = find_closest_color( reference_palette['background'], hex_palette) # @TODO: use real lightness from HSV or Lab color model lightness_delta = sum(int_list_from_hex(new_bg_color)) * (1 if is_dark_bg else -1) + \ max_possible_lightness // 4 # max_possible_lightness // 6 min_lightness = max_possible_lightness // 38 max_lightness = max_possible_lightness - min_lightness if is_dark_bg: min_lightness = lightness_delta else: max_lightness = max_possible_lightness - lightness_delta for key, value in reference_palette.items(): if key not in [ 'color0', 'color7', 'color8', 'color15', 'foreground', 'background' ]: closest_color, _diff = find_closest_color( value, bright_colors, min_lightness=min_lightness, max_lightness=max_lightness) else: closest_color, _diff = find_closest_color(value, hex_palette) result_palette[key] = closest_color gc.collect() result_callback(result_palette)
def _generate_terminal_palette(cls, template_path, image_path, quality, use_whole_palette): hex_palette = cls.get_image_palette(image_path, quality, use_whole_palette)[:] gray_colors = get_gray_colors(hex_palette) ACCURACY = 40 # pylint: disable=invalid-name hex_palette += [hex_darker(c, ACCURACY) for c in gray_colors] hex_palette += [hex_darker(c, -ACCURACY) for c in gray_colors] reference_palette = import_xcolors( os.path.join(TERMINAL_TEMPLATE_DIR, template_path)) result_palette = {} for key, value in reference_palette.items(): closest_color, _diff = find_closest_color(value, hex_palette) result_palette[key] = closest_color gc.collect() return result_palette
def _generate_terminal_palette( # pylint: disable=too-many-arguments,too-many-locals,too-many-branches cls, template_path, image_path, quality, use_whole_palette, inverse_palette ): start_time = time() if str(quality).startswith('colorz'): hex_palette = cls._get_colorz_lib_palette( image_path, color_count=int(quality.split('colorz')[1]) ) elif str(quality).startswith('colorthief'): hex_palette = cls._get_colorthief_palette( image_path, color_count=int(quality.split('colorthief')[1]) + 1 ) elif quality == 'haishoku': hex_palette = cls._get_haishoku_palette(image_path) elif str(quality).startswith('all_'): _quality = quality.split('_')[1] if _quality == 'low': quality_per_plugin = [100, 16, 16] elif _quality == 'medium': quality_per_plugin = [200, 32, 32] else: raise NotImplementedError() hex_palette = cls._get_all_available_palettes( image_path=image_path, use_whole_palette=use_whole_palette, quality_per_plugin=quality_per_plugin ) else: hex_palette = cls.get_image_palette(image_path, int(quality), use_whole_palette)[:] print("{} quality, {} colors found, took {:.8f}s".format( quality, len(hex_palette), (time() - start_time) )) gray_colors = get_gray_colors(hex_palette) bright_colors = set(hex_palette) bright_colors.difference_update(gray_colors) bright_colors = list(bright_colors) ACCURACY = 40 # pylint: disable=invalid-name hex_palette += [hex_darker(c, ACCURACY) for c in gray_colors] hex_palette += [hex_darker(c, -ACCURACY) for c in gray_colors] reference_palette = import_xcolors(os.path.join(TERMINAL_TEMPLATE_DIR, template_path)) result_palette = {} if inverse_palette: reference_palette['foreground'], reference_palette['background'] = \ reference_palette['background'], reference_palette['foreground'] is_dark_bg = is_dark(reference_palette['background']) max_possible_lightness = 255 * 3 new_bg_color, _diff = find_closest_color(reference_palette['background'], hex_palette) # @TODO: use real lightness from HSV or Lab color model lightness_delta = sum(int_list_from_hex(new_bg_color)) * (1 if is_dark_bg else -1) + \ max_possible_lightness // 4 # max_possible_lightness // 6 min_lightness = max_possible_lightness // 38 max_lightness = max_possible_lightness - min_lightness if is_dark_bg: min_lightness = lightness_delta else: max_lightness = max_possible_lightness - lightness_delta for key, value in reference_palette.items(): if key not in ['color0', 'color7', 'color8', 'color15', 'foreground', 'background']: closest_color, _diff = find_closest_color( value, bright_colors, min_lightness=min_lightness, max_lightness=max_lightness ) else: closest_color, _diff = find_closest_color(value, hex_palette) result_palette[key] = closest_color gc.collect() return result_palette