help please: is there a fast way to convert srgb values to linear ?

The issue I came across is that when importing models in a Blender scene, Blender assumes that the color values are linear, so the imported models get in with washed out colors.

This happens both with external formats (dae fbx obj 3ds etc) and even with old Blender formats (2.49 and before).

I opened a thread at blender.org for this issue and they say the problem is that i/o scripts are not CM aware so Blender fails importing srgb colors. They will add CM capabilities to i/o scripts in a future release. see https://developer.blender.org/T43025

But meanwhile, since I need a way to use my old models in actual Blender scenes, does anyone know of a fast way to convert srgb to linear ? Actually I have to correct every single color by hand and this is a real PITA. Thank you in advance for any help you could provide.

best regards, Alessandro

is there like a math solution which might be use with a script ?

mat be ask question in python forum!

happy bl

The issue I came across is that when importing models in a Blender scene, Blender assumes that the color values are linear, so the imported models get in with washed out colors.

This happens both with external formats (dae fbx obj 3ds etc) and even with old Blender formats (2.49 and before).

I opened a thread at blender.org for this issue and they say the problem is that i/o scripts are not CM aware so Blender fails importing srgb colors. They will add CM capabilities to i/o scripts in a future release. see https://developer.blender.org/T43025

But meanwhile, since I need a way to use my old models in actual Blender scenes, does anyone know of a fast way to convert srgb to linear ? Actually I have to correct every single color by hand and this is a real PITA. Thank you in advance for any help you could provide.

best regards, Alessandro

p.s. just found a link for srgb-linear conversion math, don’t know if this could be useful to write a script http://entropymine.com/imageworsener/srgbformula/
Actually I just copy and paste the HEX values between none and srgb display device and this works fine, but as I said I have to do it by hand for every color and it’s a PITA.

done, thanks for the suggestion :slight_smile:

Something like that ?

http://excamera.com/sphinx/article-srgb.html

EDIT : I don’t know where to find the “where” function. I’ll do that with a simple condition

def s2lin(x):
...     a = 0.055
...     return x * (1.0 / 12.92) if x <= 0.04045 else pow((x + a) * (1.0 / (1 + a)), 2.4)

more like this



 print ('Color S RGB  to Linear RFB')
print ()
 
 
 
def s2lin(x):
 a = 0.055
 if x <=0.04045 :
  y = x * (1.0 / 12.92)
 else:
  y = pow( (x + a) * (1.0 / (1 + a)), 2.4)
 return y
 
x = 0.1
 
z = s2lin(x)
 
print ('x =', x, 'z =',z)
print ()
 
print ('Linear RGB  to   S RGB')
print ()
 
def lin2s(x):
 a = 0.055
 if x <=0.0031308:
  y = x * 12.92
 else:
  y = (1 + a) * pow(x, 1 / 2.4) - a
 return y
 
x = 0.1
 
z = lin2s(x)
 
print ('x =', x, 'z =',z)
 
print ()


happy bl

1 Like

Seems very interesting !! Unfortunately I can’t write a script myself since I don’t know python and/or the Blender API. So I’m looking for a kind coder who could solve the issue for the community. It would be enough to have two scripts:

  1. srgb_to_linear: to convert srgb values of current selection to linear values
  2. linear_to_srgb: to convert back

These two scripts alone could provide the artist with a powerful tool to fix any import/export issue Blender actually has. Are you a coder ? Could any of you please code those scripts ?

p.s. just found a link for srgb-linear conversion math, don’t know if this could be useful to write a script http://entropymine.com/imageworsener/srgbformula/
Actually I just copy and paste the HEX values between none and srgb display device and this works fine, but as I said I have to do it by hand for every color and it’s a PITA.

p.p.s. so actually my workflow to convert from srgb to linear is

  • turn off CM by setting display device to none
  • import the srgb model into the scene (dae, fbx, obj etc)
  • copy the HEX color values for every material in the model
  • turn on CM by setting display device to srgb
  • paste back the color values into the HEX slots

as I said this works fine but it’s SLOOOOW way … …

found that one but don’t understand what vars are ?

and what do you mean for every color
do you mean for every mat on every objects ?

or what that modify an import export script ?
or stand alone script

also is this or Bl or cycles mats or textures ?

happy bl

  1. as far as I can understand in the link above vars are rgb values, for example

0 ≤ S ≤ 0.04045 => L = S/12.92

means that when red, green or blue value of srgb (S) is below 0.04045 (from 0 to 1 scale) then the corresponding linear value (L) is S/12.92

  1. for every color I mean the rgb values of every material in the model. For example if the model has 10 materials then I have 10 rgb values to convert.

  2. I’m not sure what you mean for stand alone script. It should be a script that the user could run on the current selection, so inside blender. The script should find all the materials in the current selection and convert the rgb values appropriately.

  3. Cycles or not doesn’t matter at all, it is a CM issue and it affects any render engine you use, cycles, BI and even the opengl render. And it affects material colors only, since srgb textures are already handled the correct way (CM managed)

Please let me know if you need more info … if you could write those scripts it would be very very useful indeed … and of course let me know if I can help in any way :slight_smile:

for last link with S L vars
what these represent is RGB ?
S = R L = G and what about B

