Added custom Color class to envoy.ui with invert() and toHex() methods.

This commit is contained in:
Kai S. K. Engelbart 2019-12-23 11:28:00 +01:00
parent 762d7630e3
commit eebc5ab7ad
9 changed files with 127 additions and 44 deletions

View File

@ -1,11 +1,11 @@
package envoy.client; package envoy.client;
import java.awt.Color;
import java.io.*; import java.io.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.prefs.Preferences; import java.util.prefs.Preferences;
import envoy.client.ui.Color;
import envoy.client.ui.Theme; import envoy.client.ui.Theme;
/** /**

View File

@ -0,0 +1,106 @@
package envoy.client.ui;
import java.awt.color.ColorSpace;
/**
* Project: <strong>envoy-clientChess</strong><br>
* File: <strong>Color.javaEvent.java</strong><br>
* Created: <strong>23.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
*/
@SuppressWarnings("javadoc")
public class Color extends java.awt.Color {
/**
* The color white. In the default sRGB space.
*/
public static final Color white = new Color(255, 255, 255);
/**
* The color light gray. In the default sRGB space.
*/
public static final Color lightGray = new Color(192, 192, 192);
/**
* The color gray. In the default sRGB space.
*/
public static final Color gray = new Color(128, 128, 128);
/**
* The color dark gray. In the default sRGB space.
*/
public static final Color darkGray = new Color(64, 64, 64);
/**
* The color black. In the default sRGB space.
*/
public static final Color black = new Color(0, 0, 0);
/**
* The color red. In the default sRGB space.
*/
public static final Color red = new Color(255, 0, 0);
/**
* The color pink. In the default sRGB space.
*/
public static final Color pink = new Color(255, 175, 175);
/**
* The color orange. In the default sRGB space.
*/
public static final Color orange = new Color(255, 200, 0);
/**
* The color yellow. In the default sRGB space.
*/
public static final Color yellow = new Color(255, 255, 0);
/**
* The color green. In the default sRGB space.
*/
public static final Color green = new Color(0, 255, 0);
/**
* The color magenta. In the default sRGB space.
*/
public static final Color magenta = new Color(255, 0, 255);
/**
* The color cyan. In the default sRGB space.
*/
public static final Color cyan = new Color(0, 255, 255);
/**
* The color blue. In the default sRGB space.
*/
public static final Color blue = new Color(0, 0, 255);
private static final long serialVersionUID = -9166233199998257344L;
public Color(int rgb) { super(rgb); }
public Color(int rgba, boolean hasalpha) { super(rgba, hasalpha); }
public Color(int r, int g, int b) { super(r, g, b); }
public Color(float r, float g, float b) { super(r, g, b); }
public Color(ColorSpace cspace, float[] components, float alpha) { super(cspace, components, alpha); }
public Color(int r, int g, int b, int a) { super(r, g, b, a); }
public Color(float r, float g, float b, float a) { super(r, g, b, a); }
/**
* @return the inversion of this {@link Color} by replacing the red, green and
* blue values by subtracting them form 255
*/
public Color invert() { return new Color(255 - getRed(), 255 - getGreen(), 255 - getBlue()); }
/**
* @return the hex value of this {@link Color}
*/
public String toHex() { return String.format("#%02x%02x%02x", getRed(), getGreen(), getBlue()); }
}

View File

@ -1,6 +1,5 @@
package envoy.client.ui; package envoy.client.ui;
import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -46,11 +45,11 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer<Mess
// Getting the MessageColor in the Chat of the current theme // Getting the MessageColor in the Chat of the current theme
String textColor = null; String textColor = null;
textColor = toHex(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat()); textColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat().toHex();
// Getting the DateColor in the Chat of the current theme // Getting the DateColor in the Chat of the current theme
String dateColor = null; String dateColor = null;
dateColor = toHex(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat()); dateColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat().toHex();
setText(String.format("<html><p style=\"color:%s\"><b><small>%s</b></small><br><p style=\"color:%s\">%s :%s</html>", setText(String.format("<html><p style=\"color:%s\"><b><small>%s</b></small><br><p style=\"color:%s\">%s :%s</html>",
dateColor, dateColor,
@ -60,12 +59,4 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer<Mess
state)); state));
return this; return this;
} }
private String toHex(Color c) {
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
String hex = String.format("#%02x%02x%02x", r, g, b);
return hex;
}
} }

View File

