0

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

enter image description here

coddding
  • 333
  • 1
  • 6
  • 15
  • What programming language is your code in? Is it C++? I suggest you to tag your question with the language in use. To update your question, click on the **"[edit]"** link under the post. Thank you. – Pang Mar 05 '17 at 10:18

1 Answers1

1

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:

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:

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380