excolor.utils
- excolor.utils.get_color_name(color)[source]
Finds the closest color name from the rich color dataset for a given color.
This function converts the input color to RGB, then uses a vectorized numpy operation to efficiently find the color with the minimum Euclidean distance in the RGB space from a pre-compiled list of over 9,000 colors.
- Parameters:
color (Any) – The input color in any format supported by to_rgb255.
- Returns:
The closest matching color name.
- Return type:
- excolor.utils.get_colors(cmap, n=None, exclude_extreme=True)[source]
Extracts colors from a colormap with optional sampling and filtering.
This function extracts colors from a colormap, with options to: - Specify the number of colors to extract - Exclude extreme (very dark/light) colors - Handle both qualitative and continuous colormaps appropriately
- Parameters:
cmap (str or matplotlib.colors.Colormap) – Colormap name or instance
n (int, optional) – Sumpling number of colors to extract. If None: - For qualitative colormaps: uses all colors - For continuous colormaps: uses 10 colors (9 for divergent)
exclude_extreme (bool, default=True) – Filtering out the darkest and lightest colors from the output. This is useful for continuous colormaps to avoid pure black/white.
- Returns:
List of colors in hex format, sampled from the colormap
- Return type:
Examples
>>> get_colors('viridis', n=5) # Get 5 colors from viridis >>> get_colors('Set1') # Get all colors from qualitative colormap >>> get_colors('RdBu', exclude_extreme=False) # Include extremes
- excolor.utils.interpolate_colors(c, n=5)[source]
Creates a smooth gradient of colors by interpolating between input colors.
This function takes a list of colors and generates a new list with n colors by creating a smooth gradient between the input colors using linear interpolation.
- Parameters:
- Returns:
List of n colors in hex format, forming a smooth gradient between the input colors
- Return type:
Examples
>>> interpolate_colors(['#FF0000', '#00FF00'], n=3) ['#FF0000', '#808000', '#00FF00'] >>> interpolate_colors(['red', 'blue'], n=4) ['#FF0000', '#800080', '#0000FF']