Map node type to fix color

This commit is contained in:
yangdx
2025-04-08 12:39:22 +08:00
parent 1af97bbead
commit f87dfd2067

View File

@@ -11,112 +11,148 @@ import { useSettingsStore } from '@/stores/settings'
import seedrandom from 'seedrandom' import seedrandom from 'seedrandom'
// Predefined node colors - Primary colors // 同义词列表 - 用于快速查找节点类型
const NODE_COLORS = [ const TYPE_SYNONYMS: Record<string, string> = {
'#fdd868', // Yellow - UNKNOWN // unknown 类型及其同义词
'#e3493b', // Google Red - geo 'unknown': 'unknown',
'#1212a1', // Deep Cyan - weapon '未知': 'unknown',
'#0f705d', // Green - organization 'other': 'unknown',
'#b300b3', // Purple - technology
'#f46a9b', // Magenta
'#bd7ebe', // Light Violet
'#fdcce5', // Pale Pink - category
'#0f558a', // Blue - location
'#b2e061', // Yellow Green
'#00bfa0', // Turquoise - event
'#439bd6', // Cyan - person
'#094338', // Deep Green
'#5a2c6d', // Deep Violet
'#fd7f6f', // Light Brown - location
'#adc2eb', // Sky Blue - animal
];
// Extended colors - Used when node types exceed primary colors // category 类型及其同义词
'category': 'category',
'类别': 'category',
'type': 'category',
'分类': 'category',
// organization 类型及其同义词
'organization': 'organization',
'组织': 'organization',
'org': 'organization',
'company': 'organization',
'公司': 'organization',
'机构': 'organization',
// event 类型及其同义词
'event': 'event',
'事件': 'event',
'activity': 'event',
'活动': 'event',
// person 类型及其同义词
'person': 'person',
'人物': 'person',
'people': 'person',
'human': 'person',
'人': 'person',
// animal 类型及其同义词
'animal': 'animal',
'动物': 'animal',
'creature': 'animal',
'生物': 'animal',
// geo 类型及其同义词
'geo': 'geo',
'地理': 'geo',
'geography': 'geo',
'地域': 'geo',
// location 类型及其同义词
'location': 'location',
'地点': 'location',
'place': 'location',
'address': 'location',
'位置': 'location',
'地址': 'location',
// technology 类型及其同义词
'technology': 'technology',
'技术': 'technology',
'tech': 'technology',
'科技': 'technology',
// equipment 类型及其同义词
'equipment': 'equipment',
'设备': 'equipment',
'device': 'equipment',
'装备': 'equipment',
// weapon 类型及其同义词
'weapon': 'weapon',
'武器': 'weapon',
'arms': 'weapon',
'军火': 'weapon'
};
// 节点类型到颜色的映射
const NODE_TYPE_COLORS: Record<string, string> = {
'unknown': '#f4d371', // Yellow
'category': '#e3493b', // GoogleRed
'organization': '#0f705d', // Green
'event': '#00bfa0', // Turquoise
'person': '#4169E1', // RoyalBlue
'animal': '#84a3e1', // SkyBlue
'geo': '#ff99cc', // Pale Pink
'location': '#cf6d17', // Carrot
'technology': '#b300b3', // Purple
'equipment': '#2F4F4F', // DarkSlateGray
'weapon': '#0f558a', // NavyBlue
};
// Extended colors pool - Used for unknown node types
const EXTENDED_COLORS = [ const EXTENDED_COLORS = [
'#742b25', // Dark Brown '#4421af', // DeepPurple
'#9efacc', // Light Green '#cd071e', // ChinaRed
'#ffbf80', // Light Brown '#5a2c6d', // DeepViolet
'#003366', // Dark Blue '#0000ff', // Blue
'#996600', // Yellow Brown '#00cc00', // Green
'#4421af', // Deep Purple '#9b3a31', // DarkBrown
'#cf6d17', // Carrot '#003366', // DarkBlue
'#ff1a1a', // Pure Red '#00CED1', // DarkTurquoise
'#DEB887', // BurlyWood
'#bd7ebe', // LightViolet
'#b2e061', // YellowGreen
'#6ef7b3', // LightGreen
]; ];
// All available colors combined // Select color based on node type
const ALL_COLORS = [...NODE_COLORS, ...EXTENDED_COLORS];
// Helper function to get color based on node type
const getNodeColorByType = (nodeType: string | undefined): string => { const getNodeColorByType = (nodeType: string | undefined): string => {
const defaultColor = '#5D6D7E'; // Default color for nodes without a type or undefined type
// Return default color if node type is undefined const defaultColor = '#5D6D7E';
if (!nodeType) {
return defaultColor;
}
// Get type color map from store const normalizedType = nodeType ? nodeType.toLowerCase() : 'unknown';
const typeColorMap = useGraphStore.getState().typeColorMap; const typeColorMap = useGraphStore.getState().typeColorMap;
// If this type already has an assigned color, return it // Return previous color if already mapped
if (typeColorMap.has(nodeType)) { if (typeColorMap.has(normalizedType)) {
return typeColorMap.get(nodeType) || defaultColor; return typeColorMap.get(normalizedType) || defaultColor;
} }
// Get all currently used colors const standardType = TYPE_SYNONYMS[normalizedType];
const usedColors = new Set<string>(); if (standardType) {
typeColorMap.forEach(color => { const color = NODE_TYPE_COLORS[standardType];
usedColors.add(color); // Update color mapping
}); const newMap = new Map(typeColorMap);
newMap.set(normalizedType, color);
// Assign color for new node type useGraphStore.setState({ typeColorMap: newMap });
// Use a simple hash function to map node type to color index return color;
const getColorIndex = (str: string): number => {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash) + str.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
// Ensure result is positive and within NODE_COLORS range only
return Math.abs(hash) % NODE_COLORS.length;
};
// Get initial color index from hash
const colorIndex = getColorIndex(nodeType);
let newColor = NODE_COLORS[colorIndex];
// If the color is already used, find the next available color
if (usedColors.has(newColor) && usedColors.size < ALL_COLORS.length) {
// First try to find an unused color in NODE_COLORS
let foundUnused = false;
for (let i = 0; i < NODE_COLORS.length; i++) {
const candidateColor = NODE_COLORS[i];
if (!usedColors.has(candidateColor)) {
newColor = candidateColor;
foundUnused = true;
break;
}
} }
// If all NODE_COLORS are used, then try EXTENDED_COLORS // For unpredefind nodeTypes, use extended colors
if (!foundUnused) { // Find used extended colors
newColor = defaultColor; const usedExtendedColors = new Set(
for (let i = 0; i < EXTENDED_COLORS.length; i++) { Array.from(typeColorMap.entries())
const candidateColor = EXTENDED_COLORS[i]; .filter(([, color]) => !Object.values(NODE_TYPE_COLORS).includes(color))
if (!usedColors.has(candidateColor)) { .map(([, color]) => color)
newColor = candidateColor; );
break;
}
}
}
}
// If all colors are used, we'll still use the hashed color // Find and use the first unused extended color
// In a more advanced implementation, we could create color variants here const unusedColor = EXTENDED_COLORS.find(color => !usedExtendedColors.has(color));
const newColor = unusedColor || defaultColor;
// Update color mapping // Update color mapping
const newMap = new Map(typeColorMap); const newMap = new Map(typeColorMap);
newMap.set(nodeType, newColor); newMap.set(normalizedType, newColor);
useGraphStore.setState({ typeColorMap: newMap }); useGraphStore.setState({ typeColorMap: newMap });
return newColor; return newColor;