Python Codes

Warm/Cool Color Classification – Python

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"
Advertisements

Let me Know What you Think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s