I am trying to get a hex value out of material color. I find an reply here by @douripo: Get Hex triplet for color and real 256 RGB from diffuse Color using python
it successfully converts most of the colors to hex correctly. except a few. here is an example it does not convert right.
in blender, add a material to default cube and manually set its diffuse_color to hex 60590A (brown-ish yellow)
run below code in text editor, it would return 6059AA (purple-ish blue). could anyone check where went wrong? and hopefully a fix?
import bpy import math #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,color_inferior,color_inferior) print (a)