如何将两种颜色不同的百分比两种、百分比、如何将、颜色

2023-09-08 00:28:04 作者:①错再错ベ错到底

我有两个颜色和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%颜色1 + 10%COLOR2 第二个1:80%颜色1 + 20%COLOR2 ...... 第二个1:10%颜色1 + 90%COLOR2 第二个1:0%颜色1 + 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;
}

不过,这是一个糟糕的idea.the结果是令人失望的。 是有这个目标的任何解决方案。 谢谢。

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.

您一般的做法似乎是正确的,也许你只是缺少一些细节,所以我将它改写在不同的方面。让颜色1 COLOR2 是三元组(R,G,B),其中每个的R,G,B的范围内[0,1]。如果不是的话,除以255,如果这是在您的情况的限制,稍后再乘以255所设取值是步骤,从转变数颜色1 COLOR2 ,我在这里包括取值的与颜色1 初步框架,但不与 COLOR2 最后一帧。在步骤 K ,你有一个值 P ,使得 P =(S - K) / S 。随着 P 你这样做颜色= P *颜色1 +获得帧 K 颜色( 1 - P)* COLOR2 。现在,你可能要乘以颜色 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.

一个伪code这个描述是:

A pseudocode for this description is:

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 你只有颜色1 ,并在 K =小号你只有 COLOR2 。正如你看到的,它类似于您发布更多的细节内容。请注意,我在这里通过 P 乘以每个R,G,B的。

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.

下面是一些例子从黄色过渡到一些蓝色,步骤= 10,25,500 分别。

Here are some examples transitioning from a yellow to some blue color, steps = 10, 25, 500 respectively.