Skip to content

Instantly share code, notes, and snippets.

@jkohlin
Last active April 16, 2023 17:37
Show Gist options
  • Save jkohlin/1cd9d5b94bcae174b5b1929f3f41790a to your computer and use it in GitHub Desktop.
Save jkohlin/1cd9d5b94bcae174b5b1929f3f41790a to your computer and use it in GitHub Desktop.
svgColorBoxes
function svgColorSwatches(colors, size = 64, vertical = false) {
let svg = vertical
? `<svg width="${size}" height="${colors.length * size}" viewBox="0 0 ${size} ${colors.length * size}" fill="none" xmlns="http://www.w3.org/2000/svg">`
: `<svg width="${colors.length * size}" height="${size}" viewBox="0 0 ${colors.length * size} ${size}" fill="none" xmlns="http://www.w3.org/2000/svg">`;
let x = 0, y = 0;
for (let shade of colors) {
svg += `
<rect width="${size}" height="${size}" fill="${shade}" x="${x}" y="${y}"/>`;
x += vertical ? 0 : size;
y += vertical ? size : 0;
}
svg += `
</svg>`;
return svg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment