简体   繁体   English

脉动/闪烁RGBA颜色?

[英]Pulsating / Flashing RGBA Color?

So in my program colors are defined like this for example 所以在我的程序中,例如,颜色是这样定义的

//R, G, B, A
float red[4] = { 1, 0, 0, 1 };

float green[4] = { 0, 1, 0, 0.5 };//green[3] = 0.5; this mean opacity is at 50%

float blue[4] = { 0, 0, 1, 0.2 };blue[3] = 0.2; this mean opacity is at 20%

My question is, how could I make a color smoothly pulsate/flash? 我的问题是,如何使颜色平滑地脉动/闪烁? I am VERY confused with the math involved in accomplishing what I want. 我对完成自己想要的数学很困惑。

For example, if I wanted to fade a color in and out I would do this 例如,如果我想淡入和淡出颜色,则可以这样做

float color[4] = { 1, 0, 0, 1 };
void fadeColorInAndOut(float * Colorin)
{
    for(float i = 1; i > 0; i-= 0.01f)
    {
        Colorin[3] = i;
        sleep(10);//10 milliseconds
    }
    for(float i = 0; i < 1; i+= 0.01f)
    {
        Colorin[3] = i;
        sleep(10);//10 milliseconds
    }
}

But when it comes to actually Pulsating / Flashing the colors I really wouldn't even know where to start. 但是当涉及到脉动/闪烁颜色时,我什至不知道从哪里开始。

To hopefully help you understand more, this is the kind of effect I am looking for. 为了希望能帮助您了解更多,这就是我想要的那种效果。 Here is a GIF I found which pretty much perfectly demonstrates my desired effect 这是我发现的GIF,几乎可以完美展示我想要的效果

在此处输入图片说明

there are more ways how to do this: 有更多方法可以做到这一点:

  1. interpolate between random colors 在随机颜色之间插值
  2. interpolate between predefined table of colors 在预定义的颜色表之间插值
  3. use continuous function 使用连续功能

Here simple C++ example of #3: 这里是#3的简单C ++示例:

void iterate(float *col,float t)
 {
 col[0]=cos(0.5*t); // the coefficients gives you frequency of change
 col[1]=cos(0.3*t); // different numbers between bands will give you colors
 col[2]=cos(0.1*t);
 }

void main()
 {
 float t=0.0;
 float col[4]={0.0,0.0,0.0,1.0};
 for (;;)
  {
  iterate(col,t);
  // here render your colored stuff
  Sleep(10); t+=0.01; // speed of pulsation [rad/10ms]
  // here test end of program condition and break if needed...
  }
 }

All the approaches are just computing function value in time (based on random value, table of points, math function). 所有方法都只是及时计算函数值(基于随机值,点表,数学函数)。 Handle each color band as separate function And step time after each iteration step. 将每个色带作为单独的函数进行处理,并在每个迭代步骤之后选择步骤时间。

If you know the pattern of colors you want to achieve then create a table of them and interpolate between them. 如果知道要实现的颜色模式,则创建一个颜色表并在它们之间进行插值。 Here few links which can help you: 这里有一些链接可以帮助您:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM