Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting theme colors in Visual Studio Classifier extension

I'm building a syntax highlighting extension for the Properties language in Visual Studio and already built the classifier/tagger. I'm stuck however at setting/choosing the right colors for the different tags (i.e. keys, values, comments).

I'd like to make the colors follow the current theme of Visual Studio. Right now they're hard-coded (see ForegroundColor = ...):

[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = "PropertiesKeyTypeDefinition")]
[Name("PropertiesKeyFormat")]
[Order(Before = Priority.Default)]
internal sealed class PropertiesKey : ClassificationFormatDefinition {
    public PropertiesKey() {
        DisplayName = "Properties Key";
        ForegroundColor = Color.FromRgb(86, 156, 214);
    }
}

What I've found so far:

  • This question presumes my question is already answered.
  • This answer shows where in the registry the colors could be stored, but that's not a reliable solution.
  • This question addresses colors for WPF (not my case)
  • There's the Extensibility Tools extension with the Theme Swatches which show the colors from EnvironmentColors, however I don't know how to use the C# code it provides

If possible, I'd like to use the colors used for 'Keyword', 'String' and 'Comment' colors which can be found in VS in Tools -> Options -> Environment -> Fonts and Colors, again, in accordance with the current theme.

like image 886
clausavram Avatar asked Aug 20 '16 11:08

clausavram


People also ask

How do I create a color theme in Visual Studio?

Here's how to change the color theme of the IDE frame and the tool windows in Visual Studio. On the menu bar, choose Tools > Options. In the options list, choose Environment > General. In the Color theme list, choose either the default Dark theme, the Light theme, the Blue theme, or the Blue (Extra Contrast) theme.

How do you change the color theme on VS code?

You can customize your active Visual Studio Code color theme with the workbench. colorCustomizations user setting. Note: If you want to use an existing color theme, see Color Themes where you'll learn how to set the active color theme through the Preferences: Color Theme dropdown (Ctrl+K Ctrl+T).


1 Answers

Based on the EnvironmentColors you can get a ThemeResourceKey.

That key can than be transformed into a SolidColorBrush using this function:

private static SolidColorBrush ToBrush(ThemeResourceKey key)
{
    var color = VSColorTheme.GetThemedColor(key);
    return new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
}

So assigning it to your foreground becomes:

[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = "PropertiesKeyTypeDefinition")]
[Name("PropertiesKeyFormat")]
[Order(Before = Priority.Default)]
internal sealed class PropertiesKey : ClassificationFormatDefinition {
    public PropertiesKey() {
        DisplayName = "Properties Key";
        ForegroundColor = ToBrush(EnvironmentColors.ClassDesignerCommentTextColorKey);
    }
 }

Documentation:

ThemeResouceKey

VSColorTheme.GetThemedColor

Extra:

This might be helpful in selecting the right ThemeResourceKey

VS Colors

like image 173
sboulema Avatar answered Oct 18 '22 18:10

sboulema