GDI +颜色整数操作 [英] GDI + colors integer manipulation

查看:105
本文介绍了GDI +颜色整数操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

目前,我正在尝试使用一种简单的线性插值方法来计算颜色插值,但是当我处理这些数字时,我遇到了容量超调"问题.
我不是很擅长数学,但是我知道在进行计算时,我会超出整数的最大值.因此,我需要转换我的R,G& B值在[0.0到1.0]范围内.对吗?

是否有一个很好的方法可以解决此问题,而不会出现舍入错误或其他我未曾见过的问题?

这就是我的工作:

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



我还需要使用浮点数进行3D计算(根据我阅读的内容),因此我需要了解最佳做法,但在网上找不到很好的解释.

在此先感谢您的帮助,对于我的英语很奇怪,对不起:sigh:



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:

推荐答案

这行吗?在C#中,您需要除以浮点数(255.0)以获取浮点值.然后,您需要确定是否要缩放所有值,因此,如果您的值为2.0,则任何1.0都将变为0.5,以使它们保持比例,或者您只是说R是否大于等于? 1.0,R = 1.0.

您的英语很好.
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.


克里斯蒂安,谢谢

然后您需要确定是否要缩放所有值,所以如果您的值为2.0,则任何1.0都将变为0.5,以便它们保持比例

是的,所有值都必须成比例.我认为这部分很容易,因为我知道最小值和最大值(0-255).

这行吗?在C#中,您需要除以浮点数(255.0)以获得浮点值.


嗡嗡声,这只是一个伪代码",但是今天我在一个简单的项目中尝试了一下,它似乎可以正常工作.
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



结果似乎总是很好.当我在Double中输入"1"时,整数转换为255,就可以了.
但是我不明白为什么,当我在Integer中输入128时,Double的结果是0.501960784313725:confused:
为什么不是0.5000000 ..... ??

我不知道这对我的插值计算是否会造成问题:doh:
也许有更好的方法可以做到这一点?



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 ?


这篇关于GDI +颜色整数操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