0

I started making an IntelliJ plugin in Java and wanted to know if there is a way to programmatically get certain colors from the current IDE theme, for example getting the magenta terminal foreground color.

For now I only achieve getting a light color for light theme and dark for dark.

What I aim to is to get precise colors from the user installed color schemes. For instance I use the Atom One Dark theme from the plugin store.

I'm looking to get the colors in the currently selected ColorScheme, all colors from themes are stored into an .icls file by the format :

<option name="CONSOLE_ERROR_OUTPUT">
   <value>
      <option name="FOREGROUND" value="e06c75" />
   </value>
</option>

I'd like to get the color by its name attribute.

Thanks for helping, have a nice day.

Thomas Be
  • 21
  • 3
  • 1
    Here you can find some classes from intelij openapi: https://stackoverflow.com/a/55576830/13523946 – notAPPP Mar 31 '21 at 20:22
  • These show me the composition of the ColorSchemes, however i'm looking for the currently selected ColorScheme by the user in the `Appearance -> Theme` dropdown menu in settings. – Thomas Be Mar 31 '21 at 21:36

2 Answers2

2

Thank you very much you both lead me in the right direction ! I found just what I was looking for. You can get my function for collecting color schemes colors at the bottom of this post.

The solution was :

ColorSchemes files or .icls files look like this :

<scheme name="ThemeName" version="142" parent_scheme="Darcula">
  <metaInfo>
  </metaInfo>
  <colors>
    <option name="ADDED_LINES_COLOR" value="98c379" />
  </colors>
  <attributes>
    <option name="ABSTRACT_CLASS_NAME_ATTRIBUTES">
      <value>
        <option name="FOREGROUND" value="e6c07b" />
      </value>
    </option>
  </attributes>
</scheme>

You can get those colors programmatically , in java (sorry I don't know how to do it in Kotlin) you can fetch the current color scheme by doing :

EditorColorsScheme colorsScheme = EditorColorsManager.getInstance().getSchemeForCurrentUITheme();

Or you can get the default color scheme by doing :

EditorColorsScheme colorsScheme = = EditorColorsManager.getInstance().getScheme(EditorColorsManager.getInstance().getAllSchemes()[0].getName());

From that I've made a simple function that can fetch a specific color by its name (attribute and colors). If the name given does not correspond to a color defined in <attributes> it will look in the <colors> section. You can also precise if you specifically want the background color of an attribute.

Here is the function :

public Color fetchIJColor(String name, boolean isBackground){
    Color c = colorsScheme.getAttributes(TextAttributesKey.createTextAttributesKey(name)).getForegroundColor();
    if(c == null || isBackground){
        c = colorsScheme.getAttributes(TextAttributesKey.createTextAttributesKey(name)).getBackgroundColor();
    }
    if(c == null){
        c = colorsScheme.getColor(ColorKey.createColorKey(name));
    }
    return c;
}
Thomas Be
  • 21
  • 3
1

you can check class: UIUtil for example UIUtil.getTreeTextBackground() it should return Color

notAPPP
  • 188
  • 5
  • Thanks ! This is working for me to get the Background colors correctly ! However i'm looking to get some colors appearing in the editor too. For instance the color of a string surrounded by `"`. I figured that the Color i'm looking for is stored in ColorScheme `icls` files and looks like this for a given color : `````` – Thomas Be Mar 31 '21 at 22:07