@ -26,8 +26,8 @@ import envoy.client.util.EnvoyLog;
public class PrimaryToggleSwitch extends JPanel { public class PrimaryToggleSwitch extends JPanel {
private final JButton b = new JButton(""); private final JButton b = new JButton("");
private boolean currentState; private boolean currentState;
private boolean variable;
private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName());
private static final long serialVersionUID = -721155303106833184L; private static final long serialVersionUID = -721155303106833184L;
@ -69,7 +69,7 @@ public class PrimaryToggleSwitch extends JPanel {
try { try {
// Dispatch event // Dispatch event
Constructor<? extends Event<Boolean>> constructor = eventClass.getConstructor(boolean.class); Constructor<? extends Event<Boolean>> constructor = eventClass.getConstructor(boolean.class);
EventBus.getInstance().dispatch((Event<?>) constructor.newInstance(variable)); EventBus.getInstance().dispatch(constructor.newInstance(currentState));
setState(!currentState); setState(!currentState);
revalidate(); revalidate();
@ -110,6 +110,5 @@ public class PrimaryToggleSwitch extends JPanel {
add(b, gbc_toggleButton); add(b, gbc_toggleButton);
currentState = state; currentState = state;
variable = state;
} }
} }

View File

@ -1,6 +1,5 @@
package envoy.client.ui; package envoy.client.ui;
import java.awt.Color;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View File

@ -1,6 +1,5 @@
package envoy.client.ui; package envoy.client.ui;
import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import javax.swing.JLabel; import javax.swing.JLabel;
@ -44,7 +43,7 @@ public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
// Getting the UserNameColor of the current theme // Getting the UserNameColor of the current theme
String textColor = null; String textColor = null;
textColor = toHex(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor()); textColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor().toHex();
switch (status) { switch (status) {
case ONLINE: case ONLINE:
setText(String setText(String
@ -57,12 +56,4 @@ public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
} }
return this; return this;
} }
private String toHex(Color c) {
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
String hex = String.format("#%02x%02x%02x", r, g, b);
return hex;
}
} }

View File

@ -134,8 +134,7 @@ public class General extends SettingsPanel {
add(stateText, gbc_stateText); add(stateText, gbc_stateText);
descriptionText.setText(text); descriptionText.setText(text);
descriptionText.setBackground(theme.getBackgroundColor()); descriptionText.setBackground(theme.getBackgroundColor().invert());
// TODO: Change to inverted color.
descriptionText.setForeground(theme.getUserNameColor()); descriptionText.setForeground(theme.getUserNameColor());
descriptionText.setEditable(false); descriptionText.setEditable(false);

View File

@ -13,6 +13,7 @@ import javax.swing.*;
import envoy.client.Settings; import envoy.client.Settings;
import envoy.client.event.EventBus; import envoy.client.event.EventBus;
import envoy.client.event.ThemeChangeEvent; import envoy.client.event.ThemeChangeEvent;
import envoy.client.ui.Color;
import envoy.client.ui.Theme; import envoy.client.ui.Theme;
import envoy.client.util.EnvoyLog; import envoy.client.util.EnvoyLog;
@ -30,7 +31,6 @@ import envoy.client.util.EnvoyLog;
*/ */
public class ThemeCustomizationPanel extends SettingsPanel { public class ThemeCustomizationPanel extends SettingsPanel {
private JPanel colorsPanel = new JPanel(); private JPanel colorsPanel = new JPanel();
private String[] themeArray = Settings.getInstance().getThemes().keySet().toArray(new String[0]); private String[] themeArray = Settings.getInstance().getThemes().keySet().toArray(new String[0]);
@ -185,7 +185,7 @@ public class ThemeCustomizationPanel extends SettingsPanel {
textPane.setFont(new Font("Arial", Font.PLAIN, 14)); textPane.setFont(new Font("Arial", Font.PLAIN, 14));
textPane.setBackground(theme.getBackgroundColor()); textPane.setBackground(theme.getBackgroundColor());
textPane.setForeground(getInvertedColor(theme.getBackgroundColor())); textPane.setForeground(theme.getBackgroundColor().invert());
textPane.setText(name); textPane.setText(name);
textPane.setEditable(false); textPane.setEditable(false);
@ -194,7 +194,7 @@ public class ThemeCustomizationPanel extends SettingsPanel {
button.addActionListener((evt) -> { button.addActionListener((evt) -> {
try { try {
Color newColor = JColorChooser.showDialog(null, "Choose a color", color); Color newColor = (Color) JColorChooser.showDialog(null, "Choose a color", color);
if (newColor.getRGB() != color.getRGB()) { if (newColor.getRGB() != color.getRGB()) {
logger.log(Level.FINEST, "New Color: " + String.valueOf(color.getRGB())); logger.log(Level.FINEST, "New Color: " + String.valueOf(color.getRGB()));
// TODO: When Theme changed in same settings screen, color variable doesn't // TODO: When Theme changed in same settings screen, color variable doesn't
@ -228,6 +228,4 @@ public class ThemeCustomizationPanel extends SettingsPanel {
colorsPanel.add(button, gbc_button); colorsPanel.add(button, gbc_button);
} }
private Color getInvertedColor(Color color) { return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue()); }
} }