tmf: Accelerate RGBA.fromString()
Change-Id: Idce19753c7b8dff9f3adfd517edb5e19f9e78625
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
Reviewed-on: https://git.eclipse.org/r/160072
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Trace Compass Bot <tracecompass-bot@eclipse.org>
diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/presentation/RGBAColor.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/presentation/RGBAColor.java
index e40c4a4..d2dbdd5 100644
--- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/presentation/RGBAColor.java
+++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/presentation/RGBAColor.java
@@ -102,17 +102,47 @@
/**
* String parser, parses the output of {@link RGBAColor#toString()}
*
- * @param toString
- * the color
+ * @param rgbaString
+ * the color string
* @return the RGBA or null if invalid
* @since 4.1
*/
- public static @Nullable RGBAColor fromString(String toString) {
- try {
- return new RGBAColor(Long.decode(toString).intValue());
- } catch (NumberFormatException ne) {
+ public static @Nullable RGBAColor fromString(String rgbaString) {
+ if (rgbaString.length() != 9 || rgbaString.charAt(0) != '#') {
return null;
}
+ int r = toInt(rgbaString.charAt(1), rgbaString.charAt(2));
+ int g = toInt(rgbaString.charAt(3), rgbaString.charAt(4));
+ int b = toInt(rgbaString.charAt(5), rgbaString.charAt(6));
+ int a = toInt(rgbaString.charAt(7), rgbaString.charAt(8));
+ if (r == -1 || g == -1 || b == -1 || a == -1) {
+ return null;
+ }
+ return new RGBAColor(r, g, b, a);
+ }
+
+ /**
+ * String parser, parses the RGB value in format "#RRGGBB" with the
+ * specified alpha value
+ *
+ * @param rgbString
+ * the color string
+ * @param alpha
+ * the alpha
+ * @return the RGBA or null if invalid
+ * @since 6.0
+ */
+ public static @Nullable RGBAColor fromString(String rgbString, int alpha) {
+ if (rgbString.length() != 7 || rgbString.charAt(0) != '#') {
+ return null;
+ }
+ int r = toInt(rgbString.charAt(1), rgbString.charAt(2));
+ int g = toInt(rgbString.charAt(3), rgbString.charAt(4));
+ int b = toInt(rgbString.charAt(5), rgbString.charAt(6));
+ if (r == -1 || g == -1 || b == -1) {
+ return null;
+ }
+ return new RGBAColor(r, g, b, alpha);
}
/**
@@ -268,4 +298,29 @@
public String toString() {
return String.format("#%08X", toInt()); //$NON-NLS-1$
}
+
+ private static int toInt(char h, char l) {
+ int high = fromDigit(h);
+ if (high == -1) {
+ return -1;
+ }
+ int low = fromDigit(l);
+ if (low == -1) {
+ return -1;
+ }
+ return 16 * high + low;
+ }
+
+ private static int fromDigit(char c) {
+ if (c >= '0' && c <= '9') {
+ return c - '0';
+ }
+ if (c >= 'A' && c <= 'F') {
+ return 10 + c - 'A';
+ }
+ if (c >= 'a' && c <= 'f') {
+ return 10 + c - 'a';
+ }
+ return -1;
+ }
}
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/model/StyleManager.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/model/StyleManager.java
index 77669a6..5719488 100644
--- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/model/StyleManager.java
+++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/model/StyleManager.java
@@ -158,7 +158,7 @@
style = fStyleMap.get(style.getParentKey());
}
int alpha = (opacity == null) ? 255 : (int) (opacity * 255);
- RGBAColor rgba = (color == null) ? (opacity == null ? null : new RGBAColor(0, 0, 0, alpha)) : RGBAColor.fromString(color + String.format("%02X", alpha)); //$NON-NLS-1$
+ RGBAColor rgba = (color == null) ? (opacity == null ? null : new RGBAColor(0, 0, 0, alpha)) : RGBAColor.fromString(color, alpha);
return (rgba == null) ? null : (blend == null) ? rgba : blend(rgba, blend);
}