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

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

I use RGB-Linear2.blend, that should be version 2.
Yes, if you can make a script that changes the colors directly on the selected object I guess it is more easy to test.

Setting the display device (none, srgb, rec709) is needed to correctly use the CM. If you don’t need the CM then you can leave display device to none.

2.4x uses the srgb space, while 2.7x can use srgb or rec709 depending on the display device you use.


ok let me till this night
will make a new version
sorry never really look at this bug for color
have a lot of old 2.49 files but I always add my own cycles mat !
so no color problems !

happy bl

here is new version which changes all material diffuse color on selected objects

test it and let me know if OK!

RGB-Linear4.blend (120 KB)

2 buttons version

RGB-Linear5.blend (120 KB)

happy bl

AWESOME !! :slight_smile:

srgb to linear works just fine, I can import any srgb model and convert it to linear values !
unfortunately linear to srgb seems to have some problems, it doesn’t convert well

test it yourself with the dark red value

dark red srgb = 0.5,0,0
dark red linear = 0.214,0,0

so if you convert 0.5,0,0 from srgb to linear you get 0.214,0,0 that is right
while if you convert 0.214,0,0 from linear to srgb you get 0.95,0,0 that is wrong since you should get 0.5,0,0

may be you didn’t code correctly the math in python …

/* 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

the order of evaluation is important:
you first need to do 1/2.4 that is about 0.4166
then power 0.214 (L) by 0.4166 that is about 0.5260
then multiply 0.5260 by 1.055 that is about 0.5549
then subtract 0.055 from 0.5549 that is about 0.5

I gave a look at your python code and I found the bug, you should rewrite the lin2s1 function as follows:

def lin2s1(x):

     a = 0.055
     if x &lt;=0.0031308:
           y = x * 12.92
     elif 0.0031308 &lt; x &lt;= 1 :
           y = 1.055*(x**(1.0/2.4)) - 0.055

     return y

this way it works fine :slight_smile:

nice catch
did u correct it

I already replace the version 3 with a new one anyway

so does seems to work as expected
show us some test so people can see the difference !

thanks
happy bl