Skip to content

Commit b508e0c

Browse files
authored
Merge pull request #2407 from ControlSystemStudio/CSSTUDIO-1215
Csstudio 1215: Transparency Support for ProgressBar, SlideButton, ComboBox widgets
2 parents f8908dd + a778ce0 commit b508e0c

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/JFXUtil.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*******************************************************************************/
88
package org.csstudio.display.builder.representation.javafx;
99

10+
import java.text.DecimalFormat;
1011
import java.util.Locale;
1112
import java.util.concurrent.ConcurrentHashMap;
1213
import java.util.logging.Level;
@@ -32,6 +33,7 @@
3233
public class JFXUtil extends org.phoebus.ui.javafx.JFXUtil
3334
{
3435
private static double font_calibration = 1.0;
36+
private static final DecimalFormat RGBA_ALPHA_DECIMAL_FORMAT = new DecimalFormat("0.00");
3537

3638
static
3739
{
@@ -88,12 +90,20 @@ public static String webRgbOrHex(final WidgetColor color)
8890
return "rgba(" + col.getRed() + ',' +
8991
col.getGreen() + ',' +
9092
col.getBlue() + ',' +
91-
col.getAlpha()/255f + ')';
93+
RGBA_ALPHA_DECIMAL_FORMAT.format(col.getAlpha()/255f) + ')';
9294
else
9395
return webHex(col);
9496
});
9597
}
9698

