Search code examples
colorsfade

Calculate a color fade


Given two colors and n steps, how can one calculate n colors including the two given colors that create a fade effect?

If possible pseudo-code is preferred but this will probably be implemented in Java.

Thanks!


Solution

  • Divide each colour into its RGB components and then calculate the individual steps required.

    oldRed = 120;
    newRed = 200;
    steps = 10;
    redStepAmount = (newRed - oldRed) / steps;
    
    currentRed = oldRed;
    for (i = 0; i < steps; i++) {
       currentRed += redStepAmount;
    }
    

    Obviously extend that for green and blue.