Skip to content

fix: replace hardcoded colour maps with color-hash for consistent, scalable colouring#87

Merged
NotYuSheng merged 2 commits intomainfrom
fix/consistent-color-system
Mar 28, 2026
Merged

fix: replace hardcoded colour maps with color-hash for consistent, scalable colouring#87
NotYuSheng merged 2 commits intomainfrom
fix/consistent-color-system

Conversation

@NotYuSheng
Copy link
Copy Markdown
Owner

Summary

  • Replaces three separate ad-hoc colour systems (Bootstrap badge classes, APP_COLORS map, CATEGORY_COLORS map) with a single deterministic string-to-colour approach using color-hash
  • Any protocol, app name, or category now gets a consistent colour automatically — same name always hashes to the same colour, new values require no code changes
  • Exports getProtocolColor() from features/network/constants.ts as a single source of truth, used by both the network graph edge strokes and all conversation badge components
  • Removes the local duplicate getProtocolColor() from NetworkGraph.tsx
  • Removes getProtocolBadgeClass() from ConversationList (Bootstrap class lookup that only covered 8 protocols)
  • Category pills in ConversationFilterPanel now colour on active, consistent with app pills

Test plan

  • Protocol badges in conversation list and detail show distinct colours for TCP, UDP, IGMP, PIM, etc.
  • Category badges (Web, VoIP, Network, Media…) show distinct, consistent colours
  • App name badges colour correctly for known and unknown app names
  • Network diagram edge colours unchanged
  • docker compose build passes cleanly

🤖 Generated with Claude Code

…alable colouring

Previously, protocol/app/category badges used three separate ad-hoc colour
systems — a Bootstrap badge-class lookup (getProtocolBadgeClass), a small
hand-maintained APP_COLORS map, and a CATEGORY_COLORS map — all falling back
to a hardcoded purple for anything unknown.

This replaces all three with a single deterministic string-to-colour approach
using the `color-hash` library (v2.0.2), which hashes any string to a
consistent HSL colour. Same name always produces the same colour, and any
new protocol/app/category gets a distinct colour automatically without any
code changes.

Changes:
- appColors.ts: replace APP_COLORS/CATEGORY_COLORS maps with color-hash;
  getAppColor() and getCategoryColor() now delegate to colorHash.hex()
- constants.ts: export getProtocolColor() as single source of truth for
  protocol colours, used by both NetworkGraph (edge strokes) and conversation
  components
- NetworkGraph.tsx: remove local duplicate getProtocolColor(); import from
  constants instead
- ConversationList.tsx: replace getProtocolBadgeClass() Bootstrap-class
  lookup with inline style using getProtocolColor(); use getCategoryColor()
  for category badges
- ConversationDetail.tsx: protocol badge uses getProtocolColor() instead
  of hardcoded bg-primary
- ConversationFilterPanel.tsx: category pills use getCategoryColor() when
  active, consistent with app pills
- CategoryBreakdownChart.tsx: already using getCategoryColor(), no change
  needed to logic
- package.json: add color-hash ^2.0.2 and @types/color-hash ^2.0.0

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces hardcoded application colors with a dynamic color generation system using the color-hash library and centralizes protocol color logic. While these changes improve maintainability and visual variety, the reviewer identified accessibility issues where hardcoding white text on dynamically generated background colors (such as yellow for UDP) results in poor contrast. It is recommended to implement a utility function that calculates the appropriate text color (black or white) based on the background's luminance to ensure readability across all generated colors.

Comment thread frontend/src/components/conversation/ConversationDetail/ConversationDetail.tsx Outdated
Comment thread frontend/src/components/conversation/ConversationList/ConversationList.tsx Outdated
…sibility

Hardcoded white text on hashed background colours fails WCAG contrast
requirements for light hues (e.g. yellow). Added getTextColor(hex) to
appColors.ts which picks '#000' or '#fff' based on perceived luminance
(0.299R + 0.587G + 0.114B). Applied to all badge/pill sites:
- ConversationList: protocol, app name, category badges
- ConversationDetail: protocol and app name badges
- ConversationFilterPanel: active app and category filter pills

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@NotYuSheng NotYuSheng merged commit 2d10caa into main Mar 28, 2026
@NotYuSheng NotYuSheng deleted the fix/consistent-color-system branch March 28, 2026 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant