//import palette from "./palette.js";




function componentToHex(c) {
    var hex = Math.round(c).toString(16);
    return hex.length == 1 ? "0" + hex : hex;
}

function rgbToHex(r, g, b) {
    return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}


function hexToRgb(hex) {
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}



function generate(colors, count) {
	let cols = new Array(count);
	let len = colors.length;
	let steps = Math.floor(count / len) + 1;
	let s = 0.65 / steps;

	let pals = new Array(len); 
	let idx = 0;
	for (let i = 0; i < len; i++) {
		pals[i] = hexToRgb(colors[i]);
	}

	outer:
	for (let i = 0; i < steps; i++) {
		let ii = (1 - (s * i));
		for (let j = 0; j < len; j++) {
			let p = pals[j];
			cols[idx++] = rgbToHex(p.r * ii, p.g * ii, p.b * ii);

			if (idx >= count) {
				break outer;
			}
		}
	}

	return cols;
}


export default generate;