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