From aeb72426c17467a72832919b003add2ac635b082 Mon Sep 17 00:00:00 2001 From: Sam Pottinger Date: Tue, 16 May 2023 11:10:14 -0700 Subject: [PATCH] Closes #720. Fix an issue where tweaks mode wont run if there are semi-transparent colors in the tab because of a regex issue. --- .../processing/mode/java/tweak/Handle.java | 34 ++++++++++++++++--- .../mode/java/tweak/SketchParser.java | 2 +- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/java/src/processing/mode/java/tweak/Handle.java b/java/src/processing/mode/java/tweak/Handle.java index 72dfa5ed31..d1b21a74ca 100644 --- a/java/src/processing/mode/java/tweak/Handle.java +++ b/java/src/processing/mode/java/tweak/Handle.java @@ -85,12 +85,26 @@ public Handle(String t, String n, int vi, String v, int ti, int l, int sc, textFormat = "0x%x"; } else if ("webcolor".equals(type)) { - Long val = Long.parseLong(strValue.substring(1, strValue.length()), 16); + Long val; + String prefix; + if (strValue.length() == 7) { + val = Long.parseLong(strValue.substring(1, strValue.length()), 16); + prefix = ""; + } else { + String valStr = strValue.substring( + strValue.length() - 6, + strValue.length() + ); + val = Long.parseLong(valStr, 16); + prefix = strValue.substring( + 1, + strValue.length() - 6 + ); + } val = val | 0xff000000; value = newValue = val.intValue(); strNewValue = strValue; - textFormat = "#%06x"; - + textFormat = "#" + prefix + "%06x"; } else if ("float".equals(type)) { value = newValue = Float.parseFloat(strValue); strNewValue = strValue; @@ -267,7 +281,19 @@ public void sendNewValue() { } else if ("hex".equals(type)) { tweakClient.sendInt(index, newValue.intValue()); } else if ("webcolor".equals(type)) { - tweakClient.sendInt(index, newValue.intValue()); + // If full opaque color, don't spend the cycles on string processing + // which does appear to matter at high frame rates. Otherwise take the + // hit and parse back from string value with transparency. + if (strNewValue.length() == 7) { + tweakClient.sendInt(index, newValue.intValue()); + } else { + long target = Long.parseLong( + strNewValue.substring(1, strNewValue.length()), + 16 + ); + tweakClient.sendInt(index, (int) target); + } + } else if ("float".equals(type)) { tweakClient.sendFloat(index, newValue.floatValue()); } diff --git a/java/src/processing/mode/java/tweak/SketchParser.java b/java/src/processing/mode/java/tweak/SketchParser.java index 2dfedd7321..40fdffee1e 100644 --- a/java/src/processing/mode/java/tweak/SketchParser.java +++ b/java/src/processing/mode/java/tweak/SketchParser.java @@ -270,7 +270,7 @@ private void addAllHexNumbers() { * list of all hexadecimal numbers in the sketch */ private void addAllWebColorNumbers() { - Pattern p = Pattern.compile("#[A-Fa-f0-9]{6}"); + Pattern p = Pattern.compile("#([A-Fa-f0-9]{2})?[A-Fa-f0-9]{6}"); for (int i=0; i