import colors from './get-colors.json'; // 示例1:显示所有颜色集合的统计信息 console.log('=== 颜色集合统计 ===\n'); const stats = Object.entries(colors).map(([setName, colorList]) => ({ name: setName, count: colorList.length })); // 按颜色数量排序 stats.sort((a, b) => b.count - a.count); console.log('颜色集合列表(按数量排序):'); stats.slice(0, 10).forEach(stat => { console.log(` ${stat.name}: ${stat.count} 色`); }); console.log(` ... (共 ${stats.length} 个集合)\n`); // 总计 const totalColors = stats.reduce((sum, stat) => sum + stat.count, 0); console.log(`总计: ${totalColors} 个颜色\n`); // 示例2:查找特定颜色 console.log('=== 查找颜色示例 ===\n'); function findColorByHex(hexColor: string): Array<{brand: string, colorName: string}> { const results: Array<{brand: string, colorName: string}> = []; Object.entries(colors).forEach(([setName, colorList]) => { const found = colorList.find(c => c.color.toLowerCase() === hexColor.toLowerCase()); if (found) { results.push({ brand: setName, colorName: found['color-name'] }); } }); return results; } const white = findColorByHex('#FFFFFF'); console.log(`白色 (#FFFFFF) 在以下集合中出现:`); white.forEach(item => { console.log(` - ${item.brand}: ${item.colorName}`); }); console.log(''); const black = findColorByHex('#000000'); console.log(`黑色 (#000000) 在以下集合中出现:`); black.forEach(item => { console.log(` - ${item.brand}: ${item.colorName}`); }); console.log(''); // 示例3:获取特定品牌的颜色 console.log('=== 特定品牌颜色示例 ===\n'); const dmcColors = colors['DMC十字绣实色 - 色号预览(508色)']; console.log(`DMC十字绣实色共有 ${dmcColors.length} 个颜色`); console.log('前10个颜色:'); dmcColors.slice(0, 10).forEach((color, index) => { console.log(` ${index + 1}. ${color['color-name']}: ${color.color}`); }); console.log(''); // 示例4:颜色去重和统计 console.log('=== 颜色去重统计 ===\n'); function getAllUniqueColors() { const uniqueColors = new Map(); Object.values(colors).forEach(colorList => { colorList.forEach(color => { const hex = color.color; uniqueColors.set(hex, (uniqueColors.get(hex) || 0) + 1); }); }); return uniqueColors; } const uniqueColors = getAllUniqueColors(); console.log(`唯一颜色数量: ${uniqueColors.size}`); // 找出最常见的颜色(在多个品牌中出现的) const commonColors = Array.from(uniqueColors.entries()) .filter(([hex, count]) => count > 5) .sort((a, b) => b[1] - a[1]) .slice(0, 10); console.log('\n最常见的颜色(出现在6个以上品牌中):'); commonColors.forEach(([hex, count]) => { console.log(` ${hex}: 出现在 ${count} 个品牌中`); }); console.log(''); // 示例5:颜色搜索功能 console.log('=== 颜色搜索功能 ===\n'); function searchColors(keyword: string, limit: number = 5): Array<{brand: string, color: any}> { const results: Array<{brand: string, color: any}> = []; Object.entries(colors).forEach(([setName, colorList]) => { const matches = colorList.filter(c => c['color-name'].toLowerCase().includes(keyword.toLowerCase()) ); matches.forEach(match => { if (results.length < limit) { results.push({ brand: setName, color: match }); } }); }); return results; } const searchA = searchColors('A1', 10); console.log('搜索包含 "A1" 的色号(前10个):'); searchA.forEach(item => { console.log(` ${item.brand}: ${item.color['color-name']} = ${item.color.color}`); }); console.log(''); // 示例6:颜色分组统计(按颜色范围) console.log('=== 颜色范围统计 ===\n'); function getColorRange(hex: string): string { const r = parseInt(hex.slice(1, 3), 16); const g = parseInt(hex.slice(3, 5), 16); const b = parseInt(hex.slice(5, 7), 16); const max = Math.max(r, g, b); const min = Math.min(r, g, b); if (max - min < 30) { if (max < 50) return '黑色系'; if (max > 200) return '白色系'; return '灰色系'; } if (r > g && r > b) return '红色系'; if (g > r && g > b) return '绿色系'; if (b > r && b > g) return '蓝色系'; if (r > 200 && g > 200 && b < 100) return '黄色系'; if (r > 200 && b > 200 && g < 100) return '紫色系'; if (g > 200 && b > 200 && r < 100) return '青色系'; return '其他'; } const rangeStats = new Map(); Object.values(colors).forEach(colorList => { colorList.forEach(color => { const range = getColorRange(color.color); rangeStats.set(range, (rangeStats.get(range) || 0) + 1); }); }); console.log('颜色范围分布:'); Array.from(rangeStats.entries()) .sort((a, b) => b[1] - a[1]) .forEach(([range, count]) => { console.log(` ${range}: ${count} 个`); }); console.log(''); console.log('=== 示例运行完成 ===');