10
$\begingroup$

let's take the hsv color 0.4, 0.8, 1.0

everywhere I looked this converted to Hex 33FF85 - even in Blender

when I convert it to RGB(using python colorsys, python Color from math, webservices),
it is 0.2, 1, 0.52 - but Blender shows me this:

hsv RGB

what I'm doing currently is this:

bpy.data.materials["name"].diffuse_color = colorsys.hsv_to_rgb(0.4, 0.8, 1)

how do I apply the hsv color to my object correctly? If I convert the color to rgb it obviously does not work as it should...

$\endgroup$
1

1 Answer 1

14
$\begingroup$

The RGB values in Blender use a linear color model while the Hex values use gamma corrected non-linear color model.

To convert between these two, you would have to manually do the gamma calculations using the transfer function of your color profile, wich is set in the Color Management section of your Scene – set to sRGB by default.

color management section in blender

The excact conversion is listed in the sRGB Wikipedia article.

def linear_to_srgb(r, g, b):
    def linear(c):
        a = .055
        if c <= .0031308:
            return 12.92 * c
        else:
            return (1+a) * c**(1/2.4) - a
    return tuple(linear(c) for c in (r, g, b))

def srgb_to_linear(r, g, b):
    def srgb(c):
        a = .055
        if c <= .04045:
            return c / 12.92
        else:
            return ((c+a) / (1+a)) ** 2.4
    return tuple(srgb(c) for c in (r, g, b))

import colorsys
col = colorsys.hsv_to_rgb(.4, .8, 1)
# (0.19999999999999996, 1, 0.5200000000000002)

srgb_to_linear(*col)
# Values to be set in the RGB tab of the color picker:
# (0.03310476657088504, 1.0, 0.23302199930143835)

While the HSV tab of the color picker doesn't give any indication as to whether the values are gamma corrected, this ticket does indicate that the displayed HSV values actually are gamma corrected as well. This would explain why the Hex representation does match the Hex value shown by various color converters.

So, to apply a non-linear HSV color to your object, would have to call

col = srgb_to_linear(*colorsys.hsv_to_rgb(0.4, 0.8, 1))
bpy.data.materials["name"].diffuse_color = col
$\endgroup$
1
  • $\begingroup$ worked like a charm, thanks! Also now I know the behaviour of tuples in parameters, regarding the importance of the * ;) $\endgroup$
    – xeruf
    Commented May 23, 2017 at 21:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .