Search code examples
intellij-plugin

Getting colors from current user selected theme


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.


Solution

  • 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;
    }