Colors¶
- This script demonstrates the usage of the Colors class from the Cleopatra package.
- The Colors class provides functionality for working with colors in different formats.
In [71]:
Copied!
import matplotlib.pyplot as plt
from cleopatra.colors import Colors
import matplotlib.pyplot as plt
from cleopatra.colors import Colors
1. Initializing Colors with Different Formats¶
The Colors class can be initialized with different color formats:
- Hex color strings (e.g., "#FF5733")
- Named colors (e.g., "red", "blue")
- RGB tuples (e.g., (255, 0, 0) or (1.0, 0.0, 0.0))
- Lists of any of the above
1.1 Initializing with Hex Colors¶
In [72]:
Copied!
hex_color = Colors("#FF5733")
print(f"Hex color: {hex_color.color_value}") # pragma: allowlist secret
hex_color = Colors("#FF5733")
print(f"Hex color: {hex_color.color_value}") # pragma: allowlist secret
Hex color: ['#FF5733']
1.2 Initializing with Named Colors¶
In [73]:
Copied!
named_color = Colors("blue")
print(f"Named color: {named_color.color_value}")
named_color = Colors("blue")
print(f"Named color: {named_color.color_value}")
Named color: ['blue']
1.3 Initializing with RGB Tuples (0-255)¶
In [74]:
Copied!
rgb_color_255 = Colors((255, 0, 0))
print(f"RGB color (0-255): {rgb_color_255.color_value}")
rgb_color_255 = Colors((255, 0, 0))
print(f"RGB color (0-255): {rgb_color_255.color_value}")
RGB color (0-255): [(255, 0, 0)]
1.4 Initializing with RGB Tuples (0-1)¶
In [75]:
Copied!
rgb_color_norm = Colors((1.0, 0.0, 0.0))
print(f"RGB color (0-1): {rgb_color_norm.color_value}")
rgb_color_norm = Colors((1.0, 0.0, 0.0))
print(f"RGB color (0-1): {rgb_color_norm.color_value}")
RGB color (0-1): [(1.0, 0.0, 0.0)]
1.5 Initializing with a List of Colors¶
In [76]:
Copied!
color_list = Colors(["red", "green", "blue"])
print(f"Color list: {color_list.color_value}")
color_list = Colors(["red", "green", "blue"])
print(f"Color list: {color_list.color_value}")
Color list: ['red', 'green', 'blue']
2. Converting Colors to Hex Format¶
- The to_hex() method converts colors to hexadecimal format.
2.1 Converting a Named Color to Hex¶
In [77]:
Copied!
named_to_hex = Colors("purple")
hex_value = named_to_hex.to_hex()
print(f"Purple in hex: {hex_value}")
named_to_hex = Colors("purple")
hex_value = named_to_hex.to_hex()
print(f"Purple in hex: {hex_value}")
Purple in hex: ['purple']
2.2 Converting RGB to Hex¶
In [78]:
Copied!
rgb_to_hex = Colors((0, 128, 0)) # Green in RGB
hex_value = rgb_to_hex.to_hex()
print(f"Green RGB in hex: {hex_value}")
rgb_to_hex = Colors((0, 128, 0)) # Green in RGB
hex_value = rgb_to_hex.to_hex()
print(f"Green RGB in hex: {hex_value}")
Green RGB in hex: ['#008000']
2.3 Converting a List of Colors to Hex¶
In [79]:
Copied!
colors_to_hex = Colors(["red", (0, 0, 255), "#00FF00"])
hex_values = colors_to_hex.to_hex()
print(f"List of colors in hex: {hex_values}")
colors_to_hex = Colors(["red", (0, 0, 255), "#00FF00"])
hex_values = colors_to_hex.to_hex()
print(f"List of colors in hex: {hex_values}")
List of colors in hex: ['red', '#0000ff', '#00FF00']
3. Converting Colors to RGB Format¶
- The to_rgb() method converts colors to RGB format.
3.1 Converting a Named Color to RGB (normalized)¶
In [80]:
Copied!
named_to_rgb = Colors("orange")
rgb_value = named_to_rgb.to_rgb(normalized=True)
print(f"Orange in normalized RGB: {rgb_value}")
named_to_rgb = Colors("orange")
rgb_value = named_to_rgb.to_rgb(normalized=True)
print(f"Orange in normalized RGB: {rgb_value}")
Orange in normalized RGB: [(1.0, 0.6470588235294118, 0.0)]
3.2 Converting a Named Color to RGB (0-255)¶
In [81]:
Copied!
named_to_rgb_255 = Colors("orange")
rgb_value_255 = named_to_rgb_255.to_rgb(normalized=False)
print(f"Orange in RGB (0-255): {rgb_value_255}")
named_to_rgb_255 = Colors("orange")
rgb_value_255 = named_to_rgb_255.to_rgb(normalized=False)
print(f"Orange in RGB (0-255): {rgb_value_255}")
Orange in RGB (0-255): [(255, 165, 0)]
3.3 Converting Hex to RGB¶
In [82]:
Copied!
hex_to_rgb = Colors("#4287f5") # Blue-ish color
rgb_value = hex_to_rgb.to_rgb(normalized=True)
print(f"#4287f5 in normalized RGB: {rgb_value}")
hex_to_rgb = Colors("#4287f5") # Blue-ish color
rgb_value = hex_to_rgb.to_rgb(normalized=True)
print(f"#4287f5 in normalized RGB: {rgb_value}")
#4287f5 in normalized RGB: [(0.25882352941176473, 0.5294117647058824, 0.9607843137254902)]
3.4 Converting a List of Colors to RGB¶
In [83]:
Copied!
colors_to_rgb = Colors(["red", "#00FF00", (0, 0, 255)])
rgb_values = colors_to_rgb.to_rgb(normalized=True)
print(f"List of colors in normalized RGB: {rgb_values}")
colors_to_rgb = Colors(["red", "#00FF00", (0, 0, 255)])
rgb_values = colors_to_rgb.to_rgb(normalized=True)
print(f"List of colors in normalized RGB: {rgb_values}")
List of colors in normalized RGB: [(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)]
4. Checking Color Validity¶
- The Colors class provides methods to check if colors are valid.
4.1 Checking if a Hex Color is Valid¶
In [84]:
Copied!
valid_hex = Colors("#FF5733").is_valid_hex()
invalid_hex = Colors("#XYZ").is_valid_hex()
print(f"Is #FF5733 a valid hex color? {valid_hex}")
print(f"Is #XYZ a valid hex color? {invalid_hex}")
valid_hex = Colors("#FF5733").is_valid_hex()
invalid_hex = Colors("#XYZ").is_valid_hex()
print(f"Is #FF5733 a valid hex color? {valid_hex}")
print(f"Is #XYZ a valid hex color? {invalid_hex}")
Is #FF5733 a valid hex color? [True] Is #XYZ a valid hex color? [False]
4.2 Checking if an RGB Color is Valid (0-255)¶
In [85]:
Copied!
valid_rgb_255 = Colors((255, 0, 0)).is_valid_rgb()
invalid_rgb_255 = Colors((300, 0, 0)).is_valid_rgb() # 300 is out of range
print(f"Is (255, 0, 0) a valid RGB color? {valid_rgb_255}")
print(f"Is (300, 0, 0) a valid RGB color? {invalid_rgb_255}")
valid_rgb_255 = Colors((255, 0, 0)).is_valid_rgb()
invalid_rgb_255 = Colors((300, 0, 0)).is_valid_rgb() # 300 is out of range
print(f"Is (255, 0, 0) a valid RGB color? {valid_rgb_255}")
print(f"Is (300, 0, 0) a valid RGB color? {invalid_rgb_255}")
Is (255, 0, 0) a valid RGB color? [True] Is (300, 0, 0) a valid RGB color? [False]
4.3 Checking if an RGB Color is Valid (0-1)¶
In [86]:
Copied!
valid_rgb_norm = Colors((0.5, 0.5, 0.5)).is_valid_rgb()
invalid_rgb_norm = Colors((1.5, 0.5, 0.5)).is_valid_rgb() # 1.5 is out of range
print(f"Is (0.5, 0.5, 0.5) a valid normalized RGB color? {valid_rgb_norm}")
print(f"Is (1.5, 0.5, 0.5) a valid normalized RGB color? {invalid_rgb_norm}")
valid_rgb_norm = Colors((0.5, 0.5, 0.5)).is_valid_rgb()
invalid_rgb_norm = Colors((1.5, 0.5, 0.5)).is_valid_rgb() # 1.5 is out of range
print(f"Is (0.5, 0.5, 0.5) a valid normalized RGB color? {valid_rgb_norm}")
print(f"Is (1.5, 0.5, 0.5) a valid normalized RGB color? {invalid_rgb_norm}")
Is (0.5, 0.5, 0.5) a valid normalized RGB color? [True] Is (1.5, 0.5, 0.5) a valid normalized RGB color? [False]
5. Getting the Type of Color¶
- The get_type() method returns the type of color.
5.1 Getting the Type of a Hex Color¶
In [87]:
Copied!
hex_color_type = Colors("#FF5733")
print(f"Type of #FF5733: {hex_color_type.get_type()}")
hex_color_type = Colors("#FF5733")
print(f"Type of #FF5733: {hex_color_type.get_type()}")
Type of #FF5733: ['hex']
5.2 Getting the Type of a Named Color¶
In [88]:
Copied!
named_color_type = Colors("blue")
print(f"Type of 'blue': {named_color_type.get_type()}")
named_color_type = Colors("blue")
print(f"Type of 'blue': {named_color_type.get_type()}")
Type of 'blue': ['hex']
5.3 Getting the Type of an RGB Color¶
In [89]:
Copied!
rgb_color_type = Colors((255, 0, 0))
print(f"Type of (255, 0, 0): {rgb_color_type.get_type()}")
rgb_color_type = Colors((255, 0, 0))
print(f"Type of (255, 0, 0): {rgb_color_type.get_type()}")
Type of (255, 0, 0): ['rgb']
5.4 Getting the Type of a List of Colors¶
In [90]:
Copied!
color_list_type = Colors(["red", "green", "blue"])
print(f"Type of ['red', 'green', 'blue']: {color_list_type.get_type()}")
color_list_type = Colors(["red", "green", "blue"])
print(f"Type of ['red', 'green', 'blue']: {color_list_type.get_type()}")
Type of ['red', 'green', 'blue']: ['hex', 'hex', 'hex']
6. Visualizing Colors¶
- Let's visualize some colors using matplotlib.
6.1 Create a list of colors¶
In [91]:
Copied!
color_names = ["red", "green", "blue", "yellow", "purple", "orange", "cyan", "magenta"]
colors_obj = Colors(color_names)
hex_colors = colors_obj.to_hex()
color_names = ["red", "green", "blue", "yellow", "purple", "orange", "cyan", "magenta"]
colors_obj = Colors(color_names)
hex_colors = colors_obj.to_hex()
6.2 Create a bar chart with these colors¶
In [92]:
Copied!
plt.figure(figsize=(10, 6))
bars = plt.bar(range(len(color_names)), [1] * len(color_names), color=hex_colors)
# Add color names as labels
for i, bar in enumerate(bars):
plt.text(bar.get_x() + bar.get_width()/2, 0.5, color_names[i],
ha='center', va='center', color='white' if color_names[i] in ['blue', 'purple'] else 'black')
plt.title("Visualization of Different Colors")
plt.xticks([])
plt.yticks([])
plt.show()
plt.figure(figsize=(10, 6))
bars = plt.bar(range(len(color_names)), [1] * len(color_names), color=hex_colors)
# Add color names as labels
for i, bar in enumerate(bars):
plt.text(bar.get_x() + bar.get_width()/2, 0.5, color_names[i],
ha='center', va='center', color='white' if color_names[i] in ['blue', 'purple'] else 'black')
plt.title("Visualization of Different Colors")
plt.xticks([])
plt.yticks([])
plt.show()
7. Creating a Custom Color Palette¶
- Let's create a custom color palette and visualize it.
7.1 Define a list of custom colors¶
In [93]:
Copied!
custom_colors = [
"#1f77b4", # Blue
"#ff7f0e", # Orange
"#2ca02c", # Green
"#d62728", # Red
"#9467bd", # Purple
"#8c564b", # Brown
"#e377c2", # Pink
"#7f7f7f" # Gray
]
custom_colors = [
"#1f77b4", # Blue
"#ff7f0e", # Orange
"#2ca02c", # Green
"#d62728", # Red
"#9467bd", # Purple
"#8c564b", # Brown
"#e377c2", # Pink
"#7f7f7f" # Gray
]
7.2 Create a Colors object and convert to RGB¶
In [94]:
Copied!
custom_palette = Colors(custom_colors)
rgb_palette = custom_palette.to_rgb(normalized=True)
custom_palette = Colors(custom_colors)
rgb_palette = custom_palette.to_rgb(normalized=True)
7.3 Visualize the palette¶
In [95]:
Copied!
plt.figure(figsize=(10, 6))
bars = plt.bar(range(len(custom_colors)), [1] * len(custom_colors), color=custom_colors)
# Add RGB values as labels
for i, bar in enumerate(bars):
rgb_values = [round(val, 2) for val in rgb_palette[i]]
plt.text(bar.get_x() + bar.get_width()/2, 0.5, str(rgb_values),
ha='center', va='center', color='white' if i in [0, 2, 4, 5] else 'black')
plt.title("Custom Color Palette with RGB Values")
plt.xticks([])
plt.yticks([])
plt.show()
plt.figure(figsize=(10, 6))
bars = plt.bar(range(len(custom_colors)), [1] * len(custom_colors), color=custom_colors)
# Add RGB values as labels
for i, bar in enumerate(bars):
rgb_values = [round(val, 2) for val in rgb_palette[i]]
plt.text(bar.get_x() + bar.get_width()/2, 0.5, str(rgb_values),
ha='center', va='center', color='white' if i in [0, 2, 4, 5] else 'black')
plt.title("Custom Color Palette with RGB Values")
plt.xticks([])
plt.yticks([])
plt.show()
Summary¶
In this notebook, we've explored the Colors class from the Cleopatra package. We've seen how to:
- Initialize the Colors class with different color formats
- Convert colors to hexadecimal format
- Convert colors to RGB format
- Check if colors are valid
- Get the type of color
- Visualize colors
- Create custom color palettes
The Colors class provides a convenient way to work with colors in different formats for data visualization and plotting.