The code is appropriately commented to understand the necessitates.
# Assumption:
# A global dictionary to hold colors along with hex values
# called 'colors' is already present.
# key is the hex and value is the color name in the dictionary.
# Signature: hex --> color-name
# 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))
# Load all the colors with warm/cool classification
def load_warm_cool():
global colors
# Dictionary that holds color and classification in the form of
# color-name --> warm/cool
warm_cool = {}
# Get the RGB values
for key, value in colors.items():
r, g, b = hex_to_rgb(key)
# Classify
# This calssification is based on the simple rule that if Blue color component
# is greater that red, it is a cool color
if b >= r:
warm_cool[value] = "cool"
else:
warm_cool[value] = "warm"