C ++颜色之间褪色? (Arduino的) [英] c++ Fade between colors? (Arduino)

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

问题描述

目前,我有这个多数民众赞成2组颜色之间的衰落:

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

随衰落code:

with the fading code:

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之间的值,并且code是在一个更新循环

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

推荐答案

这是不可能褪色到粉红色怎么一回事,因为你是从红色开始和结束绿色

It's impossible to fade to pink beacuse you are starting from red and ending with green.

要避免这种错误,我建议你写一个面向对象code。

To avoid this kind of mistake I suggest you to write an object oriented code.

如果你不想写的类来处理三维vectonr您可以使用 Arduino的补锅匠库

If you don't want to write the classes to handle a 3D vectonr you can use the Arduino Tinker Library

我写这个例子给你:

#include <Vect3d.h>
#include <SerialLog.h>

Tinker::Vect3d<float> red(255,0,0);
Tinker::Vect3d<float> green(0,255,0);
Tinker::SerialLog serialLog;

void setup(){
  Serial.begin(9600);
  serialLog.display("Fade color example");  
  serialLog.endline();
}

void loop(){
  //fade factor computation
  const uint32_t t   = millis()%10000;
  const float cosArg = t/10000.*3.1415*2;
  const float fade   = abs(cos(cosArg));

  //Here's the color computation... as you can see is very easy to do!! :)
  Tinker::Vect3d<uint8_t> finalColor(red*fade + green*(1-fade));

  //We print the vect3d on the arduino serial port
  Tinker::LOG::displayVect3d(finalColor,&serialLog);
  serialLog.endline();
  delay(500);
}

打印出串口以下输出

Which prints the following output on the serial port

Fade color example
V[255;0;0]
V[242;12;0]
V[206;48;0]
V[149;105;0]
V[78;176;0]
V[0;254;0]
V[79;175;0]
V[150;104;0]
V[206;48;0]
V[242;12;0]
V[254;0;0]
V[242;12;0]
V[205;49;0]
V[148;106;0]
V[77;177;0]
V[1;253;0]
V[80;174;0]
V[151;103;0]

希望这有助于:)

hope that this helps :)

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

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