Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate a color fade

Tags:

colors

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!

like image 251
TT. Avatar asked Nov 26 '08 02:11

TT.


People also ask

How do you fade the color in RGB?

If you want to fade between colours, work in a colourspace which makes it easy and then convert back to RGB at the end. For example, work in HSL colour space, keep S and L constant (say a fully saturated and bright colour) and then "fade" H around the circle - you'll go from red through green, blue and back to red.

What is color fade?

color fade (plural color fades) a film punctuation in which the picture brightens until it is completely a single color.

How are Colours calculated?

Each level is measured by the range of decimal numbers from 0 to 255 (256 levels for each color). For example, if a color has zero Blue, it will be a mixture of Red and Green. This means we can generate 256 x 256 x 256 = 16.777. 216 different colors with this model.

What happens in between Colours?

Davin concurs: “Depending on the mix, in-between colors create a sense of equilibrium for your eye, which puts it at rest. This signals your brain to relax.” And when a brain is relaxed, powerful things happen.


2 Answers

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.

like image 185
nickf Avatar answered Sep 18 '22 17:09

nickf


There are two good related questions you should also review:

  • Generating gradients programatically?
  • Conditional formatting — percentage to color conversion

Please note that you're often better off doing this in the HSV color space rather than RGB - it generates more pleasing colors to the human eye (lower chance of clashing or negative optical properties).

Good luck!

-Adam

like image 30
Adam Davis Avatar answered Sep 18 '22 17:09

Adam Davis