//Calculate the color at a given point on a gradient between two color
double resultRed = color1.red + percent * (color2.red - color1.red); double resultGreen = color1.green + percent * (color2.green - color1.green); double resultBlue = color1.blue + percent * (color2.blue - color1.blue);
/**
* Generate gradient color from 2 or 3 colors using JavaScript * You may use this function with both 2 or 3 interval colors for your gradient. * For example, you want to have a gradient between Bootstrap's danger-warning-success colors. */ function colorGradient(fadeFraction, rgbColor1, rgbColor2, rgbColor3) { var color1 = rgbColor1; var color2 = rgbColor2; var fade = fadeFraction; // Do we have 3 colors for the gradient? Need to adjust the params. if (rgbColor3) { fade = fade * 2; // Find which interval to use and adjust the fade percentage if (fade >= 1) { fade -= 1; color1 = rgbColor2; color2 = rgbColor3; } } var diffRed = color2.red - color1.red; var diffGreen = color2.green - color1.green; var diffBlue = color2.blue - color1.blue; var gradient = { red: parseInt(Math.floor(color1.red + (diffRed * fade)), 10), green: parseInt(Math.floor(color1.green + (diffGreen * fade)), 10), blue: parseInt(Math.floor(color1.blue + (diffBlue * fade)), 10), }; return 'rgb(' + gradient.red + ',' + gradient.green + ',' + gradient.blue + ')'; } //https://gist.github.com/gskema/2f56dc2e087894ffc756c11e6de1b5ed