PrismatIQ::ThemeDetector
Inherits Reference < Object
Thread-safe theme detector with instance-based caching.
This class detects whether a color scheme is light or dark based on relative luminance calculations. Results are cached for performance.
Thread Safety Guarantees
- Instance Isolation: Each
ThemeDetectorinstance has its own cache. Multiple threads can safely use different instances without coordination. - Internal Synchronization: Caches use
ThreadSafeCachewith mutex synchronization. - No Global State: No class variables or shared mutable state between instances.
- Safe Concurrent Access: Multiple fibers/threads can call methods on the same instance simultaneously without race conditions.
Usage Patterns
Create instances as needed - they can be safely shared across threads:
# Safe to share across multiple fibers
detector = ThemeDetector.new
spawn { detector.detect_theme(color1) }
spawn { detector.detect_theme(color2) }
Migration Note
This replaces the deprecated module-level PrismatIQ::Theme methods which
were not thread-safe due to shared global state.
- Each instance has its own cache (no shared global state)
- Safe to share a single instance across fibers, or create per-fiber instances
- Caching uses
ThreadSafeCachewith mutex synchronization
Example
detector = ThemeDetector.new
# These calls are thread-safe
theme = detector.detect_theme(color)
info = detector.detect_theme_info(color)
# Clear cache when done (also thread-safe)
detector.clear_cache
Constructors
new(accessibility : AccessibilityCalculator | Nil = nil)
Instance methods
analyze_palette(palette : Array(RGB)) : Hash(Symbol, Array(RGB))
analyze_theme(background : RGB) : ThemeInfo
cache_stats
clear_cache
dark?(color : RGB) : Bool
detect_theme(color : RGB) : Symbol
detect_theme_info(color : RGB) : ThemeInfo
dominant_theme(palette : Array(RGB)) : Symbol
light?(color : RGB) : Bool
perceived_brightness(rgb : RGB) : Float64
relative_luminance(rgb : RGB) : Float64
suggest_background(foreground : RGB) : RGB
suggest_foreground(background : RGB) : RGB
suggest_text_palette(background : RGB, level : WCAGLevel = WCAGLevel::AA) : TextColorPalette