Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting hex color to RGB and vice-versa

Tags:

hex

colors

rgb

What is the most efficient way to do this?

like image 742
moo Avatar asked Oct 18 '08 01:10

moo


People also ask

How do you convert HEX color to RGB?

Get the 2 left digits of the hex color code and convert to decimal value to get the red color level. Get the 2 middle digits of the hex color code and convert to decimal value to get the green color level. Get the 2 right digits of the hex color code and convert to decimal value to get the blue color level.

Is HEX color the same as RGB?

There is no informational difference between RGB and HEX colors; they are simply different ways of communicating the same thing – a red, green, and blue color value. HEX, along with its sister RGB, is one of the color languages used in coding, such as CSS. HEX is a numeric character based reference of RGB numbers.

How do I find the RGB color code?

Click on the color selector icon (the eyedropper), and then click on the color of in- terest to select it, then click on 'edit color'. 3. The RGB values for that color will appear in a dialogue box.


1 Answers

In python:

def hex_to_rgb(value):     """Return (red, green, blue) for the color given as #rrggbb."""     value = value.lstrip('#')     lv = len(value)     return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))  def rgb_to_hex(red, green, blue):     """Return color as #rrggbb for the given color values."""     return '#%02x%02x%02x' % (red, green, blue)  hex_to_rgb("#ffffff")           #==> (255, 255, 255) hex_to_rgb("#ffffffffffff")     #==> (65535, 65535, 65535) rgb_to_hex(255, 255, 255)       #==> '#ffffff' rgb_to_hex(65535, 65535, 65535) #==> '#ffffffffffff' 
like image 187
Jeremy Cantrell Avatar answered Sep 28 '22 05:09

Jeremy Cantrell