mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-12 15:07:11 -06:00
UIDefaultsLoader: added contrast() color function (inspired by Less CSS), which is useful to choose a foreground color that is readable, based on the luma (perceptual brightness) of a background color
This commit is contained in:
@@ -471,6 +471,12 @@ class FlatCompletionProvider
|
||||
addFunction( "shade",
|
||||
"color", colorParamDesc,
|
||||
"weight", weightParamDesc );
|
||||
|
||||
addFunction( "contrast",
|
||||
"color", colorParamDesc,
|
||||
"dark", colorParamDesc,
|
||||
"light", colorParamDesc,
|
||||
"threshold", "(optional) 0-100%, default is 43%" );
|
||||
}
|
||||
|
||||
private void addFunction( String name, String... paramNamesAndDescs ) {
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.fife.ui.rsyntaxtextarea.Token;
|
||||
import com.formdev.flatlaf.FlatLaf;
|
||||
import com.formdev.flatlaf.UIDefaultsLoaderAccessor;
|
||||
import com.formdev.flatlaf.ui.FlatUIUtils;
|
||||
import com.formdev.flatlaf.util.ColorFunctions;
|
||||
import com.formdev.flatlaf.util.HSLColor;
|
||||
import com.formdev.flatlaf.util.UIScale;
|
||||
|
||||
@@ -46,6 +47,7 @@ class FlatThemeEditorOverlay
|
||||
|
||||
static boolean showHSL = true;
|
||||
static boolean showRGB;
|
||||
static boolean showLuma;
|
||||
|
||||
private Font font;
|
||||
private Font baseFont;
|
||||
@@ -80,19 +82,21 @@ class FlatThemeEditorOverlay
|
||||
}
|
||||
|
||||
FontMetrics fm = c.getFontMetrics( font );
|
||||
int space = fm.stringWidth( " " );
|
||||
int maxTextWidth = 0;
|
||||
if( showHSL )
|
||||
maxTextWidth += fm.stringWidth( "HSL 360 100 100" );
|
||||
maxTextWidth += fm.stringWidth( "HSL 360 100 100" ) + space;
|
||||
if( showRGB )
|
||||
maxTextWidth += fm.stringWidth( "#ffffff" );
|
||||
if( showHSL && showRGB )
|
||||
maxTextWidth += fm.stringWidth( " " );
|
||||
maxTextWidth += fm.stringWidth( "#ffffff" ) + space;
|
||||
if( showLuma )
|
||||
maxTextWidth += fm.stringWidth( "100" ) + space;
|
||||
maxTextWidth = Math.max( maxTextWidth - space, 0 );
|
||||
int textHeight = fm.getAscent() - fm.getLeading();
|
||||
|
||||
int width = c.getWidth();
|
||||
int previewWidth = UIScale.scale( COLOR_PREVIEW_WIDTH );
|
||||
int gap = UIScale.scale( 4 );
|
||||
int textGap = (showHSL || showRGB) ? UIScale.scale( 6 ) : 0;
|
||||
int textGap = (showHSL || showRGB || showLuma) ? UIScale.scale( 6 ) : 0;
|
||||
|
||||
// check whether preview is outside of clip bounds
|
||||
if( clipBounds.x + clipBounds.width < width - previewWidth - maxTextWidth - gap - textGap )
|
||||
@@ -123,7 +127,7 @@ class FlatThemeEditorOverlay
|
||||
}
|
||||
|
||||
// paint text
|
||||
if( showHSL || showRGB ) {
|
||||
if( showHSL || showRGB || showLuma ) {
|
||||
int textX = px - textGap - maxTextWidth;
|
||||
if( textX > r.x + gap) {
|
||||
String colorStr = null;
|
||||
@@ -141,6 +145,14 @@ class FlatThemeEditorOverlay
|
||||
else
|
||||
colorStr = rgbStr;
|
||||
}
|
||||
if( showLuma ) {
|
||||
String lumaStr = String.format( "%3d",
|
||||
Math.round( ColorFunctions.luma( color ) * 100 ) );
|
||||
if( colorStr != null )
|
||||
colorStr += " " + lumaStr;
|
||||
else
|
||||
colorStr = lumaStr;
|
||||
}
|
||||
|
||||
int textWidth = fm.stringWidth( colorStr );
|
||||
if( textWidth > maxTextWidth )
|
||||
|
||||
@@ -83,8 +83,9 @@ class FlatThemeFileEditor
|
||||
private static final String KEY_PREVIEW = "preview";
|
||||
private static final String KEY_LAF = "laf";
|
||||
private static final String KEY_FONT_SIZE_INCR = "fontSizeIncr";
|
||||
private static final String KEY_HSL_COLORS = "hslColors";
|
||||
private static final String KEY_RGB_COLORS = "rgbColors";
|
||||
private static final String KEY_SHOW_HSL_COLORS = "showHslColors";
|
||||
private static final String KEY_SHOW_RGB_COLORS = "showRgbColors";
|
||||
private static final String KEY_SHOW_COLOR_LUMA = "showColorLuma";
|
||||
|
||||
private File dir;
|
||||
private Preferences state;
|
||||
@@ -701,9 +702,11 @@ class FlatThemeFileEditor
|
||||
private void colorModelChanged() {
|
||||
FlatThemeEditorOverlay.showHSL = showHSLColorsMenuItem.isSelected();
|
||||
FlatThemeEditorOverlay.showRGB = showRGBColorsMenuItem.isSelected();
|
||||
FlatThemeEditorOverlay.showLuma = showColorLumaMenuItem.isSelected();
|
||||
|
||||
putPrefsBoolean( state, KEY_HSL_COLORS, FlatThemeEditorOverlay.showHSL, true );
|
||||
putPrefsBoolean( state, KEY_RGB_COLORS, FlatThemeEditorOverlay.showRGB, false );
|
||||
putPrefsBoolean( state, KEY_SHOW_HSL_COLORS, FlatThemeEditorOverlay.showHSL, true );
|
||||
putPrefsBoolean( state, KEY_SHOW_RGB_COLORS, FlatThemeEditorOverlay.showRGB, false );
|
||||
putPrefsBoolean( state, KEY_SHOW_COLOR_LUMA, FlatThemeEditorOverlay.showLuma, false );
|
||||
|
||||
repaint();
|
||||
}
|
||||
@@ -755,13 +758,15 @@ class FlatThemeFileEditor
|
||||
directoryField.setModel( model );
|
||||
|
||||
// restore overlay color models
|
||||
FlatThemeEditorOverlay.showHSL = state.getBoolean( KEY_HSL_COLORS, true );
|
||||
FlatThemeEditorOverlay.showRGB = state.getBoolean( KEY_RGB_COLORS, false );
|
||||
FlatThemeEditorOverlay.showHSL = state.getBoolean( KEY_SHOW_HSL_COLORS, true );
|
||||
FlatThemeEditorOverlay.showRGB = state.getBoolean( KEY_SHOW_RGB_COLORS, false );
|
||||
FlatThemeEditorOverlay.showLuma = state.getBoolean( KEY_SHOW_COLOR_LUMA, false );
|
||||
|
||||
// restore menu item selection
|
||||
previewMenuItem.setSelected( state.getBoolean( KEY_PREVIEW, true ) );
|
||||
showHSLColorsMenuItem.setSelected( FlatThemeEditorOverlay.showHSL );
|
||||
showRGBColorsMenuItem.setSelected( FlatThemeEditorOverlay.showRGB );
|
||||
showColorLumaMenuItem.setSelected( FlatThemeEditorOverlay.showLuma );
|
||||
}
|
||||
|
||||
private void saveState() {
|
||||
@@ -877,6 +882,7 @@ class FlatThemeFileEditor
|
||||
resetFontSizeMenuItem = new JMenuItem();
|
||||
showHSLColorsMenuItem = new JCheckBoxMenuItem();
|
||||
showRGBColorsMenuItem = new JCheckBoxMenuItem();
|
||||
showColorLumaMenuItem = new JCheckBoxMenuItem();
|
||||
windowMenu = new JMenu();
|
||||
activateEditorMenuItem = new JMenuItem();
|
||||
nextEditorMenuItem = new JMenuItem();
|
||||
@@ -1024,6 +1030,11 @@ class FlatThemeFileEditor
|
||||
showRGBColorsMenuItem.setText("Show RGB colors (hex)");
|
||||
showRGBColorsMenuItem.addActionListener(e -> colorModelChanged());
|
||||
viewMenu.add(showRGBColorsMenuItem);
|
||||
|
||||
//---- showColorLumaMenuItem ----
|
||||
showColorLumaMenuItem.setText("Show color luma");
|
||||
showColorLumaMenuItem.addActionListener(e -> colorModelChanged());
|
||||
viewMenu.add(showColorLumaMenuItem);
|
||||
}
|
||||
menuBar.add(viewMenu);
|
||||
|
||||
@@ -1133,6 +1144,7 @@ class FlatThemeFileEditor
|
||||
private JMenuItem resetFontSizeMenuItem;
|
||||
private JCheckBoxMenuItem showHSLColorsMenuItem;
|
||||
private JCheckBoxMenuItem showRGBColorsMenuItem;
|
||||
private JCheckBoxMenuItem showColorLumaMenuItem;
|
||||
private JMenu windowMenu;
|
||||
private JMenuItem activateEditorMenuItem;
|
||||
private JMenuItem nextEditorMenuItem;
|
||||
|
||||
@@ -177,6 +177,11 @@ new FormModel {
|
||||
"text": "Show RGB colors (hex)"
|
||||
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "colorModelChanged", false ) )
|
||||
} )
|
||||
add( new FormComponent( "javax.swing.JCheckBoxMenuItem" ) {
|
||||
name: "showColorLumaMenuItem"
|
||||
"text": "Show color luma"
|
||||
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "colorModelChanged", false ) )
|
||||
} )
|
||||
} )
|
||||
add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) {
|
||||
name: "windowMenu"
|
||||
|
||||
@@ -71,6 +71,7 @@ public class FlatThemeTokenMaker
|
||||
tokenMap.put( "mix", TOKEN_FUNCTION );
|
||||
tokenMap.put( "tint", TOKEN_FUNCTION );
|
||||
tokenMap.put( "shade", TOKEN_FUNCTION );
|
||||
tokenMap.put( "contrast", TOKEN_FUNCTION );
|
||||
tokenMap.put( "lazy", TOKEN_FUNCTION );
|
||||
|
||||
// function options
|
||||
|
||||
Reference in New Issue
Block a user