well I hope someone else has a script already made for that cause
if you have to do that for all Bl mat and may be texture color and what about image color ?

then above that you have all the world colors and then all the cycles mat

and which color is it the one for Diffuse or Spec ?

that is not so easy it is possible but lot’s of work

to start which one is most useful ?
cycles

happy bl

  1. S is the srgb value of red,green,blue components and L is the linear value of red,green,blue components
  2. What needs to be converted is only the diffuse value of all materials. Cycles or BI doesn’t matter, it is a CM issue so it affects every render engine you use. Textures are already handled fine and don’t need any conversion.
  3. The conversion should be made on selected objects only, so the user can leave alone objects that don’t need any conversion.

Basically, the two scripts should convert the diffuse value of all the materials in the selected objects.

Please let me know if you need more info and if I can help in any way. And thank you very much for your reply. I understand that writing a script is not so easy and takes a lot of time …

bye, Alessandro :slight_smile:

I did another small script to change BL diffuse color or spec color

now this is only for material color
and what you said about color is wrong the fact of changing the diffuse won’t affect any other color somewhere else or in cycles!

anyway I can upload the material change script but this does not tell me how to modify the RGB color !
I will try to add this to a small panel in tool pro panel so it is easier to use!

I can read the RGB values but then what do u do with the algo?
do you need to get the HSV and modify the H value ?

thanks
happy bl

if you can work on those scripts it is more than amazing !!

I can’t understand why you say that changing the diffuse color doesn’t affect cycles
or may be I don’t understand what you mean

anyway, this is the math, hope it is clear enough

/* SRGB TO LINEAR
/* this assumes red,green,blue values from 0 to 1
/* and convert srgb red to linear red
/* you need to do the same for green and blue
S = red
if S <= 0.04045 then L = S/12.92 else L = ((S+0.055)/1.055)^2.4
red = L

/* LINEAR TO SRGB
/* this assumes red,green,blue values from 0 to 1
/* and convert linear red to srgb red
/* you need to do the same for green and blue
L = red
if L <= 0.0031308 then S = L12.92 else S = 1.055(L^(1/2.4))-0.055
red = S

please let me know if I can help in any way

bye, Alessandro :slight_smile:

there different material in blender
1 - blender normal
2 - blender nodes mat
3 cycles mat

these are totally independent material !

try to run script in text editor and looks at the values in console
let me know if it make sense or what ?

RGB-Linear2.blend (113 KB)

test this second version
I added one cube showing the new color
which can be compared to the first color!

happy bl

it seems it does nothing, this is what I did

  • load the scene rgb-linear2.blend
  • set display device to none
  • select cube.000
  • change material.002 to dark red (rgb 0.5,0,0)
  • run the script

If I check the materials on the two cubes they stay at the same colors they had before running the script. Do I have to do something different for testing ? How does the script work ?

for your easy testing 0.5 srgb (80 hex) = 0.216 linear

so if you

  • turn display device to none
  • set material to dark red (0.5,0,0)
  • run the srgb_to_linear script

if the script works well then you should get rgb changed to 0.216,0,0
please let me know if you need more info and/or if I can help … and thank you so much for your work so far !!

bye, Alessandro :slight_smile:

what do you mean by turn display device to none
how to do that and where ?

to run the script
select one cube
then text editor run script
after look in tool pro panel - there is a new panel
select menu and select first or second option
then click on the bottom button call custom object

have to find a better name for this run button

after you do that you should see a new cube added with the converted color !

I will try you test later

thanks

did your test and it works like a charm
select first item in menu and click on custom button


happy bl

is this a bug with import from 2.49 ?

I did cube red in 2.49 and imported in 2.5

and the RFB value are the same but may be color is not the same !


rendered in 2.5


happy bl

Well !! The script works fine with a one color cube the first time I use it. It converts the color just fine !! :slight_smile:

If I use the script more than one time on the same source cube, then the generated cube becomes darker and darker. But may be in this first version you didn’t planned a multiple use of the script.

Also, the script works odd with the 3 colors cube you provide in the scene. If I run the script on the 3 colors cube it generates a blue cube. But may be in this first version you didn’t support a multiple materials object.

The effect you see when importing a 2.49 object is the CM issue Blender suffers right now and it is the reason why your script is needed. Blender 2.5x-2.7x reads the 2.49 colors as linear values, while 2.49 uses srgb values. This also applies to any imported format (dae,fbx,obj,3ds etc). Blender always reads colors as linear values, and this is wrong since those formats use srgb values.

So to import an object you have to

  • turn off CM by setting display device to none
  • import the object (blender 2.49,dae,fbx,obj,3ds etc)
  • run the srgb_to_linear script
  • turn on CM by setting display device to srgb

You can find the display device settings in: scene tab > color management > display device
I am ready to test your next script versions ! And good job so far !

bye, Alessandro

did u use version 1 or version 2 of my script
sorry made many mistake in version 1
use version 2

this script adding a cube is only to make a test on a certain color which is converted on the cube

I can make a version where it wont add a cube and simply do the color conversion of all mat diff on an object
might be easier to see it may be

is this display device = none thing required or what ?
cause in last test I did not use it and there is color conversion!

now I thought the old 2.49 use RGB only and new one in 2.5 use the sRGB
but may be my mistake here !

happy bl

happy bl