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?


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)))    


testing the code I got:

rgb to hex convertion

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


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

  • $\begingroup$ What are you asking exactly? How to gamma-correct a colour? Or how to convert an RGBA value to hexadecimal notation? $\endgroup$
    – dr. Sybren
    Nov 27, 2017 at 22:54
  • $\begingroup$ @dr.Sybren I want the hex value or how convert the hex to RGB $\endgroup$
    – yhoyo
    Nov 27, 2017 at 22:55
  • $\begingroup$ 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. $\endgroup$
    – dr. Sybren
    Nov 27, 2017 at 22:59
  • $\begingroup$ @dr.Sybren I thought that too. But No... Or something I'm doing wrong $\endgroup$
    – yhoyo
    Nov 27, 2017 at 23:01
  • $\begingroup$ 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. $\endgroup$
    – dr. Sybren
    Nov 27, 2017 at 23:48

2 Answers 2


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
        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 = ""
    while i < 3:
        val = str(to_hex(rgb[i]))
        val = val[2:]
        if len(val) == 1:
            val += val
    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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.