xxxxxxxxxx
def rgb(r, g, b):
# helper function
def help(c):
if c<0: return 0
if c>255: return 255
return c
# make sure that values are within bounds
r = help(r)
g = help(g)
b = help(b)
# convert to hex
# maintain 2 spaces each
val = "%02x%02x%02x" % (r, g, b)
# return UpperCase string
return val.upper()
Code language: Python (python)
xxxxxxxxxx
from colormap import rgb2hex
from colormap import hex2rgb
print(rgb2hex(255, 255, 255))
print(hex2rgb('#FFFFFF'))
>>> #FFFFFF
>>> (255, 255, 255)
xxxxxxxxxx
def RGBToHex(r, g, b):
return '#%02X%02X%02X' % (r, g, b)
print(RGBToHex(255, 255, 255))
xxxxxxxxxx
# Convert RGB to HEX
rgb = (255,255,255) # ---------> pure white
print("#%02x%02x%02x" % rgb) # -----> #ffffff
# - sabz
xxxxxxxxxx
import matplotlib
print(matplotlib.colors.to_hex([ 0.47, 0.0, 1.0 ]))
print(matplotlib.colors.to_hex([ 0.7, 0.321, 0.3, 0.5 ], keep_alpha=True))
print(matplotlib.colors.to_rgb("#aabbcc"))
print(matplotlib.colors.to_rgb("#ddee9f"))