-
Notifications
You must be signed in to change notification settings - Fork 1
Bugfix/154 colorstate comparison fails when comparing with null values #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DivineThreepwood
wants to merge
3
commits into
dev
Choose a base branch
from
bugfix/154-colorstate-comparison-fails-when-comparing-with-null-values
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -169,21 +169,94 @@ static Boolean equalServiceStates(final ColorState colorStateA, final ColorState | |
| final HSBColor hsbColorA = colorStateA.getColor().getHsbColor(); | ||
| final HSBColor hsbColorB = colorStateB.getColor().getHsbColor(); | ||
|
|
||
| // Helper: compare hues with wrap-around (0 == 360) | ||
| // margin in degrees | ||
| final double HUE_MARGIN = 1.0; | ||
| final double SATURATION_MARGIN = 0.01; | ||
| final double BRIGHTNESS_MARGIN = 0.01; | ||
|
|
||
| boolean hueEquals = true; | ||
| boolean saturationEquals = true; | ||
| boolean brightnessEquals = true; | ||
| // normalize angle to [0,360) | ||
| java.util.function.DoubleUnaryOperator normalize = (v) -> { | ||
| double r = v % 360.0; | ||
| if (r < 0) r += 360.0; | ||
| return r; | ||
| }; | ||
|
|
||
| if(hsbColorA.hasHue() && hsbColorB.hasHue()) { | ||
| hueEquals = OperationService.equals(hsbColorA.getHue(), hsbColorB.getHue(), 1.0); | ||
| java.util.function.BiPredicate<Double, Double> hueEqualsWithWrap = (ha, hb) -> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here as well |
||
| double aNorm = normalize.applyAsDouble(ha); | ||
| double bNorm = normalize.applyAsDouble(hb); | ||
| double diff = Math.abs(aNorm - bNorm); | ||
| if (diff > 180.0) { | ||
| diff = 360.0 - diff; // shortest distance on circle | ||
| } | ||
| return diff <= HUE_MARGIN; | ||
| }; | ||
|
|
||
| boolean hueEquals; | ||
| if (hsbColorA.hasHue() && hsbColorB.hasHue()) { | ||
| hueEquals = hueEqualsWithWrap.test(hsbColorA.getHue(), hsbColorB.getHue()); | ||
| } else if (!hsbColorA.hasHue() && !hsbColorB.hasHue()) { | ||
| // both undefined -> treat as equal | ||
| hueEquals = true; | ||
| } else { | ||
| // one missing: if the present color is 'neutral' (saturation == 0 or brightness == 0 or both undefined) the hue is irrelevant | ||
| final HSBColor present = hsbColorA.hasHue() ? hsbColorA : hsbColorB; | ||
| boolean presentIsNeutral = false; | ||
| // If both saturation and brightness are missing, treat as neutral (no chroma information) | ||
| if (!present.hasSaturation() && !present.hasBrightness()) { | ||
| presentIsNeutral = true; | ||
| } | ||
| // If saturation is present and effectively 0 -> neutral | ||
| if (!presentIsNeutral && present.hasSaturation()) { | ||
| presentIsNeutral = OperationService.equals(present.getSaturation(), 0d, SATURATION_MARGIN); | ||
| } | ||
| // If not neutral yet and brightness is present and effectively 0 -> neutral | ||
| if (!presentIsNeutral && present.hasBrightness()) { | ||
| presentIsNeutral = OperationService.equals(present.getBrightness(), 0d, BRIGHTNESS_MARGIN); | ||
| } | ||
| hueEquals = presentIsNeutral; | ||
| } | ||
|
|
||
| if(hsbColorA.hasSaturation() && hsbColorB.hasSaturation()) { | ||
| saturationEquals = OperationService.equals(hsbColorA.getSaturation(), hsbColorB.getSaturation(), 0.01); | ||
| boolean saturationEquals; | ||
| if (hsbColorA.hasSaturation() && hsbColorB.hasSaturation()) { | ||
| saturationEquals = OperationService.equals(hsbColorA.getSaturation(), hsbColorB.getSaturation(), SATURATION_MARGIN); | ||
| } else if (!hsbColorA.hasSaturation() && !hsbColorB.hasSaturation()) { | ||
| saturationEquals = true; | ||
| } else { | ||
| // one missing: consider equal if the present saturation is effectively 0 (neutral) | ||
| // or if the present has no saturation but brightness is present and 0 -> neutral | ||
| final HSBColor present = hsbColorA.hasSaturation() ? hsbColorA : hsbColorB; | ||
| boolean presentIsNeutral = false; | ||
| if (present.hasSaturation()) { | ||
| presentIsNeutral = OperationService.equals(present.getSaturation(), 0d, SATURATION_MARGIN); | ||
| } else if (present.hasBrightness()) { | ||
| presentIsNeutral = OperationService.equals(present.getBrightness(), 0d, BRIGHTNESS_MARGIN); | ||
| } else { | ||
| // no saturation and no brightness information -> treat as neutral | ||
| presentIsNeutral = true; | ||
| } | ||
| saturationEquals = presentIsNeutral; | ||
| } | ||
|
|
||
| if(hsbColorA.hasBrightness() && hsbColorB.hasBrightness()) { | ||
| brightnessEquals = OperationService.equals(hsbColorA.getBrightness(), hsbColorB.getBrightness(), 0.01); | ||
| boolean brightnessEquals; | ||
| if (hsbColorA.hasBrightness() && hsbColorB.hasBrightness()) { | ||
| brightnessEquals = OperationService.equals(hsbColorA.getBrightness(), hsbColorB.getBrightness(), BRIGHTNESS_MARGIN); | ||
| } else if (!hsbColorA.hasBrightness() && !hsbColorB.hasBrightness()) { | ||
| brightnessEquals = true; | ||
| } else { | ||
| // one missing: consider equal if the present brightness is effectively 0 (off/neutral) | ||
| // or if the present has no brightness but saturation is present and 0 -> neutral | ||
| final HSBColor present = hsbColorA.hasBrightness() ? hsbColorA : hsbColorB; | ||
| boolean presentIsNeutral = false; | ||
| if (present.hasBrightness()) { | ||
| presentIsNeutral = OperationService.equals(present.getBrightness(), 0d, BRIGHTNESS_MARGIN); | ||
| } else if (present.hasSaturation()) { | ||
| presentIsNeutral = OperationService.equals(present.getSaturation(), 0d, SATURATION_MARGIN); | ||
| } else { | ||
| // no brightness and no saturation information -> treat as neutral | ||
| presentIsNeutral = true; | ||
| } | ||
| brightnessEquals = presentIsNeutral; | ||
| } | ||
|
|
||
| return hueEquals && saturationEquals && brightnessEquals; | ||
|
|
||
56 changes: 0 additions & 56 deletions
56
...t/java/org/openbase/bco/dal/lib/layer/service/provider/ColorStateProviderServiceTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we import DoubleUnaryOperator?