151 lines
4.7 KiB
TypeScript
151 lines
4.7 KiB
TypeScript
import colorMap from './colorMap.json';
|
||
|
||
console.log('=== ColorMap 使用示例 ===\n');
|
||
|
||
// 示例1:查找特定颜色的所有引用
|
||
function findColorByHex(hexColor: string) {
|
||
const key = Object.keys(colorMap).find(key => key.toLowerCase() === hexColor.toLowerCase());
|
||
return key ? colorMap[key] : null;
|
||
}
|
||
|
||
console.log('示例1: 查找白色 (#FFFFFF)');
|
||
const whiteColor = findColorByHex('#FFFFFF');
|
||
if (whiteColor) {
|
||
console.log(`找到 ${whiteColor.length} 个引用:`);
|
||
whiteColor.slice(0, 5).forEach((item, index) => {
|
||
console.log(` ${index + 1}. 品牌: ${item['color-title']}, 色号: ${item['color-name']}`);
|
||
});
|
||
if (whiteColor.length > 5) {
|
||
console.log(` ... 还有 ${whiteColor.length - 5} 个引用`);
|
||
}
|
||
}
|
||
console.log('');
|
||
|
||
// 示例2:统计每个颜色的引用次数
|
||
console.log('示例2: 按引用数排序(前10个颜色)');
|
||
const colorStats = Object.entries(colorMap)
|
||
.map(([color, items]) => ({ color, count: items.length }))
|
||
.sort((a, b) => b.count - a.count)
|
||
.slice(0, 10);
|
||
|
||
colorStats.forEach((item, index) => {
|
||
console.log(`${index + 1}. ${item.color}: ${item.count} 次引用`);
|
||
});
|
||
console.log('');
|
||
|
||
// 示例3:查找特定品牌的颜色
|
||
function findColorsByBrand(brandName: string) {
|
||
const results: Array<{color: string, colorName: string}> = [];
|
||
|
||
Object.entries(colorMap).forEach(([hexColor, items]) => {
|
||
items.forEach(item => {
|
||
if (item['color-title'] === brandName) {
|
||
results.push({
|
||
color: hexColor,
|
||
colorName: item['color-name']
|
||
});
|
||
}
|
||
});
|
||
});
|
||
|
||
return results;
|
||
}
|
||
|
||
console.log('示例3: 查找 COCO-291 品牌的颜色');
|
||
const cocoColors = findColorsByBrand('COCO-291');
|
||
console.log(`找到 ${cocoColors.length} 个颜色`);
|
||
console.log('前5个颜色:');
|
||
cocoColors.slice(0, 5).forEach((item, index) => {
|
||
console.log(` ${index + 1}. ${item.colorName}: ${item.color}`);
|
||
});
|
||
console.log('');
|
||
|
||
// 示例4:查找跨品牌重复的颜色
|
||
function findSharedColors(minBrandCount: number = 2) {
|
||
const sharedColors: Array<{color: string, brands: string[]}> = [];
|
||
|
||
Object.entries(colorMap).forEach(([hexColor, items]) => {
|
||
const uniqueBrands = [...new Set(items.map(item => item['color-title']))];
|
||
if (uniqueBrands.length >= minBrandCount) {
|
||
sharedColors.push({
|
||
color: hexColor,
|
||
brands: uniqueBrands
|
||
});
|
||
}
|
||
});
|
||
|
||
return sharedColors.sort((a, b) => b.brands.length - a.brands.length);
|
||
}
|
||
|
||
console.log('示例4: 查找被多个品牌共享的颜色(至少5个品牌)');
|
||
const sharedColors = findSharedColors(5);
|
||
console.log(`找到 ${sharedColors.length} 个共享颜色`);
|
||
console.log('前10个:');
|
||
sharedColors.slice(0, 10).forEach((item, index) => {
|
||
console.log(` ${index + 1}. ${item.color}: ${item.brands.length} 个品牌`);
|
||
console.log(` 品牌: ${item.brands.join(', ')}`);
|
||
});
|
||
console.log('');
|
||
|
||
// 示例5:统计品牌分布
|
||
function countBrandDistribution() {
|
||
const brandCounts: { [brand: string]: number } = {};
|
||
|
||
Object.values(colorMap).forEach(items => {
|
||
items.forEach(item => {
|
||
const brand = item['color-title'];
|
||
brandCounts[brand] = (brandCounts[brand] || 0) + 1;
|
||
});
|
||
});
|
||
|
||
return Object.entries(brandCounts)
|
||
.sort((a, b) => b[1] - a[1]);
|
||
}
|
||
|
||
console.log('示例5: 品牌颜色统计');
|
||
const brandDist = countBrandDistribution();
|
||
console.log('各品牌颜色数量:');
|
||
brandDist.forEach(([brand, count]) => {
|
||
console.log(` ${brand}: ${count} 个颜色`);
|
||
});
|
||
console.log('');
|
||
|
||
// 示例6:查找特定色号
|
||
function findColorByName(colorName: string) {
|
||
const results: Array<{brand: string, color: string}> = [];
|
||
|
||
Object.entries(colorMap).forEach(([hexColor, items]) => {
|
||
items.forEach(item => {
|
||
if (item['color-name'] === colorName) {
|
||
results.push({
|
||
brand: item['color-title'],
|
||
color: hexColor
|
||
});
|
||
}
|
||
});
|
||
});
|
||
|
||
return results;
|
||
}
|
||
|
||
console.log('示例6: 查找色号 "A01"');
|
||
const a01Colors = findColorByName('A01');
|
||
console.log(`找到 ${a01Colors.length} 个品牌包含色号 A01:`);
|
||
a01Colors.forEach((item, index) => {
|
||
console.log(` ${index + 1}. ${item.brand}: ${item.color}`);
|
||
});
|
||
console.log('');
|
||
|
||
// 示例7:颜色统计信息
|
||
console.log('示例7: 整体统计信息');
|
||
const totalColorReferences = Object.values(colorMap).reduce((sum, items) => sum + items.length, 0);
|
||
const uniqueColors = Object.keys(colorMap).length;
|
||
const avgReferences = (totalColorReferences / uniqueColors).toFixed(2);
|
||
|
||
console.log(` 总颜色引用次数: ${totalColorReferences}`);
|
||
console.log(` 唯一颜色数量: ${uniqueColors}`);
|
||
console.log(` 平均每个颜色被引用: ${avgReferences} 次`);
|
||
console.log('');
|
||
|
||
console.log('=== 示例运行完成 ===');
|