mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-11 06:27:13 -06:00
Theme Editor: use real colors as background of color strings
This commit is contained in:
@@ -16,7 +16,13 @@
|
|||||||
|
|
||||||
package com.formdev.flatlaf.themeeditor;
|
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.TextEditorPane;
|
||||||
|
import org.fife.ui.rsyntaxtextarea.Token;
|
||||||
|
import com.formdev.flatlaf.UIDefaultsLoaderAccessor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A text area that supports editing FlatLaf themes.
|
* A text area that supports editing FlatLaf themes.
|
||||||
@@ -26,6 +32,76 @@ import org.fife.ui.rsyntaxtextarea.TextEditorPane;
|
|||||||
class FlatSyntaxTextArea
|
class FlatSyntaxTextArea
|
||||||
extends TextEditorPane
|
extends TextEditorPane
|
||||||
{
|
{
|
||||||
|
private boolean useColorOfColorTokens;
|
||||||
|
|
||||||
|
private final Map<String, Color> parsedColorsMap = new HashMap<>();
|
||||||
|
|
||||||
FlatSyntaxTextArea() {
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ class FlatThemeEditorPane
|
|||||||
textArea.setSyntaxEditingStyle( FLATLAF_STYLE );
|
textArea.setSyntaxEditingStyle( FLATLAF_STYLE );
|
||||||
textArea.setMarkOccurrences( true );
|
textArea.setMarkOccurrences( true );
|
||||||
textArea.addParser( new FlatThemeParser() );
|
textArea.addParser( new FlatThemeParser() );
|
||||||
|
textArea.setUseColorOfColorTokens( true );
|
||||||
|
|
||||||
// theme
|
// theme
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user