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[0],color_inferior[1],color_inferior[2]) print (a)
16 / 256cubed or less than one ten thousands of one percent of all possible colors.... , lol. Good spot. IIRC there is another answer re this converting both ways. ... actually prob less, 15 numerator since zero00would have worked as expected too. $\endgroup$3 * 15 / 256.. not AND never was good at stats $\endgroup$