c ++ 颜色之间的淡入淡出?(阿杜诺) [英] c++ Fade between colors? (Arduino)

查看:27
本文介绍了c ++ 颜色之间的淡入淡出?(阿杜诺)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有这两种颜色之间的渐变:

I currently have this thats fading between 2 set colours:

for(int i=0;i<nLEDs;i++){
    a = (255 / 100) * (incomingByte * sensitivity);
    r = (r * 7 + a + 7) / 8;
    g = (g * 7 + (255 - a) + 7) / 8;
    b = 0;

    FTLEDColour col = { r , g , b };
    led.setLED(i, col);
  }

但现在我试图允许用户输入他们自己的颜色:

But now im trying to allow users to enter their own colours:

// > Colour fade, Start colour

int colFade1Red = 0;
int colFade1Green = 255;
int colFade1Blue = 0;

// > Colour fade, End colour

int colFade2Red = 255;
int colFade2Green = 0;
int colFade2Blue = 0;

int fadeAm = 7; // Fade speed

带有淡入淡出的代码:

void ColourFade(){
  for(int i=0;i<nLEDs;i++){

    r = ctest(colFade1Red, colFade2Red, r);
    g = ctest(colFade1Green, colFade2Green, g);
    b = ctest(colFade1Blue, colFade2Blue, b);

    FTLEDColour col = { r , g , b };
    led.setLED(i, col);
  }
}

int ctest(int col1, int col2, int cur){
   int temp = col1 - col2;
   if(temp < 0) { temp = -temp; } 

   int alp = (temp / 100) * (incomingByte * sensitivity);

   if(col1 < col2){
     return (cur * fadeAm + (col2 - alp) + fadeAm) / (fadeAm +1 );
   } else {
     return (cur * fadeAm + alp + fadeAm) / (fadeAm +1 );
   }
}

但是这从第二个用户颜色开始并逐渐变成粉红色.我如何正确地淡化颜色?

But this starts with the Second user colour and fades into pink. How would I fade colours properly?

还有incomingByte"是0到100之间的值,代码在更新循环中.

Also "incomingByte" is a value between 0 and 100, and the code is in a update loop.

推荐答案

uint8_t clrR = abs(255 * cos(<some var that changes in time>));

同样适用于 clrB &clrG

same for clrB & clrG

这篇关于c ++ 颜色之间的淡入淡出?(阿杜诺)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