Skip to content
Snippets Groups Projects
Commit 41902a6e authored by René Schöne's avatar René Schöne
Browse files

Make TupleHSB immutable.

parent 1200d613
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,7 @@ aspect ItemHandling { ...@@ -26,7 +26,7 @@ aspect ItemHandling {
// TupleHSB and String work like default // TupleHSB and String work like default
eq ColorItem.getStateAsDouble() { eq ColorItem.getStateAsDouble() {
logger.warn("getStateAsDouble called on item " + getLabel() + ". Using brightness."); logger.warn("getStateAsDouble called on item " + getLabel() + ". Using brightness.");
return getState().brightness; return getState().getBrightness();
} }
eq DateTimeItem.getStateAsDouble() = getState().getTime(); eq DateTimeItem.getStateAsDouble() = getState().getTime();
eq ItemWithBooleanState.getStateAsDouble() = getState() ? 1 : 0; eq ItemWithBooleanState.getStateAsDouble() = getState() ? 1 : 0;
...@@ -87,7 +87,7 @@ aspect ItemHandling { ...@@ -87,7 +87,7 @@ aspect ItemHandling {
public abstract void Item.setStateFromLong(long value); public abstract void Item.setStateFromLong(long value);
public void ColorItem.setStateFromLong(long value) { public void ColorItem.setStateFromLong(long value) {
// only set brightness // only set brightness
this.setState(TupleHSB.of(getState().hue, getState().saturation, Math.toIntExact(value))); this.setState(getState().withDifferentBrightness(Math.toIntExact(value)));
} }
public void DateTimeItem.setStateFromLong(long value) { public void DateTimeItem.setStateFromLong(long value) {
this.setState(new Date(value)); this.setState(new Date(value));
...@@ -167,10 +167,10 @@ aspect ItemHandling { ...@@ -167,10 +167,10 @@ aspect ItemHandling {
logger.warn("Ignoring color update using {} for {}", this, value); logger.warn("Ignoring color update using {} for {}", this, value);
} }
public void ItemWithBooleanState.setStateFromColor(TupleHSB value) { public void ItemWithBooleanState.setStateFromColor(TupleHSB value) {
this.setState(value != null && value.brightness > 0); this.setState(value != null && value.getBrightness() > 0);
} }
public void ItemWithDoubleState.setStateFromColor(TupleHSB value) { public void ItemWithDoubleState.setStateFromColor(TupleHSB value) {
this.setState(value.brightness); this.setState(value.getBrightness());
} }
public void ItemWithStringState.setStateFromColor(TupleHSB value) { public void ItemWithStringState.setStateFromColor(TupleHSB value) {
this.setState(value.toString()); this.setState(value.toString());
...@@ -358,9 +358,7 @@ aspect ItemHandling { ...@@ -358,9 +358,7 @@ aspect ItemHandling {
} }
private void ColorItem.setBrightness(int value) { private void ColorItem.setBrightness(int value) {
TupleHSB newState = getState().clone(); setState(getState().withDifferentBrightness(value));
newState.brightness = value;
setState(newState);
} }
//--- ItemPreference.apply --- //--- ItemPreference.apply ---
......
...@@ -8,9 +8,9 @@ import java.util.Objects; ...@@ -8,9 +8,9 @@ import java.util.Objects;
* @author rschoene - Initial contribution * @author rschoene - Initial contribution
*/ */
public class TupleHSB implements Cloneable { public class TupleHSB implements Cloneable {
public int hue; private int hue;
public int saturation; private int saturation;
public int brightness; private int brightness;
public static TupleHSB of(int hue, int saturation, int brightness) { public static TupleHSB of(int hue, int saturation, int brightness) {
TupleHSB result = new TupleHSB(); TupleHSB result = new TupleHSB();
result.hue = hue; result.hue = hue;
...@@ -19,6 +19,30 @@ public class TupleHSB implements Cloneable { ...@@ -19,6 +19,30 @@ public class TupleHSB implements Cloneable {
return result; return result;
} }
public int getHue() {
return hue;
}
public int getSaturation() {
return saturation;
}
public int getBrightness() {
return brightness;
}
public TupleHSB withDifferentHue(int hue) {
return TupleHSB.of(hue, this.saturation, this.brightness);
}
public TupleHSB withDifferentSaturation(int saturation) {
return TupleHSB.of(this.hue, saturation, this.brightness);
}
public TupleHSB withDifferentBrightness(int brightness) {
return TupleHSB.of(this.hue, this.saturation, brightness);
}
public String toString() { public String toString() {
return String.format("%s,%s,%s", hue, saturation, brightness); return String.format("%s,%s,%s", hue, saturation, brightness);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment