3.1 KiB
3.1 KiB
04. Create Theme Resolution Logic with Color Reference Lookup
meta: id: theme-refactoring-04 feature: theme-refactoring-json-format priority: P0 depends_on: [theme-refactoring-02, theme-refactoring-03] tags: [implementation, logic]
objective:
- Implement theme resolution logic that handles color references and variants
- Create function to resolve colors from theme JSON files
- Support hex colors, color references, ANSI codes, and RGBA values
- Handle light/dark mode selection based on current mode
deliverables:
src/utils/theme-resolver.ts- Theme resolution utilitysrc/utils/ansi-to-rgba.ts- ANSI color conversion utility
steps:
-
Step 4.1: Create
src/utils/ansi-to-rgba.ts- Implement
ansiToRgba(code: number): RGBAfunction - Handle standard ANSI colors (0-15)
- Handle 6x6x6 color cube (16-231)
- Handle grayscale ramp (232-255)
- Add type definitions for RGBA
- Implement
-
Step 4.2: Create
src/utils/theme-resolver.ts- Implement
resolveTheme(theme: ThemeJson, mode: "dark" | "light"): ThemeColors - Create
resolveColor(c: ColorValue): RGBAhelper function - Handle RGBA objects directly
- Handle hex color strings
- Handle color references from
defs - Handle variants (dark/light mode)
- Handle ANSI codes
- Add error handling for invalid color references
- Implement
-
Step 4.3: Implement color reference resolution
- Create lookup logic for
defsobject - Add fallback to theme colors if reference not in defs
- Throw descriptive errors for invalid references
- Create lookup logic for
-
Step 4.4: Handle optional theme properties
- Support
selectedListItemTextproperty - Support
backgroundMenuproperty - Support
thinkingOpacityproperty - Add default values for missing properties
- Support
tests:
-
Unit:
- Test
ansiToRgbawith ANSI codes 0-15 - Test
ansiToRgbawith color cube codes 16-231 - Test
ansiToRgbawith grayscale codes 232-255 - Test
resolveColorwith hex colors - Test
resolveColorwith color references - Test
resolveColorwith light/dark variants - Test
resolveColorwith RGBA objects - Test
resolveThemewith complete theme JSON - Test
resolveThemewith missing optional properties - Test error handling for invalid color references
- Test
-
Integration/e2e:
- Test resolving colors from actual theme JSON files
- Verify light/dark mode selection works correctly
acceptance_criteria:
src/utils/ansi-to-rgba.tsfile exists with conversion functionssrc/utils/theme-resolver.tsfile exists with resolution logic- All color value types are handled correctly
- Error messages are descriptive and helpful
- Theme resolution works with both light and dark modes
validation:
- Run:
bun run typecheck- Should pass - Run unit tests with
bun test src/utils/theme-resolver.test.ts - Test with sample theme JSON files
- Verify error messages are clear
notes:
- Use opencode's implementation as reference for color resolution
- Ensure RGBA values are normalized to 0-1 range
- Add comprehensive error handling for invalid inputs
- Reference:
/home/mike/code/PodTui/opencode/packages/opencode/src/cli/cmd/tui/context/theme.tsx(lines 176-277)