diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatSyntaxTextArea.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatSyntaxTextArea.java index 755edbd5..34760888 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatSyntaxTextArea.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatSyntaxTextArea.java @@ -16,7 +16,13 @@ package com.formdev.flatlaf.themeeditor; +import java.awt.Color; +import java.util.HashMap; +import java.util.Map; +import javax.swing.text.BadLocationException; import org.fife.ui.rsyntaxtextarea.TextEditorPane; +import org.fife.ui.rsyntaxtextarea.Token; +import com.formdev.flatlaf.UIDefaultsLoaderAccessor; /** * A text area that supports editing FlatLaf themes. @@ -26,6 +32,76 @@ import org.fife.ui.rsyntaxtextarea.TextEditorPane; class FlatSyntaxTextArea extends TextEditorPane { + private boolean useColorOfColorTokens; + + private final Map parsedColorsMap = new HashMap<>(); + FlatSyntaxTextArea() { } + + boolean isUseColorOfColorTokens() { + return useColorOfColorTokens; + } + + void setUseColorOfColorTokens( boolean useColorOfColorTokens ) { + this.useColorOfColorTokens = useColorOfColorTokens; + setHighlightCurrentLine( !useColorOfColorTokens ); + } + + @Override + public Color getBackgroundForToken( Token t ) { + if( useColorOfColorTokens && t.getType() == FlatThemeTokenMaker.TOKEN_COLOR ) { + Color color = parseColor( t ); + if( color != null ) + return color; + } + + return super.getBackgroundForToken( t ); + } + + @Override + public Color getForegroundForToken( Token t ) { + if( useColorOfColorTokens && t.getType() == FlatThemeTokenMaker.TOKEN_COLOR && !isCurrentLineHighlighted( t.getOffset() )) { + Color color = parseColor( t ); + if( color != null ) { + return (colorLuminance( color ) > 164 || color.getAlpha() < 96) + ? Color.black + : Color.white; + } + } + + return super.getForegroundForToken( t ); + } + + private Color parseColor( Token token ) { + return parsedColorsMap.computeIfAbsent( token.getLexeme(), s -> { + try { + return new Color( UIDefaultsLoaderAccessor.parseColorRGBA( s ), true ); + } catch( IllegalArgumentException ex ) { + return null; + } + } ); + + } + + private int colorLuminance( Color c ) { + int red = c.getRed(); + int green = c.getGreen(); + int blue = c.getBlue(); + + int min = Math.min( red, Math.min( green, blue ) ); + int max = Math.max( red, Math.max( green, blue ) ); + + return (max + min) / 2; + } + + private boolean isCurrentLineHighlighted( int offset ) { + try { + return getHighlightCurrentLine() && + getSelectionStart() == getSelectionEnd() && + getLineOfOffset( offset ) == getLineOfOffset( getSelectionStart() ); + } catch( BadLocationException ex ) { + return false; + } + } } diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java index 27de8f8d..6f346a50 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java @@ -55,6 +55,7 @@ class FlatThemeEditorPane textArea.setSyntaxEditingStyle( FLATLAF_STYLE ); textArea.setMarkOccurrences( true ); textArea.addParser( new FlatThemeParser() ); + textArea.setUseColorOfColorTokens( true ); // theme try {