farm-recolor / compColors.py
vettorazi's picture
copied local files. docker initial setup
52cbb9c
raw
history blame contribute delete
No virus
3.43 kB
#This file generates Complementary Colours palletes using three different alghoritms:
#Adjacent (https://itsphbytes.wordpress.com/2016/09/25/getting-adjacent-colors-python-code/),
#Complementary (https://www.101computing.net/complementary-colours-algorithm/) and Gradient.
#The Gradient alghoritm is based on the code from https://stackoverflow.com/questions/22607043/python-generate-color-palette-from-image
import colorsys
def gradient_colors(color1, color2):
"""
It takes two colors and returns a list of 5 colors that are
complementary to the two colors. Basically is a gradient between
the two colors.
"""
if color1.startswith('#'):
color1 = color1[1:]
if color2.startswith('#'):
color2 = color2[1:]
# Convert hex colors to RGB
color1_rgb = tuple(int(color1[i:i+2], 16) for i in (0, 2, 4))
color2_rgb = tuple(int(color2[i:i+2], 16) for i in (0, 2, 4))
# Calculate the difference between the two colors
diff = [c2 - c1 for c1, c2 in zip(color1_rgb, color2_rgb)]
# Generate the palette
palette = []
for i in range(5):
r = color1_rgb[0] + diff[0] * i / 4
g = color1_rgb[1] + diff[1] * i / 4
b = color1_rgb[2] + diff[2] * i / 4
palette.append('#%02x%02x%02x' % (int(r), int(g), int(b)))
return palette
def complementary_colors(hexcolor):
# Check if the input starts with '#'. If it does, remove it.
if hexcolor.startswith('#'):
hexcolor = hexcolor[1:]
# Convert hex to RGB
r = int(hexcolor[0:2], 16)
g = int(hexcolor[2:4], 16)
b = int(hexcolor[4:6], 16)
# Calculate complementary colors
comp_r = 255 - r
comp_g = 255 - g
comp_b = 255 - b
# Convert RGB back to hex
comp_hex = "#{:02X}{:02X}{:02X}".format(comp_r, comp_g, comp_b)
return comp_hex
# Module to convert rgb to hex
def rgb_to_hex(rgb):
return '#%02X%02X%02X' % (rgb)
# Module to convert hex to rgb
def hex_to_rgb(value):
value = value.lstrip('#')
return tuple(int(value[i:i+2], 16) for i in (0, 2 ,4))
# This module gets the adjacent colors.
# They can be obtained by rotating the color by 30 and 360
# degrees.
def adjacent_colors(color):
# List to hold two colors
adjacent_colors = []
# Get the RGB
(r, g, b) = hex_to_rgb(color)
# Set the degree
d = 30/360
# Values has to be between 0 to 1
r = r / 255.0
g = g / 255.0
b = b / 255.0
# Convert to HLS using colorsys
h, l, s = colorsys.rgb_to_hls(r, g, b)
# Rotate the color
h = [(h+d) % 1 for d in (-d, d)]
# For both the colors, convert back to RGB and
# load to list
for hi in h:
r, g, b = colorsys.hls_to_rgb(hi, l, s)
r = int(r * 255.0)
g = int(g * 255.0)
b = int(b * 255.0)
hex_val = rgb_to_hex((r,g,b))
adjacent_colors.append(hex_val)
print(adjacent_colors)
return adjacent_colors
# # Test complementarty colors:
# color = "#771958"
# complementary = complementary_colors(color)
# print(f"Original color: {color}")
# print(f"Complementary color: {complementary}")
# # Test adjacent colors:
# print(f"Adjacent colors for {color}:")
# colors = adjacent_colors(color)
# # Test gradient colors:
# print(f"Gradient colors between {colors[0]} and {colors[1]}:")
# gradient = gradient_colors(colors[0], colors[1])
# for color in gradient:
# print(color)