Skip to content

Add asHex proposal to ThemeColor API #252631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 27, 2025

This PR introduces an asHex proposal to the ThemeColor API. The change adds a new method to allow consumers to retrieve the hexadecimal representation of a ThemeColor instance, improving theme color interoperability and making it easier for extensions and core features to work with color values in a standard hex format.

Changes

  • New Proposal File: Added vscode.proposed.themeColorAsHex.d.ts with the new API definition
  • Extended Protocol: Updated extHost.protocol.ts to support theme color resolution via RPC
  • Main Thread Implementation: Added $getColorAsHex method to MainThreadTheming using existing theme service
  • Extension Host Implementation: Added asHex() method to ThemeColor class with proper error handling
  • API Registration: Updated extHost.api.impl.ts to register theming service for ThemeColor access
  • Comprehensive Tests: Added test coverage for valid colors, invalid colors, and error scenarios

API Design

export class ThemeColor {
    /**
     * Returns the hexadecimal representation of this theme color when resolved
     * against the currently active color theme.
     * 
     * @returns A promise that resolves to the hexadecimal color string (e.g., '#FF0000' for red), 
     * or undefined if the color cannot be resolved in the current theme.
     */
    asHex(): Promise<string | undefined>;
}

Usage Example

const themeColor = new vscode.ThemeColor('editor.foreground');
const hexValue = await themeColor.asHex(); // Returns "#CCCCCC" or undefined

// Integration with external libraries
if (hexValue) {
    externalChart.setTextColor(hexValue);
}

Key Features

  • Async Design: Uses RPC communication to resolve colors from the main thread
  • Standard Format: Returns consistent hex format (#RRGGBB) using existing VS Code color utilities
  • Graceful Degradation: Returns undefined for colors that cannot be resolved
  • Error Handling: Proper error messages when theming service is unavailable
  • No Breaking Changes: Purely additive API that maintains backward compatibility

Use Cases Enabled

  • Integration with external charting and visualization libraries that require hex color values
  • Dynamic CSS generation based on current theme colors
  • Accessibility calculations (contrast ratios, color blindness considerations)
  • Color interoperability with non-VS Code UI components
  • Custom rendering scenarios where actual color values are needed

Implementation Notes

The implementation leverages VS Code's existing color infrastructure:

  • Uses Color.Format.CSS.formatHex() for consistent formatting
  • Integrates with the existing theme service architecture
  • Follows established patterns for extension host ↔ main thread communication
  • Maintains the same error handling patterns as other async APIs

No breaking changes are introduced. The proposal follows VS Code coding guidelines and API conventions.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • electronjs.org
    • Triggering command: node-gyp (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add asHex proposal to ThemeColor Add asHex proposal to ThemeColor API Jun 27, 2025
@Copilot Copilot AI requested a review from alexr00 June 27, 2025 12:24
Copilot finished work on behalf of alexr00 June 27, 2025 12:24
Copy link
Member

@alexr00 alexr00 left a comment

Choose a reason for hiding this comment

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

Instead of putting asHex on ThemeColor, add a getHexFromThemeColor to the ColorTheme.

Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 27, 2025

Instead of putting asHex on ThemeColor, add a getHexFromThemeColor to the ColorTheme.

Done! I've refactored the API to add getHexFromThemeColor to the ColorTheme interface instead of having asHex on ThemeColor. The new API allows accessing the hex value through the active color theme: vscode.window.activeColorTheme.getHexFromThemeColor(themeColor). Commit: 7764287

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • electronjs.org
    • Triggering command: node-gyp (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@Copilot Copilot AI requested a review from alexr00 June 27, 2025 12:49
Copilot finished work on behalf of alexr00 June 27, 2025 12:49
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.

2 participants