c++ Fade between colors? (Arduino)

前端 未结 3 1953
醉酒成梦
醉酒成梦 2021-01-28 01:11

I currently have this thats fading between 2 set colours:

for(int i=0;i

        
相关标签:
3条回答
  • 2021-01-28 01:56

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

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

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

    I wrote this example for you:

    #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 :)

    0 讨论(0)
  • 2021-01-28 01:57

    Smooth transitions between colours is best done in a different colour space (IMHO).

    As an example, to transition from bright red to bright green, do you want to go via bright yellow (around the edge of the colour wheel) or via #808000 (murky yellow) - which is what a straight line interpolation would give you in the RGB domain.

    Having done this for my Moodlamp app, I used the HSL colour space. I specified a start colour and end colour, along with a number of steps for the transition to take. That enabled me to calculate how much to adjust H, S and L by at each point in the transition.

    Only at the point of using the colour did I convert back to RGB.

    You can see the javascript code here (please bear in mind it's the first Javascript I ever wrote, so if it seems non-idiomatic, that's probably why!):

    https://github.com/martinjthompson/MoodLamp/blob/master/app/assistants/lamp-assistant.js

    0 讨论(0)
  • 2021-01-28 02:02
    uint8_t clrR = abs(255 * cos(<some var that changes in time>));
    

    same for clrB & clrG

    0 讨论(0)
提交回复
热议问题