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 13, 2023
1 parent 3dd8e6f commit 2725545
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
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.items.ColorItem;
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 +354,18 @@ protected String processColor(Widget w, String originalSnippet) {
String color = "";
String snippet = originalSnippet;

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

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

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

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

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

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

if (color != null) {
style = "style=\"color:" + color + "\"";
Expand All @@ -379,15 +393,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 String itemRGBHexCode) {
if ("primary".equals(color)) {
return PRIMARY_COLOR;
} else if ("secondary".equals(color)) {
return SECONDARY_COLOR;
} else if ("itemValue".equals(color) && itemRGBHexCode != null) {
return itemRGBHexCode;
}
return color;
}

public @Nullable String getRGBHexCode(@Nullable Item item) {
if (item instanceof ColorItem && item.getState() instanceof HSBType) {
HSBType hsbState = (HSBType) item.getState();
return "#" + Integer.toHexString(hsbState.getRGB()).substring(2);
}
return null;
}

protected @Nullable String getCategory(Widget w) {
return itemUIRegistry.getCategory(w);
}
Expand Down

0 comments on commit 2725545

Please sign in to comment.