Skip to main content

Overview

The getUserColor utility function generates a consistent HSL color string based on a user ID. Each unique user ID will always produce the same color, making it perfect for visual user identification in avatars, cursors, highlights, and other UI elements where you need to visually distinguish between different users.
Perfect for: User avatars, cursor colors, user highlights, visual identification, collaborative annotations, and creating consistent user-specific styling across your application.

Import

Signature

Parameters

string
required
The unique user identifier to generate a color from. The same userId will always produce the same color.

Returns

string
An HSL color string (e.g., “hsl(240, 70%, 50%)”) that is consistent for the given userId. The color has good saturation and lightness for UI use.

Examples

Basic Usage

Generate colors for user identification:

User Avatar System

Create consistent user avatars with colors:

Live Cursors with User Colors

Implement live cursors with consistent user colors:

Collaborative Highlighting

Create user-specific highlighting colors:

Team Color Legend

Create a color legend for team identification:

Activity Feed with User Colors

Build an activity feed using user colors for identification:

Color Accessibility Helper

Create accessible color combinations:

Color Theme Generator

Generate color themes based on user colors:

Color Properties

The generated colors have these characteristics: HSL Format: hsl(hue, saturation%, lightness%)
  • Hue: 0-360 degrees (varies by user)
  • Saturation: ~70% (high saturation for visibility)
  • Lightness: ~50% (balanced for readability)
Example colors:

Consistency Guarantee

The same user ID will always generate the same color:

Color Accessibility

High Contrast

Colorblind Considerations

Best Practices

Performance

UI Consistency

Accessibility

Common Use Cases

  • User Avatars: Background colors for user profile pictures
  • Live Cursors: Distinct colors for collaborative cursor tracking
  • Text Highlighting: User-specific highlighting colors
  • Visual Identification: Consistent user identification across UI
  • Team Coordination: Color-coded team member identification
  • Activity Tracking: Color-coded user activity in feeds

TypeScript Support

The function is fully typed for better development experience: