4
$\begingroup$

The color accuracy is completely wrong when you convert from byte rgb to float.

13/255.0, 105/255.0, 171/255.0 =
R: 0.050980, G: 0.411764, B: 0.67058

While if you were to convert the byte rgb to hex and insert that into blender where you change the base color of the material, you would get these values.

R: 0.004025, G: 0.141263, B: 0.40724

I first thought that the inaccuracy might happen because of floating division accuracy but that isn't the case and its most likely some sort of gamma correction happening within blender.

The problem is that you can not create materials by giving diffuse_color hex values let alone byte values since it only accepts floating numbers. https://docs.blender.org/api/current/bpy.types.Material.html#bpy.types.Material.diffuse_color

Is there some sort of way to do the gamma correction which blender does but apply it when converting from byte rgb to floats?

$\endgroup$

1 Answer 1

5
$\begingroup$

Hex colors are in sRGB. You need linear colors. The conversion is (from here)

def color_srgb_to_scene_linear(c):
    if c < 0.04045:
        return 0.0 if c < 0.0 else c * (1.0 / 12.92)
    else:
        return ((c + 0.055) * (1.0 / 1.055)) ** 2.4

print(color_srgb_to_scene_linear(13/255))  # => 0.00402
print(color_srgb_to_scene_linear(105/255)) # => 0.14126
print(color_srgb_to_scene_linear(171/255)) # => 0.40724
$\endgroup$

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.