|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +const generateDistinctColor = (index) => { |
| 5 | + const goldenRatio = 0.618033988749895; |
| 6 | + |
| 7 | + const hue = (index * goldenRatio * 360) % 360; |
| 8 | + |
| 9 | + const saturation = 85 + Math.random() * 15; |
| 10 | + const lightness = 45 + Math.random() * 20; |
| 11 | + |
| 12 | + return `hsl(${Math.round(hue)}, ${Math.round(saturation)}%, ${Math.round(lightness)}%)`; |
| 13 | +}; |
| 14 | + |
| 15 | +const createSvgContent = (color) => { |
| 16 | + return `<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" fill="${color}" r="30"/></svg>`; |
| 17 | +}; |
| 18 | + |
| 19 | +export const generateIndicators = (count) => { |
| 20 | + const outputDir = path.join("src", "img", "indicators"); |
| 21 | + |
| 22 | + // Check if directory exists and has files |
| 23 | + if (fs.existsSync(outputDir)) { |
| 24 | + const existingFiles = fs.readdirSync(outputDir).filter((file) => file.endsWith(".svg")); |
| 25 | + if (existingFiles.length) { |
| 26 | + return; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + fs.mkdirSync(outputDir, { recursive: true }); |
| 31 | + |
| 32 | + const colors = new Set(); |
| 33 | + let index = 0; |
| 34 | + |
| 35 | + while (colors.size < count) { |
| 36 | + colors.add(generateDistinctColor(index)); |
| 37 | + index++; |
| 38 | + } |
| 39 | + |
| 40 | + colors.forEach((color) => { |
| 41 | + const safeFileName = color |
| 42 | + .replace("hsl(", "") |
| 43 | + .replace(")", "") |
| 44 | + .replace(/, /g, "-") |
| 45 | + .replace(/%/g, ""); |
| 46 | + const fileName = path.join(outputDir, `${safeFileName}.svg`); |
| 47 | + fs.writeFileSync(fileName, createSvgContent(color)); |
| 48 | + }); |
| 49 | +}; |
0 commit comments