如何混合两种颜色与不同百分比 [英] how to mix two color with different percent
问题描述
我有两个颜色和一个View组件。颜色一是我的组件的背景。我会改变我的背景颜色为颜色两。但不突然。改变类似动画。例如:
i have two color and a View component. color one is background of my component. i will change my background Color to color two. but not suddenly. change similar a animation. for example:
秒1:90%color1 + 10%color2
秒1:80%color1 + 20%color2
..
second 1:10%color1 + 90%color2
second 1:0%color1 + 100%color2
second 1 : 90% color1 + 10% color2 second 1 : 80% color1 + 20% color2 ...... second 1 : 10% color1 + 90% color2 second 1 : 0% color1 + 100% color2
:
percent=100;
while (percent>=0) {
color = (color1*precent)+(color2*(100-percent));
percent-=10;
}
但这是一个糟糕的想法,结果令人失望。
有这个目标的任何解决方案。
感谢。
but this is a bad idea.the result is disappointing. is there any solution for this target. thanks.
推荐答案
您没有清楚说明为什么结果令人失望,所以我假设它你得到的颜色转换不是你期望的。
You didn't clearly say why the result is disappointing, so I'm assuming it means the color transition you get is not as good you expected it to be.
你的一般方法似乎是正确的,也许你只是缺少一些细节,所以我将重写它在不同术语。让 color1
和 color2
为三元组(R,G,B),其中R,G, [0,1]。如果不是这种情况,除以255,如果这是你的情况的限制,然后再乘以255.让 s
是从 color1
到 color2
,这里我包括 s
初始框架 color1
,但不是最后一个框架 color2
。在步骤 k
,您有一个值 p
,使得 p =(s - k) / s
。使用 p
,您可以通过执行 color = p * color1 +()获得
。现在您可能需要将 k
1-p)* color2 color
乘以255.
Your general approach seems right, maybe you are just missing some detail so I will rewrite it in different terms. Let color1
and color2
be triples (R, G, B) where each of R, G, B is in range [0, 1]. If that is not the case, divide by 255 if that is the limit in your situation, and later multiply again by 255. Let s
be the number of steps to transition from color1
to color2
, here I'm including in s
the initial frame with color1
but not the final frame with color2
. At step k
, you have a value p
such that p = (s - k)/s
. With p
you obtain the color in frame k
by doing color = p * color1 + (1 - p) * color2
. Now you may want to multiply color
by 255.
此说明的伪代码为:
color1 = (R1, G1, B1)
color2 = (R2, G2, B2)
s = N
for k = 0 to s: # s + 1 steps, according to the description
p = (s - k) / s
color = (p * color1) + ((1 - p) * color2)
请注意,在 k = 0
你只有 color1
和 k = s
你只得到 color2
。正如你所看到的,它类似于你张贴的更多细节。注意,这里我将每个R,G,B乘以 p
。
Note that at k = 0
you have only color1
, and at k = s
you get only color2
. As you see, it is similar to what you posted with more details. Note that here I'm multiplying each of R, G, B by p
.
从黄色到一些蓝色,分别 steps = 10,25,500
Here are some examples transitioning from a yellow to some blue color, steps = 10, 25, 500
respectively.
这篇关于如何混合两种颜色与不同百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!