Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi !

Currently, I'm trying to compute colors interpolation with a simple linear interpolation method, but I have a "capacity overshooting" issue when I manipulate those numbers.
I'm not very good at maths, but I understand that when I make my calculations, I overshoot the max value of integers. So I need to convert my R,G & B values in a range [0.0 to 1.0].Right ?

Is there a good way to do this without rounding errors or another problem I haven't seen ?

This is what I do:
Dim R as Double = Color1.R / 255
'...the same with G,B and Alpha

'...do my calculations...
'...convert from range [0.0 - 1.0] to [0 - 255] like this:
Dim RR as Integer = R * 255


I also need working with floats for 3D calculations (according to what I read), so I need to understand the best practices, but don't find good explanations on the web.

Thanks in advance for your help and sorry for my strange english :sigh:
Posted

Does this work ? In C# you need to divide by a float ( 255.0 ) to get a float value. Then you need to decide, do you scale all your values, so if you have a value of 2.0, any 1.0 becomes .5 so they stay in proportion, or do you just say if R > 1.0, R = 1.0.

Your English is just fine.
 
Share this answer
 
Hi Christian and thanks

Then you need to decide, do you scale all your values, so if you have a value of 2.0, any 1.0 becomes .5 so they stay in proportion

Yes, all values need to be in proportion. I think this part is easy as I know minimum and maximum values (0-255).

Does this work ? In C# you need to divide by a float ( 255.0 ) to get a float value.


Hum that was just a "pseudo code", but today I tried this in a simple project and it seems to work fine.
Dim Rdbl As Double = inptR.Value / 255.0  'NumericUpDown controls
Dim Gdbl As Double = inptG.Value / 255.0
Dim Bdbl As Double = inptB.Value / 255.0

lblR.Text = "R: " & Rdbl.ToString  'debug labels
lblG.Text = "G: " & Gdbl.ToString
lblB.Text = "B: " & Bdbl.ToString


Dim Rint As Integer = CInt(Rdbl * 255)
Dim Gint As Integer = CInt(Gdbl * 255)
Dim Bint As Integer = CInt(Bdbl * 255)

lblRint.Text = "R: " & Rint  'debug labels
lblGint.Text = "G: " & Gint
lblBint.Text = "B: " & Bint


The results seems to be always good. When I have "1" in Double ,integer conversion is 255, that's ok.
But I don't understand why, when I enter 128 in Integer, the result in Double is 0.501960784313725 :confused:
Why it's not 0.5000000..... ??

I don't know if that will be a problem for my interpolation calculations :doh:
Maybe there is a better way to do this ?
 
Share this answer
 
v2
Comments
Christian Graus 27-Aug-10 20:44pm    
You should push comment, not answer, to comment. Yes that works, b/c you mae your 255 a float with 255.0. 256/2 is 128. Your maximum is 255. In any case, computer floating point is not 100% accurate.
norrisMiou 3-Sep-10 23:07pm    
Thanks Christian, that's help me. I'm so crazy, I did not think that 255/2 isn't equal to 128.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900