Author: Christian Lopes
Date: 2011-06-10 08:57:08 -0700 (Fri, 10 Jun 2011)
New Revision: 25708
Modified:
core3/presentation-api/trunk/src/main/java/org/cytoscape/view/presentation/property/PaintVisualProperty.java
core3/presentation-api/trunk/src/test/java/org/cytoscape/view/presentation/VisualPropertyTest.java
Log:
Serializing colors as hexadecimal codes ("#00ff00"), but it can parse the old
format ("0,255,0") as well. And now it can also parse RGB functions
("rgb(0,255,0)").
Modified:
core3/presentation-api/trunk/src/main/java/org/cytoscape/view/presentation/property/PaintVisualProperty.java
===================================================================
---
core3/presentation-api/trunk/src/main/java/org/cytoscape/view/presentation/property/PaintVisualProperty.java
2011-06-10 04:46:57 UTC (rev 25707)
+++
core3/presentation-api/trunk/src/main/java/org/cytoscape/view/presentation/property/PaintVisualProperty.java
2011-06-10 15:57:08 UTC (rev 25708)
@@ -50,20 +50,22 @@
@Override public String toSerializableString(final Paint paint) {
- if(paint instanceof Color == false)
+ if (paint instanceof Color == false)
throw new UnsupportedOperationException("Currently,
this implementation supports only Color object.");
final Color color = (Color) paint;
- final Integer red = Integer.valueOf(color.getRed());
- final Integer green = Integer.valueOf(color.getGreen());
- final Integer blue = Integer.valueOf(color.getBlue());
+ String hex = Integer.toHexString(color.getRGB());
+ hex = hex.substring(2, hex.length()); // remove alpha bits
- return red.toString() + "," + green.toString() + "," +
blue.toString();
+ return "#" + hex;
}
@Override public Paint parseSerializableString(final String text) {
+ if (text == null)
+ throw new IllegalArgumentException("invalid color
format: null");
+
// Start by seeing if this is a hex representation
if (text.startsWith("#")) {
try {
@@ -73,8 +75,11 @@
}
}
- // ok, this must be 3 comma separated integers instead
- StringTokenizer strtok = new StringTokenizer(text, ",");
+ // could be an RGB color, such as "rgb(255,0,255)"
+ String s = text.replaceAll("(?i)rgb *\\(",
"").replaceAll("\\)", "");
+
+ // ok, this must be 3 comma separated integers now
+ StringTokenizer strtok = new StringTokenizer(s, ",");
if (strtok.countTokens() != 3)
throw new IllegalArgumentException("not all RGB
integers specified");
Modified:
core3/presentation-api/trunk/src/test/java/org/cytoscape/view/presentation/VisualPropertyTest.java
===================================================================
---
core3/presentation-api/trunk/src/test/java/org/cytoscape/view/presentation/VisualPropertyTest.java
2011-06-10 04:46:57 UTC (rev 25707)
+++
core3/presentation-api/trunk/src/test/java/org/cytoscape/view/presentation/VisualPropertyTest.java
2011-06-10 15:57:08 UTC (rev 25708)
@@ -50,28 +50,37 @@
assertEquals(CyNode.class, paintProp.getTargetDataType());
final Color testColor = new Color(10, 20, 30);
- assertEquals("10,20,30",
paintProp.toSerializableString(testColor));
+ assertEquals("#0A141E",
paintProp.toSerializableString(testColor).toUpperCase());
assertEquals(testColor,
paintProp.parseSerializableString("#0A141E"));
+ assertEquals(testColor,
paintProp.parseSerializableString("#0a141e"));
assertEquals(testColor,
paintProp.parseSerializableString("10,20,30"));
+ assertEquals(testColor,
paintProp.parseSerializableString("rgb(10,20,30)"));
+ assertEquals(testColor, paintProp.parseSerializableString("RGB
( 10 , 20 , 30 )"));
try {
- final Paint result =
paintProp.parseSerializableString("#2JK20A141E");
+ paintProp.parseSerializableString(null);
}catch(Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
try {
- final Paint result =
paintProp.parseSerializableString("10, 20");
+ paintProp.parseSerializableString("#2JK20A141E");
}catch(Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
try {
- final Paint result =
paintProp.parseSerializableString("10, 20, IJK");
+ paintProp.parseSerializableString("10, 20");
}catch(Exception e) {
assertTrue(e instanceof IllegalArgumentException);
}
+ try {
+ paintProp.parseSerializableString("10, 20, IJK");
+ }catch(Exception e) {
+ assertTrue(e instanceof IllegalArgumentException);
+ }
+
final VisualProperty<Visualizable> visualizableProp =
MinimalVisualLexicon.NODE;
assertEquals(Visualizable.class,
visualizableProp.getRange().getType());
assertTrue(visualizableProp.parseSerializableString("test
string") instanceof Visualizable );
--
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en.