give me a phyton code for 3 colors, one is a shade of grey, and the other two are one…
- User
give me a phyton code for 3 colors, one is a shade of grey, and the other two are one light, and one dark shade of the same color
- Assistant
give me a phyton code for 3 colors, one is a shade of grey, and the other two are one light, and one dark shade of the same color
Certainly! Below is a Python script that defines three colors:
The script uses the matplotlib library to visually display these colors as colored rectangles. You can customize the colors by changing the HEX codes as desired.
You can install matplotlib using pip if you haven't already:
pip install matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
# Define colors
grey = '#808080' # Shade of grey
# Base color: Let's choose blue for this example
base_color = '#1f77b4' # Base blue color
light_color = '#aec7e8' # Light shade of blue
dark_color = '#03396c' # Dark shade of blue
# Alternatively, you can generate light and dark shades programmatically
# Here's how you can do it using the colorsys library
import colorsys
def lighten_color(hex_color, factor=0.3):
"""Lightens the given color by multiplying (1-luminosity) by the given factor."""
rgb = tuple(int(hex_color.lstrip('#')[i:i+2], 16)/255.0 for i in (0, 2, 4))
h, l, s = colorsys.rgb_to_hls(*rgb)
l = min(1, l + factor * (1 - l))
r, g, b = colorsys.hls_to_rgb(h, l, s)
return '#{0:02x}{1:02x}{2:02x}'.format(int(r*255), int(g*255), int(b*255))
def darken_color(hex_color, factor=0.3):
"""Darkens the given color by multiplying luminosity by the given factor."""
rgb = tuple(int(hex_color.lstrip('#')[i:i+2], 16)/255.0 for i in (0, 2, 4))
h, l, s = colorsys.rgb_to_hls(*rgb)
l = max(0, l - factor * l)
r, g, b = colorsys.hls_to_rgb(h, l, s)
return '#{0:02x}{1:02x}{2:02x}'.format(int(r*255), int(g*255), int(b*255))
# Uncomment below lines to generate light and dark shades programmatically
# light_color = lighten_color(base_color, factor=0.5)
# dark_color = darken_color(base_color, factor=0.5)
# Create a figure and axis to display the colors
fig, ax = plt.subplots(figsize=(6, 2))
# Add rectangles for each color
colors = [grey, light_color, dark_color]
labels = ['Grey', 'Light Blue', 'Dark Blue']
for idx, (color, label) in enumerate(zip(colors, labels)):
rect = Rectangle((idx, 0), 1, 1, color=color)
ax.add_patch(rect)
# Add text labels
plt.text(idx + 0.5, -0.1, label, ha='center', va='top', fontsize=12)
# Set the limits and remove axes
plt.xlim(0, 3)
plt.ylim(-0.2, 1)
ax.axis('off')
plt.title('Color Shades Example', fontsize=16)
plt.show()
Color Definitions:
grey: A standard shade of grey (#808080).base_color: The primary color you want to create light and dark shades for (#1f77b4 is a shade of blue).light_color: A lighter shade of the base color (#aec7e8).dark_color: A darker shade of the base color (#03396c).Generating Shades Programmatically:
lighten_color and darken_color using the colorsys library to adjust the luminosity of the base color. This allows for dynamic generation of light and dark shades.factor as needed.Visualization:
matplotlib to draw rectangles filled with the specified colors.Change Base Color:
base_color variable to any HEX color of your choice. The light_color and dark_color will need to be adjusted accordingly, either manually or by using the provided functions.Adjust Light/Dark Factor:
factor parameter in lighten_color and darken_color to get lighter or darker shades.Add More Colors:
[image: Color Shades Example] open (Replace with actual image if needed)
This script provides a simple way to define and visualize different color shades in Python. You can integrate these color definitions into larger projects, such as GUI applications, data visualizations, or web development.
Not yet included in a published release.