function hex(c) {
var s = "0123456789abcdef";
var i = parseInt(c);
if (i == 0 || isNaN(c))
return "00";
i = Math.round(Math.min(Math.max(0, i), 255));
return s.charAt((i - i % 16) / 16) + s.charAt(i % 16);
}
/* Convert an RGB triplet to a hex string */
function convertToHex(rgb) {
return hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
}
/* Remove '#' in color hex string */
function trim(s) {
return (s.charAt(0) == '#') ? s.substring(1, 7) : s
}
/* Convert a hex string to an RGB triplet */
function convertToRGB(hex) {
var color = [];
color[0] = parseInt((trim(hex)).substring(0, 2), 16);
color[1] = parseInt((trim(hex)).substring(2, 4), 16);
color[2] = parseInt((trim(hex)).substring(4, 6), 16);
return color;
}
function generateColor(colorStart, colorEnd, colorCount) {
// The beginning of your gradient
var start = convertToRGB(colorStart);
// The end of your gradient
var end = convertToRGB(colorEnd);
// The number of colors to compute
var len = colorCount;
//Alpha blending amount
var alpha = 0.0;
var saida = [];
for (i = 0; i < len; i++) {
var c = [];
alpha += (1.0 / len);
c[0] = start[0] * alpha + (1 - alpha) * end[0];
c[1] = start[1] * alpha + (1 - alpha) * end[1];
c[2] = start[2] * alpha + (1 - alpha) * end[2];
saida.push(convertToHex(c));
}
return saida;
}
// Exemplo de como usar
var tmp = generateColor('#3333ff', '#e6e6ff', 110);
for (cor in tmp) {
$('#result_show').append("<div style='padding:8px;color:#FFF;background-color:#" + tmp[cor] + "'>COLOR " + cor + "° - #" + tmp[cor] + "</div>")
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.