# Python De-Linearize Colors

I'm working on a custom exporter from blender to my iOS game engine, and all works fine and dandy, but I just noticed that all of the colors are messed up! I've looked at the Blender documentation and have searched google and found that the problem is that Blender works in a linearized color space.

How can I convert the colors to a format that other programs like? When I make a color in photoshop using the RGB values that blender gives me I get the skrewy colors, but when I copy the Hex code the color is right. I see that it says the Hex is Gamma Corrected. I'm using Mesh.materials[].diffuse_color, how can I correct this value?

Thanks for any help!

CoDEmanX's link had a way to fix this. It's more of a work around than a solution, but I can just change the display device in the "Scene" tab to None. This allows me to export the colors exactly as seen in Blender. It might mess up rendering a bit, but these are models for a game and in my case I don't need to render in Blender. One speed bump, though, is that any colors set before changing it will be messed up and will need to be corrected.

• Sergey suggests to write a python script for sRGB to linear conversion to compensate the change in color as you switch the display device (color spaces). There's a formula on wikipedia. – CoDEmanX Feb 28 '14 at 0:51

There are three states of an image in Blender: the input state, the reference state, and the output state. While the internal reference space is always a linearized model, the input and output states are variable.

In this case, if you take a typical JPEG and open it in Blender, the default transfer curve that is assumed is sRGB. That is, it takes the canonized specification of sRGB's transfer curve and inverts it and bends the colours of your JPEG accordingly. This results in, as best as a JPEG can deliver (which is not very good) a radiometrically linear version of the RGB representation. Why? In order to compose and blend colours correctly, colours must be expressed relative to their radiometrically correct luminance. If not, colours will result in very strange blending.

So now we know why, what happens when things come out of Blender?

This is all handled via OpenColorIO, and as such, you will need to make sure that your output format handles things correctly. If you are dealing with this via Python, the correct approach is to apply a transformation on the RGB data to whatever output transformation you require. In most instances, you will want to transform from the internal reference linear space to sRGB, which will apply the correct intensity transfer curve to get the RGB back into that typical JPEG intensity curve you began with.

If you are generating files out of Blender, much of this is handled automatically. PNG, JPEG, TIFF, etc. will all perform the sRGB transformation out of the reference space. EXR, however, being a strictly linear storage medium, will leave it in linear, whatever colour space it happened to be in within the reference space.