Browse Source

Allow for opacity fallback keys.

pull/189/head
weisj 5 years ago
parent
commit
f4b31f7e04
  1. 65
      property-loader/src/main/java/com/github/weisj/darklaf/icons/IconColorMapper.java

65
property-loader/src/main/java/com/github/weisj/darklaf/icons/IconColorMapper.java

@ -87,10 +87,11 @@ public final class IconColorMapper {
if (child instanceof LinearGradient) { if (child instanceof LinearGradient) {
LinearGradient grad = (LinearGradient) child; LinearGradient grad = (LinearGradient) child;
String id = grad.getId(); String id = grad.getId();
StyleAttribute fallbacks = getFallbacks(grad); StyleAttribute colorFallbacks = getAttribute("fallback", grad);
StyleAttribute opacityFallbacks = getAttribute("opacity-fallback", grad);
String opacityKey = getOpacityKey(grad); String opacityKey = getOpacityKey(grad);
float opacity = getOpacity(opacityKey, defaults); float opacity = getOpacity(opacityKey, getFallbacks(opacityFallbacks), defaults);
float opacity1 = opacity; float opacity1 = opacity;
float opacity2 = opacity; float opacity2 = opacity;
if (opacity < 0) { if (opacity < 0) {
@ -113,8 +114,9 @@ public final class IconColorMapper {
if (opacity2 < 0) opacity2 = opacity; if (opacity2 < 0) opacity2 = opacity;
} }
Color c = resolveColor(id, getFallbacks(fallbacks), FALLBACK_COLOR, defaults); Color c = resolveColor(id, getFallbacks(colorFallbacks), FALLBACK_COLOR, defaults);
Pair<LinearGradient, Runnable> result = createColor(c, id, opacityKey, fallbacks, opacity1, opacity2); Pair<LinearGradient, Runnable> result = createColor(c, id, opacityKey, colorFallbacks, opacity1,
opacity2);
LinearGradient gradient = result.getFirst(); LinearGradient gradient = result.getFirst();
Runnable finalizer = result.getSecond(); Runnable finalizer = result.getSecond();
themedDefs.loaderAddChild(null, gradient); themedDefs.loaderAddChild(null, gradient);
@ -125,32 +127,30 @@ public final class IconColorMapper {
public static float getOpacity(final LinearGradient gradient, final Map<Object, Object> propertyMap) { public static float getOpacity(final LinearGradient gradient, final Map<Object, Object> propertyMap) {
String opacityKey = getOpacityKey(gradient); String opacityKey = getOpacityKey(gradient);
return getOpacity(opacityKey, propertyMap); return getOpacity(opacityKey, null, propertyMap);
} }
public static Color getColor(final LinearGradient gradient, final Map<Object, Object> propertyMap) { public static Color getColor(final LinearGradient gradient, final Map<Object, Object> propertyMap) {
String id = (gradient).getId(); String id = (gradient).getId();
StyleAttribute fallbacks = getFallbacks(gradient); StyleAttribute fallbacks = getAttribute("fallback", gradient);
return resolveColor(id, getFallbacks(fallbacks), FALLBACK_COLOR, propertyMap); return resolveColor(id, getFallbacks(fallbacks), FALLBACK_COLOR, propertyMap);
} }
private static Color resolveColor(final String key, final String[] fallbacks, private static Color resolveColor(final String key, final String[] fallbacks,
final Color fallbackColor, final Map<Object, Object> propertyMap) { final Color fallbackColor, final Map<Object, Object> propertyMap) {
Object color = propertyMap.get(key); Color color = get(propertyMap, key, fallbacks, Color.class);
for (int i = 0; i < fallbacks.length && !(color instanceof Color); i++) {
color = propertyMap.get(fallbacks[i]); if (color == null) {
}
if (!(color instanceof Color)) {
color = fallbackColor; color = fallbackColor;
LOGGER.warning("Could not load color with id '" + key + "' fallbacks" + Arrays.toString(fallbacks) LOGGER.warning("Could not load color with id '" + key + "' fallbacks" + Arrays.toString(fallbacks)
+ ". Using color '" + fallbackColor + "' instead."); + ". Using color '" + fallbackColor + "' instead.");
} }
return (Color) color; return color;
} }
private static StyleAttribute getFallbacks(final LinearGradient child) { private static StyleAttribute getAttribute(final String key, final LinearGradient child) {
StyleAttribute attribute = new StyleAttribute(); StyleAttribute attribute = new StyleAttribute();
attribute.setName("fallback"); attribute.setName(key);
try { try {
child.getStyle(attribute); child.getStyle(attribute);
} catch (SVGException e) { } catch (SVGException e) {
@ -175,17 +175,17 @@ public final class IconColorMapper {
return fallbacks.getStringList(); return fallbacks.getStringList();
} }
private static float getOpacity(final String key, final Map<Object, Object> propertyMap) { private static float getOpacity(final String key, final String[] fallbacks, final Map<Object, Object> propertyMap) {
// UIManager defaults to 0, if the values isn't an integer (or null). // UIManager defaults to 0, if the value isn't an integer (or null).
Object obj = propertyMap.get(key); Number obj = get(propertyMap, key, fallbacks, Number.class);
if (obj instanceof Integer) { if (obj instanceof Integer) {
return ((Integer) obj) / 100.0f; return obj.intValue() / 100.0f;
} else if (obj instanceof Long) { } else if (obj instanceof Long) {
return ((Long) obj) / 100.0f; return obj.intValue() / 100.0f;
} else if (obj instanceof Float) { } else if (obj instanceof Float) {
return (Float) obj; return obj.floatValue();
} else if (obj instanceof Double) { } else if (obj instanceof Double) {
return ((Double) obj).floatValue(); return obj.floatValue();
} }
if (key != null && !key.isEmpty()) { if (key != null && !key.isEmpty()) {
LOGGER.warning(obj + " is an invalid opacity value. Key = '" + key + "'"); LOGGER.warning(obj + " is an invalid opacity value. Key = '" + key + "'");
@ -242,6 +242,29 @@ public final class IconColorMapper {
}); });
} }
private static <T> T get(final Map<Object, Object> map, final Object key, final Class<T> type) {
return getFromMap(map, key, null, type);
}
private static <T> T get(final Map<Object, Object> map, final Object key, final Object[] fallbacks,
final Class<T> type) {
T obj = getFromMap(map, key, fallbacks, type);
if (obj == null) return getFromMap(UIManager.getDefaults(), key, fallbacks, type);
return obj;
}
private static <T> T getFromMap(final Map<Object, Object> map, final Object key, final Object[] fallbacks,
final Class<T> type) {
Object obj = map.get(key);
if (fallbacks != null) {
for (int i = 0; i < fallbacks.length && !type.isInstance(obj); i++) {
obj = map.get(fallbacks[i]);
}
}
if (type.isInstance(obj)) return type.cast(obj);
return null;
}
private static String toHexString(final Color color) { private static String toHexString(final Color color) {
return "#" + ColorUtil.toHex(color); return "#" + ColorUtil.toHex(color);
} }

Loading…
Cancel
Save