Skip to content

Commit

Permalink
[BasicUI] Handle new color keyword "itemValue"
Browse files Browse the repository at this point in the history
Depends on openhab/openhab-core#3453

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Mar 14, 2023
1 parent 3dd8e6f commit 1889701
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.openhab.core.i18n.I18nUtil;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.types.State;
Expand Down Expand Up @@ -350,8 +353,18 @@ protected String processColor(Widget w, String originalSnippet) {
String color = "";
String snippet = originalSnippet;

State itemState = null;
if (w.getItem() != null) {
try {
Item item = itemUIRegistry.getItem(w.getItem());
itemState = item.getState();
} catch (ItemNotFoundException e) {
logger.debug("{}", e.getMessage());
}
}

color = itemUIRegistry.getLabelColor(w);
color = applyPrimaryOrSecondaryColor(color);
color = convertSpecialColors(color, itemState);

if (color != null) {
style = "style=\"color:" + color + "\"";
Expand All @@ -360,7 +373,7 @@ protected String processColor(Widget w, String originalSnippet) {

style = "";
color = itemUIRegistry.getValueColor(w);
color = applyPrimaryOrSecondaryColor(color);
color = convertSpecialColors(color, itemState);

if (color != null) {
style = "style=\"color:" + color + "\"";
Expand All @@ -369,7 +382,7 @@ protected String processColor(Widget w, String originalSnippet) {

style = "";
color = itemUIRegistry.getIconColor(w);
color = applyPrimaryOrSecondaryColor(color);
color = convertSpecialColors(color, itemState);

if (color != null) {
style = "style=\"color:" + color + "\"";
Expand All @@ -379,15 +392,25 @@ protected String processColor(Widget w, String originalSnippet) {
return snippet;
}

private @Nullable String applyPrimaryOrSecondaryColor(@Nullable String color) {
private @Nullable String convertSpecialColors(@Nullable String color, @Nullable State itemState) {
if ("primary".equals(color)) {
return PRIMARY_COLOR;
} else if ("secondary".equals(color)) {
return SECONDARY_COLOR;
} else if ("itemValue".equals(color)) {
return getRGBHexCodeFromItemState(itemState);
}
return color;
}

protected @Nullable String getRGBHexCodeFromItemState(@Nullable State itemState) {
if (itemState instanceof HSBType) {
HSBType hsbState = (HSBType) itemState;
return "#" + Integer.toHexString(hsbState.getRGB()).substring(2);
}
return null;
}

protected @Nullable String getCategory(Widget w) {
return itemUIRegistry.getCategory(w);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.model.sitemap.sitemap.Colorpicker;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.types.State;
Expand Down Expand Up @@ -68,10 +67,9 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th

// get RGB hex value
State state = itemUIRegistry.getState(cp);
String hexValue = "#ffffff";
if (state instanceof HSBType) {
HSBType hsbState = (HSBType) state;
hexValue = "#" + Integer.toHexString(hsbState.getRGB()).substring(2);
String hexValue = getRGBHexCodeFromItemState(state);
if (hexValue == null) {
hexValue = "#ffffff";
}
String purelabel = itemUIRegistry.getLabel(w);
if (purelabel != null) {
Expand Down

0 comments on commit 1889701

Please sign in to comment.