Python Codes

RGB and Hex Conversions – Python

Indian Bloggers

While dealing with color operations we would need to work with RGB and Hex values and need conversation between formats for the operations. Here are the python code snippets to achieve the same.

1. Converting RGB to HEX

# Module to convert rgb to hex
def rgb_to_hex(rgb):
    return '#%02X%02X%02X' % (rgb)

2. Converting HEX to 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))

5 thoughts on “RGB and Hex Conversions – Python

  1. PH? I continue climbing the mountain top. Every minute of my waking hours, even my sleeping hours, I’m getting closer, closer, closer to the summit! How? Don’t ask! Just keep watching my task. All I can see like a bee, are the flowers to soak up the pollen for the honey to be!

    BTW wish you could remote me and fix my jumping mouse and my jumping computer at its will not mine. I done exhausted all my wealth of comp. knowledge to fix the problem to no avail. So? I suffer! 🙂

    Liked by 1 person

Let me Know What you Think!

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s