# Get Hex triplet for color and real 256 RGB from diffuse Color using python

as you know the RGB structure each parameter (red, green, and blue) defines the intensity of the color as an integer between 0 and 255., Actually, in Blender, you have that in sRGB, then any traditional convertion to HEX work BECAUSE the Hex in blender is GAMMA CORRECTED.

also in Documentation I can NOT get any reference about how get the HEX TRIPLE FOR COLOR CODE ...

how to get both with python?

UPDATE:

I want the hex value or how convert the hex to RGB

AS @dr.Sybren I thougth That was just a straight conversion of the bytes in RGB order converted to hexadecimal notation, and then tried some simple code as @tet_ii

context = bpy.context
obj = context.object

color_inferior= obj.material_slots['inferior'].material.diffuse_color

a = '#%02x%02x%02x' % (int(255.999 * pow(color_inferior.r, 1/2.2)),int(255.999 * pow(color_inferior.g, 1/2.2)),int(255.999 * pow(color_inferior.b, 1/2.2)))

print(a)


testing the code I got:

also, I test other codes with the same or worst result:

https://stackoverflow.com/questions/3380726/converting-a-rgb-color-tuple-to-a-six-digit-code-in-python

then. Can I capture the value directly from blender without any convertion? or do a 100% accurate conversion?

• What are you asking exactly? How to gamma-correct a colour? Or how to convert an RGBA value to hexadecimal notation? Nov 27, 2017 at 22:54
• @dr.Sybren I want the hex value or how convert the hex to RGB Nov 27, 2017 at 22:55
• That's just a straight conversion of the bytes in RGB order converted to hexadecimal notation. The digits are then RRBBGG, where each letter represents 4 of the 8 bits that make up a byte. Nov 27, 2017 at 22:59
• @dr.Sybren I thought that too. But No... Or something I'm doing wrong Nov 27, 2017 at 23:01
• Then describe what you are doing, instead of shouting (uppercase letters means shouting) that things don't work. Give us the math you're applying, the results you get, and the results you expected. Nov 27, 2017 at 23:48

This code adapted from devtalk.blender.org, convert color from blender standard active material to hex.

import bpy

#code by : brecht - devtalk.blender.org
def to_hex(c):
if c < 0.0031308:
srgb = 0.0 if c < 0.0 else c * 12.92
else:
srgb = 1.055 * math.pow(c, 1.0 / 2.4) - 0.055

return hex(max(min(int(srgb * 255 + 0.5), 255), 0))

def toHex(r,g,b):
rgb = [r,g,b]
result = ""
i=0
while i < 3:
val = str(to_hex(rgb[i]))
val = val[2:]
if len(val) == 1:
val += val
result+=val
i+=1
return result

ob = bpy.context.object
color_inferior = ob.active_material.diffuse_color

a = toHex(color_inferior[0],color_inferior[1],color_inferior[2])

print (a)


Try turn off color manager for display device:

bpy.context.scene.display_settings.display_device = 'None'


After, color circle give value 0xCA0001 for 0.791; 0.000; 0.003