I'm trying to add a fade effect to my form by manually changing the opacity of the form but I'm having some trouble calculating the correct value to increment by the Opacity value of the form.
我试图通过手动更改窗体的不透明度来为我的窗体添加淡入淡出效果,但是我在计算正确值以通过窗体的不透明度值增加时遇到一些麻烦。
I know I could use the AnimateWindow API but it's showing some unexpected behavior and I'd rather do it manually anyways as to avoid any p/invoke so I could use it in Mono later on.
我知道我可以使用AnimateWindow API,但它显示出一些意想不到的行为,我宁愿手动操作,以避免任何p / invoke,所以我可以稍后在Mono中使用它。
My application supports speeds ranging from 1 to 10. And I've manually calculated that for a speed of 1 (slowest) I should increment the opacity by 0.005 and for a speed of 10 (fastest) I should increment by 0.1. As for the speeds between 1 and 10, I used the following expression to calculate the correct value:
我的应用程序支持从1到10的速度。我手动计算速度为1(最慢)我应该增加不透明度0.005和速度10(最快)我应该增加0.1。至于1到10之间的速度,我使用以下表达式来计算正确的值:
double opSpeed = (((0.1 - 0.005) * (10 - X)) / (1 - 10)) + 0.1; // X = [1, 10]
I though this could give me a linear value and that that would be OK. However, for X equal 4 and above, it's already too fast. More than it should be. I mean, speeds between 7 and 10, I barely see a difference and the animation speed with these values should be a little more spaced
我虽然这可以给我一个线性值,那就没关系。但是,对于等于4及以上的X,它已经太快了。超过应有的。我的意思是,速度在7到10之间,我几乎看不出差异,这些值的动画速度应该稍微间隔一点
Note that I still want the fastest increment to be 0.1 and the slowest 0.005. But I need all the others to be linear between them.
请注意,我仍然希望最快增量为0.1,最慢增量为0.005。但我需要所有其他人在他们之间保持线性关系。
What I'm doing wrong?
我做错了什么?
EDIT: It actually makes sense why it works like this, for instance, for a fixed interval between increments, say a few milliseconds, and with the equation above, if X = 10, then opSpeed = 0.1 and if X = 5, then opSpeed = 0.47. If we think about this, a value of 0.1 will loop 10 times and a value of 0.47 will loop just the double. For such a small interval of just a few milliseconds, the difference between these values is not that much as to differentiate speeds from 5 to 10.
编辑:它实际上是有道理的,为什么它这样工作,例如,对于增量之间的固定间隔,比如几毫秒,并且使用上面的等式,如果X = 10,则opSpeed = 0.1并且如果X = 5,则opSpeed = 0.47。如果我们考虑这个,值为0.1将循环10次,值为0.47将循环为double。对于仅几毫秒的这么小的间隔,这些值之间的差异并不大到将速度从5分到10分。
5 个解决方案
#1
I think what you want is:
我想你想要的是:
0.005 + ((0.1-0.005)/9)*(X-1)
for X ranging from 1-10
X的范围从1-10
This gives a linear scale corresponding to 0.005 when X = 1 and 0.1 when X = 10
当X = 1时,这给出对应于0.005的线性标度,当X = 10时给出对应于0.1的线性标度
After the comments below, I'm also including my answer fit for a geometric series instead of a linear scale.
在下面的评论之后,我还包括我的答案适合几何系列而不是线性比例。
0.005 * (20^((X-1)/9)))
Results in a geometric variation corresponding to 0.005 when X = 1 and 0.1 when X = 10
当X = 1时,产生对应于0.005的几何变化,当X = 10时,得到0.1
After much more discussion, as seen in the comments below, the updates are as follows.
经过更多讨论后,如下面的评论所示,更新如下。
@Nazgulled found the following relation between my geometric series and the manual values he actually needed to ensure smooth fade animation.
@Nazgulled在我的几何系列和他确实需要的手动值之间找到了以下关系,以确保平滑的淡入淡出动画。
The relationship was as follows: I ♥ Graphs! :) http://i41.tinypic.com/9v8lqg.jpg
关系如下:我♥图! :) http://i41.tinypic.com/9v8lqg.jpg
Which means a geometric/exponential series is the way to go.
这意味着几何/指数系列是要走的路。
After my hours of trying to come up with the appropriate curve fitting to the right hand side graph and derive a proper equation, @Nazgulled informed me that Wolfram|Alpha does that. Seriously amazing. :)
经过几个小时的努力,想出适当的曲线拟合到右侧图表并得出一个合适的方程式,@ Nazgulled告诉我,Wolfram | Alpha就是这么做的。真是太神奇了。 :)
He should have what he wants now, barring very high error from the equation above.
他应该拥有他现在想要的东西,除非上面的等式有很高的误差。
#2
Your problem stems from the fact that the human eye is not linear in its response; to be precise, the eye does not register the difference between a luminosity of 0.05 to 0.10 to be the same as the luminosity difference between 0.80 and 0.85. The whole topic is complicated; you may want to search for the phrase "gamma correction" for some additional information. In general, you'll probably want to find an equation which effectively "gamma corrects" for human ocular response, and use that as your fading function.
你的问题源于人眼的反应不是线性的;准确地说,眼睛没有记录0.05到0.10之间的亮度与0.80和0.85之间的亮度差异相同。整个主题很复杂;您可能想要搜索短语“伽马校正”以获取一些其他信息。一般来说,您可能希望找到一个有效“伽马校正”人类眼睛反应的方程式,并将其用作您的衰落函数。
#3
It's not really an answer, but I'll just point out that everyone who's posted so far, including the original question, are all posting the same equation. So with four independent derivations, maybe we should assume that the equation was probably correct.
这不是一个真正的答案,但我只想指出,到目前为止发布的每个人,包括原始问题,都发布了相同的等式。所以有了四个独立的推导,也许我们应该假设方程可能是正确的。
I did the algebra, but here's the code to verify (in Python, btw, with offsets added to separate the curves:
我做了代数,但这里是验证的代码(在Python中,顺便说一句,添加了偏移量来分隔曲线:
from pylab import *
X = arange(1, 10, .1)
opSpeed0 = (((0.1 - 0.005) * (10 - X)) / (1 - 10)) + 0.1 # original
opSpeed1 = 0.005 + ((0.1-0.005)/9)*(X-1) # Suvesh
opSpeed2 = 0.005*((10-X)/9.) + 0.1*(X-1)/9. # duffymo
a = (0.1 - 0.005) / 9 #= 0.010555555555... # Roger
b = 0.005 - a #= -0.00555555555...
opSpeed3 = a*X+b
nonlinear01 = 0.005*2**((2*(-1 + X))/9.)*5**((-1 + X)/9.)
plot(X, opSpeed0)
plot(X, opSpeed1+.001)
plot(X, opSpeed2+.002)
plot(X, opSpeed3+.003)
plot(X, nonlinear01)
show()
Also, at Nazgulled's request, I've included the non-linear curve suggested by Suvesh (which also, btw, looks quite alot like a gamma correction curve, as suggested by McWafflestix). The Suvesh's nonlinear equation is in the code as nonlinear01
.
另外,在Nazgulled的要求下,我已经包含了Suvesh提出的非线性曲线(顺便说一句,顺便说一下,看起来很像伽马校正曲线,正如McWafflestix所建议的那样)。 Suvesh的非线性方程在代码中是非线性的01。
alt text http://i41.tinypic.com/2extx0k.png
替代文字http://i41.tinypic.com/2extx0k.png
#4
Here's how I'd program that linear relationship. But first I'd like to make clear what I think you're doing.
这是我如何编程线性关系。但首先我要说清楚我认为你在做什么。
You want the rate of change in opacity to be a linear function of speed:
您希望不透明度的变化率是速度的线性函数:
o(v) = o1*N1(v) + o2*N2(v) so that 0 <= v <=1 and o(v1) = o1 and o(v2) = o2.
o(v)= o1 * N1(v)+ o2 * N2(v)使得0 <= v <= 1且o(v1)= o1且o(v2)= o2。
If we choose N1(v) to equal 1-v and N2(v) = v we end up with what you want:
如果我们选择N1(v)等于1-v和N2(v)= v,我们最终得到你想要的:
o(v) = o1*(1-v) + o2*v
o(v)= o1 *(1-v)+ o2 * v
So, plugging in your values:
所以,插入你的价值观:
v = (u-1)/(10-1) = (u-1)/9
v =(u-1)/(10-1)=(u-1)/ 9
o1 = 0.005 and o2 = 0.1
o1 = 0.005,o2 = 0.1
So the function should look like this:
所以函数应该如下所示:
- o(u) = 0.005*{1-(u-1)/9} + 0.1*(u-1)/9
- o(u) = 0.005*{(9-u+1)/9} + 0.1*(u-1)/9
- o(u) = 0.005*{(10-u)/9} + 0.1(u-1)/9
o(u)= 0.005 * {1-(u-1)/ 9} + 0.1 *(u-1)/ 9
o(u)= 0.005 * {(9-u + 1)/ 9} + 0.1 *(u-1)/ 9
o(u)= 0.005 * {(10-u)/ 9} + 0.1(u-1)/ 9
You can simplify this until you get a simple formula for o(u) where 1 <= u <= 10. Should work fine.
您可以简化这一过程,直到得到o(u)的简单公式,其中1 <= u <= 10.应该可以正常工作。
#5
If I understand what you're after, you want the equation of a line which passes through these two points in the plane: (1, 0.005) and (10, 0.1). The general equation for such a line (as long as it is not vertical) is y = ax+b. Plug the two points into this equation and solve the resulting set of two linear equations to get
如果我理解你所追求的是什么,你需要通过平面中这两个点的直线方程:(1,0.005)和(10,0.1)。这种线的一般方程(只要它不是垂直的)是y = ax + b。将两个点插入此等式并求解得到的两个线性方程组
a = (0.1 - 0.005) / 9 = 0.010555555555...
b = 0.005 - a = -0.00555555555...
Then, for each integer x = 1, 2, 3, ..., 10, plug x into y = ax+b to compute y, the value you want.
然后,对于每个整数x = 1,2,3,...,10,将x插入y = ax + b以计算y,即所需的值。
#1
I think what you want is:
我想你想要的是:
0.005 + ((0.1-0.005)/9)*(X-1)
for X ranging from 1-10
X的范围从1-10
This gives a linear scale corresponding to 0.005 when X = 1 and 0.1 when X = 10
当X = 1时,这给出对应于0.005的线性标度,当X = 10时给出对应于0.1的线性标度
After the comments below, I'm also including my answer fit for a geometric series instead of a linear scale.
在下面的评论之后,我还包括我的答案适合几何系列而不是线性比例。
0.005 * (20^((X-1)/9)))
Results in a geometric variation corresponding to 0.005 when X = 1 and 0.1 when X = 10
当X = 1时,产生对应于0.005的几何变化,当X = 10时,得到0.1
After much more discussion, as seen in the comments below, the updates are as follows.
经过更多讨论后,如下面的评论所示,更新如下。
@Nazgulled found the following relation between my geometric series and the manual values he actually needed to ensure smooth fade animation.
@Nazgulled在我的几何系列和他确实需要的手动值之间找到了以下关系,以确保平滑的淡入淡出动画。
The relationship was as follows: I ♥ Graphs! :) http://i41.tinypic.com/9v8lqg.jpg
关系如下:我♥图! :) http://i41.tinypic.com/9v8lqg.jpg
Which means a geometric/exponential series is the way to go.
这意味着几何/指数系列是要走的路。
After my hours of trying to come up with the appropriate curve fitting to the right hand side graph and derive a proper equation, @Nazgulled informed me that Wolfram|Alpha does that. Seriously amazing. :)
经过几个小时的努力,想出适当的曲线拟合到右侧图表并得出一个合适的方程式,@ Nazgulled告诉我,Wolfram | Alpha就是这么做的。真是太神奇了。 :)
He should have what he wants now, barring very high error from the equation above.
他应该拥有他现在想要的东西,除非上面的等式有很高的误差。
#2
Your problem stems from the fact that the human eye is not linear in its response; to be precise, the eye does not register the difference between a luminosity of 0.05 to 0.10 to be the same as the luminosity difference between 0.80 and 0.85. The whole topic is complicated; you may want to search for the phrase "gamma correction" for some additional information. In general, you'll probably want to find an equation which effectively "gamma corrects" for human ocular response, and use that as your fading function.
你的问题源于人眼的反应不是线性的;准确地说,眼睛没有记录0.05到0.10之间的亮度与0.80和0.85之间的亮度差异相同。整个主题很复杂;您可能想要搜索短语“伽马校正”以获取一些其他信息。一般来说,您可能希望找到一个有效“伽马校正”人类眼睛反应的方程式,并将其用作您的衰落函数。
#3
It's not really an answer, but I'll just point out that everyone who's posted so far, including the original question, are all posting the same equation. So with four independent derivations, maybe we should assume that the equation was probably correct.
这不是一个真正的答案,但我只想指出,到目前为止发布的每个人,包括原始问题,都发布了相同的等式。所以有了四个独立的推导,也许我们应该假设方程可能是正确的。
I did the algebra, but here's the code to verify (in Python, btw, with offsets added to separate the curves:
我做了代数,但这里是验证的代码(在Python中,顺便说一句,添加了偏移量来分隔曲线:
from pylab import *
X = arange(1, 10, .1)
opSpeed0 = (((0.1 - 0.005) * (10 - X)) / (1 - 10)) + 0.1 # original
opSpeed1 = 0.005 + ((0.1-0.005)/9)*(X-1) # Suvesh
opSpeed2 = 0.005*((10-X)/9.) + 0.1*(X-1)/9. # duffymo
a = (0.1 - 0.005) / 9 #= 0.010555555555... # Roger
b = 0.005 - a #= -0.00555555555...
opSpeed3 = a*X+b
nonlinear01 = 0.005*2**((2*(-1 + X))/9.)*5**((-1 + X)/9.)
plot(X, opSpeed0)
plot(X, opSpeed1+.001)
plot(X, opSpeed2+.002)
plot(X, opSpeed3+.003)
plot(X, nonlinear01)
show()
Also, at Nazgulled's request, I've included the non-linear curve suggested by Suvesh (which also, btw, looks quite alot like a gamma correction curve, as suggested by McWafflestix). The Suvesh's nonlinear equation is in the code as nonlinear01
.
另外,在Nazgulled的要求下,我已经包含了Suvesh提出的非线性曲线(顺便说一句,顺便说一下,看起来很像伽马校正曲线,正如McWafflestix所建议的那样)。 Suvesh的非线性方程在代码中是非线性的01。
alt text http://i41.tinypic.com/2extx0k.png
替代文字http://i41.tinypic.com/2extx0k.png
#4
Here's how I'd program that linear relationship. But first I'd like to make clear what I think you're doing.
这是我如何编程线性关系。但首先我要说清楚我认为你在做什么。
You want the rate of change in opacity to be a linear function of speed:
您希望不透明度的变化率是速度的线性函数:
o(v) = o1*N1(v) + o2*N2(v) so that 0 <= v <=1 and o(v1) = o1 and o(v2) = o2.
o(v)= o1 * N1(v)+ o2 * N2(v)使得0 <= v <= 1且o(v1)= o1且o(v2)= o2。
If we choose N1(v) to equal 1-v and N2(v) = v we end up with what you want:
如果我们选择N1(v)等于1-v和N2(v)= v,我们最终得到你想要的:
o(v) = o1*(1-v) + o2*v
o(v)= o1 *(1-v)+ o2 * v
So, plugging in your values:
所以,插入你的价值观:
v = (u-1)/(10-1) = (u-1)/9
v =(u-1)/(10-1)=(u-1)/ 9
o1 = 0.005 and o2 = 0.1
o1 = 0.005,o2 = 0.1
So the function should look like this:
所以函数应该如下所示:
- o(u) = 0.005*{1-(u-1)/9} + 0.1*(u-1)/9
- o(u) = 0.005*{(9-u+1)/9} + 0.1*(u-1)/9
- o(u) = 0.005*{(10-u)/9} + 0.1(u-1)/9
o(u)= 0.005 * {1-(u-1)/ 9} + 0.1 *(u-1)/ 9
o(u)= 0.005 * {(9-u + 1)/ 9} + 0.1 *(u-1)/ 9
o(u)= 0.005 * {(10-u)/ 9} + 0.1(u-1)/ 9
You can simplify this until you get a simple formula for o(u) where 1 <= u <= 10. Should work fine.
您可以简化这一过程,直到得到o(u)的简单公式,其中1 <= u <= 10.应该可以正常工作。
#5
If I understand what you're after, you want the equation of a line which passes through these two points in the plane: (1, 0.005) and (10, 0.1). The general equation for such a line (as long as it is not vertical) is y = ax+b. Plug the two points into this equation and solve the resulting set of two linear equations to get
如果我理解你所追求的是什么,你需要通过平面中这两个点的直线方程:(1,0.005)和(10,0.1)。这种线的一般方程(只要它不是垂直的)是y = ax + b。将两个点插入此等式并求解得到的两个线性方程组
a = (0.1 - 0.005) / 9 = 0.010555555555...
b = 0.005 - a = -0.00555555555...
Then, for each integer x = 1, 2, 3, ..., 10, plug x into y = ax+b to compute y, the value you want.
然后,对于每个整数x = 1,2,3,...,10,将x插入y = ax + b以计算y,即所需的值。