99+
/**
100+
* Extract alpha value, formatted as expected for e.g. an RGBA function
101+
* @return alpha value in decimal form, from 0.00 to 1.00
102+
*/
103+
public static String webAlpha(final WidgetColor color) {
104+
return RGBA_ALPHA_DECIMAL_FORMAT.format(color.getAlpha()/255f);
105+
}
106+
97107
/** Convert model color into web-type RGB text
98108
* @param buf StringBuilder where RGB text of the form "#FF8080" is added
99109
* @param color {@link WidgetColor}

app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/ComboRepresentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public void updateChanges()
266266
Font f = JFXUtil.convert(model_widget.propFont().getValue());
267267

268268
jfx_node.setStyle(MessageFormat.format(
269-
"-fx-body-color: linear-gradient(to bottom,ladder({0}, derive({0},8%) 75%, derive({0},10%) 80%), derive({0},-8%)); "
269+
"-fx-background-color: linear-gradient(to bottom,ladder({0}, derive({0},8%) 75%, derive({0},10%) 80%), derive({0},-8%)); "
270270
+ "-fx-text-base-color: ladder(-fx-color, -fx-light-text-color 45%, -fx-dark-text-color 46%, -fx-dark-text-color 59%, {1}); "
271271
+ "-fx-font: {2} {3}px \"{4}\";",
272272
JFXUtil.webRgbOrHex(model_widget.propBackgroundColor().getValue()),

app/display/representation-javafx/src/main/java/org/csstudio/display/builder/representation/javafx/widgets/ProgressBarRepresentation.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected void registerListeners()
4949
{
5050
super.registerListeners();
5151
model_widget.propFillColor().addUntypedPropertyListener(lookChangedListener);
52+
model_widget.propBackgroundColor().addUntypedPropertyListener(lookChangedListener);
5253
model_widget.propWidth().addUntypedPropertyListener(lookChangedListener);
5354
model_widget.propHeight().addUntypedPropertyListener(lookChangedListener);
5455
model_widget.propLimitsFromPV().addUntypedPropertyListener(valueChangedListener);
@@ -64,6 +65,7 @@ protected void registerListeners()
6465
protected void unregisterListeners()
6566
{
6667
model_widget.propFillColor().removePropertyListener(lookChangedListener);
68+
model_widget.propBackgroundColor().removePropertyListener(lookChangedListener);
6769
model_widget.propWidth().removePropertyListener(lookChangedListener);
6870
model_widget.propHeight().removePropertyListener(lookChangedListener);
6971
model_widget.propLimitsFromPV().removePropertyListener(valueChangedListener);
@@ -181,8 +183,36 @@ public void updateChanges()
181183
// Tweaking the color used by CSS keeps overall style.
182184
// See also http://stackoverflow.com/questions/13467259/javafx-how-to-change-progressbar-color-dynamically
183185
final StringBuilder style = new StringBuilder();
184-
style.append("-fx-accent: ").append(JFXUtil.webRgbOrHex(model_widget.propFillColor().getValue())).append(';');
185-
style.append("-fx-control-inner-background: ").append(JFXUtil.webRgbOrHex(model_widget.propBackgroundColor().getValue())).append(';');
186+
187+
// Color of the progress bar / foreground
188+
style.append("-fx-accent: ").append(JFXUtil.webRGB(
189+
JFXUtil.convert(
190+
model_widget.propFillColor().getValue()
191+
)
192+
)).append(" !important; ");
193+
194+
// Color of the background underneath the progress bar
195+
// Note per moderna.css the background is actually three layers of color
196+
// with fx-shadow-highlight-color on the bottom,
197+
// then fx-text-box-border,
198+
// and finally fx-control-inner-background on top, all stacked in place with offsets.
199+
// This gives the illusion of having a bordered box with a shadow instead of actually being a
200+
// bordered box with a shadow...
201+
// Fortunately, the bottom-most color (the 'shadow') is already transparent so we can leave it alone
202+
// Unfortunately, the middle color (the "border" color) is a solid gray color (#ececec), so we must
203+
// override it with its rgba equivalent so that it has transparency matching the picked background color.
204+
style.append("-fx-control-inner-background: ")
205+
.append(JFXUtil.webRGB(
206+
JFXUtil.convert(
207+
model_widget.propBackgroundColor().getValue()))
208+
)
209+
.append(";");
210+
style.append("-fx-text-box-border: rgba(236, 236, 236, ")
211+
.append(JFXUtil.webAlpha(model_widget.propBackgroundColor().getValue()))
212+
.append(");");
213+
style.append("-fx-shadow-highlight-color: rgba(236, 236, 236, ")
214+
.append(JFXUtil.webAlpha(model_widget.propBackgroundColor().getValue()))
215+
.append(");");
186216
jfx_node.setStyle(style.toString());
187217
}
188218
if (dirty_value.checkAndClear())

app/display/representation-javafx/src/main/resources/org/csstudio/display/builder/representation/javafx/opibuilder.css

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,15 @@
237237
{
238238
-db-toggle-switch-off: #f5f5f5;
239239
-db-toggle-switch-on: #0b99c9;
240+
-db-transparent-text-box-border: rgba(236, 236, 236, 0.50);
240241
}
241242
.toggle-switch .thumb-area
242243
{
243244
-fx-background-radius: 1em;
244245
-fx-background-color: linear-gradient(
245246
to bottom,
246-
derive(-fx-text-box-border, -20%),
247-
derive(-fx-text-box-border, -30%)
247+
derive(-db-transparent-text-box-border, -20%),
248+
derive(-db-transparent-text-box-border, -30%)
248249
),
249250
-db-toggle-switch-off;
250251
-fx-background-insets: 0, 1;
@@ -254,8 +255,8 @@
254255
{
255256
-fx-background-color: linear-gradient(
256257
to bottom,
257-
derive(-fx-text-box-border, -20%),
258-
derive(-fx-text-box-border, -30%)
258+
derive(-db-transparent-text-box-border, -20%),
259+
derive(-db-transparent-text-box-border, -30%)
259260
),
260261
linear-gradient(
261262
to bottom,

app/display/representation-javafx/src/test/java/org/csstudio/display/builder/representation/javafx/JFXUtilTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@ public void testRGB()
3838
{
3939
assertThat(JFXUtil.webRgbOrHex(new WidgetColor(15, 255, 0)), equalTo("#0FFF00"));
4040
assertThat(JFXUtil.webRgbOrHex(new WidgetColor(0, 16, 255)), equalTo("#0010FF"));
41-
assertThat(JFXUtil.webRgbOrHex(new WidgetColor(0, 16, 255, 50)), equalTo("rgba(0,16,255,0.19607843)"));
41+
assertThat(JFXUtil.webRgbOrHex(new WidgetColor(0, 16, 255, 50)), equalTo("rgba(0,16,255,0.20)"));
42+
}
43+
44+
@Test
45+
public void testRGBWithAlpha()
46+
{
47+
// NOTE that the actual decimal value for transparency would have been 0.019607844, however it needs
48+
// to be formatted to two decimal places
49+
assertThat(JFXUtil.webRgbOrHex(new WidgetColor(15, 255, 0, 5)), equalTo("rgba(15,255,0,0.02)"));
50+
assertThat(JFXUtil.webRgbOrHex(new WidgetColor(0, 16, 255)), equalTo("#0010FF"));
4251
}
4352

4453
@Test

0 commit comments

Comments
 (0)