You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2021/01/14 06:01:40 UTC
svn commit: r1885469 - in /pivot/trunk:
demos/src/org/apache/pivot/demos/colors/Colors.java
wtk/src/org/apache/pivot/wtk/CSSColor.java
Author: rwhitcomb
Date: Thu Jan 14 06:01:39 2021
New Revision: 1885469
URL: http://svn.apache.org/viewvc?rev=1885469&view=rev
Log:
Enhancements to the CSSColor demo.
Modified:
pivot/trunk/demos/src/org/apache/pivot/demos/colors/Colors.java
pivot/trunk/wtk/src/org/apache/pivot/wtk/CSSColor.java
Modified: pivot/trunk/demos/src/org/apache/pivot/demos/colors/Colors.java
URL: http://svn.apache.org/viewvc/pivot/trunk/demos/src/org/apache/pivot/demos/colors/Colors.java?rev=1885469&r1=1885468&r2=1885469&view=diff
==============================================================================
--- pivot/trunk/demos/src/org/apache/pivot/demos/colors/Colors.java (original)
+++ pivot/trunk/demos/src/org/apache/pivot/demos/colors/Colors.java Thu Jan 14 06:01:39 2021
@@ -17,14 +17,18 @@
package org.apache.pivot.demos.colors;
import java.awt.Color;
+import java.awt.Font;
+import java.util.Set;
import org.apache.pivot.collections.Map;
+import org.apache.pivot.util.StringUtils;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.Border;
import org.apache.pivot.wtk.BoxPane;
import org.apache.pivot.wtk.CSSColor;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
-import org.apache.pivot.wtk.FlowPane;
+import org.apache.pivot.wtk.FontUtilities;
+import org.apache.pivot.wtk.GridPane;
import org.apache.pivot.wtk.HorizontalAlignment;
import org.apache.pivot.wtk.Label;
import org.apache.pivot.wtk.Orientation;
@@ -33,38 +37,92 @@ import org.apache.pivot.wtk.Style;
import org.apache.pivot.wtk.Window;
public final class Colors implements Application {
+ private static final int CELLS_PER_ROW = 5;
+
private Window mainWindow;
+
+ private Label makeLabel(final String text) {
+ Label label = new Label(text);
+ label.getStyles().put(Style.horizontalAlignment, HorizontalAlignment.CENTER);
+ return label;
+ }
+
@Override
- public void startup(Display display, Map<String, String> properties) {
- FlowPane flowPane = new FlowPane();
- flowPane.getStyles().put(Style.padding, 6);
+ public void startup(final Display display, final Map<String, String> properties) {
+ GridPane gridPane = new GridPane(CELLS_PER_ROW);
+ gridPane.getStyles().put(Style.padding, 6);
+
+ Font fontBold = FontUtilities.getFont(FontUtilities.SANS_SERIF_FONTS, Font.BOLD, 13);
+ Font fontRegular = FontUtilities.getFont(FontUtilities.SANS_SERIF_FONTS, Font.PLAIN, 12);
+ Font fontItalic = FontUtilities.getFont(FontUtilities.SANS_SERIF_FONTS, Font.ITALIC, 11);
+
+ int cell = 0;
+ GridPane.Row row = null;
+
+ int numColors = CSSColor.numberOfColors();
+
for (CSSColor color : CSSColor.values()) {
+ if (cell % CELLS_PER_ROW == 0) {
+ row = new GridPane.Row(gridPane);
+ }
+
BoxPane container = new BoxPane(Orientation.VERTICAL);
container.getStyles().put(Style.padding, 4);
container.getStyles().put(Style.fill, true);
+
BoxPane colorFill = new BoxPane(Orientation.VERTICAL);
- Color fillColor = color.getColor();
+
+ Color fillColor = color.getColor();
+ String colorName = color.toString();
+ int r = fillColor.getRed();
+ int g = fillColor.getGreen();
+ int b = fillColor.getBlue();
+
colorFill.getStyles().put(Style.backgroundColor, fillColor);
- colorFill.setMinimumWidth(50);
- colorFill.setPreferredHeight(50);
- colorFill.setTooltipText(String.format("%1$s=R:%2$3d,G:%3$3d,B:%4$3d",
- color.toString(), fillColor.getRed(), fillColor.getGreen(), fillColor.getBlue()));
- Label nameLabel = new Label(color.toString());
- nameLabel.getStyles().put(Style.horizontalAlignment, HorizontalAlignment.CENTER);
+ colorFill.setPreferredWidth(372);
+ colorFill.setPreferredHeight(100);
+ Set<CSSColor> matchingColors = CSSColor.getMatchingColors(color);
+ String matches = matchingColors.size() == 0
+ ? "No matches."
+ : "Matches: " + StringUtils.toString(matchingColors);
+ colorFill.setTooltipText(matches);
+
+ Label nameLabel = makeLabel(color.toString());
+ nameLabel.getStyles().put(Style.font, fontBold);
+
+ String rgbText = String.format("R=%1$3d, G=%2$3d, B=%3$3d", r, g, b);
+ Label rgbLabel = makeLabel(rgbText);
+ rgbLabel.getStyles().put(Style.font, fontRegular);
+
+ float[] hsbValues = Color.RGBtoHSB(r, g, b, null);
+ String hsbText = String.format("H=%1$5.3f, S=%2$5.3f, V=%3$5.3f",
+ hsbValues[0], hsbValues[1], hsbValues[2]);
+ Label hsbLabel = makeLabel(hsbText);
+ hsbLabel.getStyles().put(Style.font, fontRegular);
+
+ String seqText = String.format("%1$d / %2$d", cell + 1, numColors);
+ Label seqLabel = makeLabel(seqText);
+ seqLabel.getStyles().put(Style.font, fontItalic);
+
container.add(colorFill);
container.add(nameLabel);
- flowPane.add(new Border(container));
+ container.add(rgbLabel);
+ container.add(hsbLabel);
+ container.add(seqLabel);
+
+ row.add(new Border(container));
+ cell++;
}
ScrollPane scrollPane = new ScrollPane(ScrollPane.ScrollBarPolicy.FILL,
ScrollPane.ScrollBarPolicy.AUTO);
- scrollPane.setView(flowPane);
+ scrollPane.setView(gridPane);
mainWindow = new Window(scrollPane);
mainWindow.setMaximized(true);
mainWindow.open(display);
}
@Override
- public boolean shutdown(boolean optional) {
+ public boolean shutdown(final boolean optional) {
if (mainWindow != null) {
mainWindow.close();
mainWindow = null;
@@ -72,7 +130,7 @@ public final class Colors implements App
return false;
}
- public static void main(String[] args) {
+ public static void main(final String[] args) {
DesktopApplicationContext.main(Colors.class, args);
}
}
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/CSSColor.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/CSSColor.java?rev=1885469&r1=1885468&r2=1885469&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/CSSColor.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/CSSColor.java Thu Jan 14 06:01:39 2021
@@ -347,4 +347,30 @@ public enum CSSColor {
return matches;
}
+ /**
+ * Return the set of all these colors with the same RGB value as the given {@code CSSColor},
+ * in other words, the synonyms for this color.
+ * <p> This could be a bit time-consuming because we have to search all the values.
+ * @param color One of these colors to find the matching enum values.
+ * @return The complete set (could be empty) of these colors with the same RGB value (not
+ * including the given color).
+ */
+ public static Set<CSSColor> getMatchingColors(final CSSColor color) {
+ Set<CSSColor> matches = EnumSet.noneOf(CSSColor.class);
+ Color solidColor = color.color;
+ for (CSSColor cssColor : values()) {
+ if (cssColor.color.equals(solidColor) && cssColor != color) {
+ matches.add(cssColor);
+ }
+ }
+ return matches;
+ }
+
+ /**
+ * @return The number of colors in this list.
+ */
+ public static int numberOfColors() {
+ return values().length;
+ }
+
}