Skip to content
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

Add ColorUtil.xyToDuv #4401

Merged
merged 4 commits into from
Oct 7, 2024
Merged

Add ColorUtil.xyToDuv #4401

merged 4 commits into from
Oct 7, 2024

Conversation

ccutrer
Copy link
Contributor

@ccutrer ccutrer commented Oct 3, 2024

This adds a new helper method to calculate the Duv (Delta u, v) of a color. The code is taken almost literally from the mqtt.espmilighthub binding, but changed slightly to fit in with other functions in ColorUtil. I intend to use this from espmilighthub, but also the HomeKit add-on. HomeKit doesn't allow linking both HSB and Color Temperature characteristics to a single accessory, even though the iOS Home App will present a Color Temperature picker for any HSB light. So I want to allow linking devices that support both RGB+CCT to HomeKit, and if the Duv is within threshold, send a command to the Color Temperature channel instead.

I also think it might be useful to add a profile to apply to a Color channel link that does the same thing - if a color is close enough to "white", send a command to the color temperature item instead. This would cover all other use cases, such as BasicUI and MainUI.

This can also be useful for user rules dealing with RGB+CCT light.

Signed-off-by: Cody Cutrer <[email protected]>
@ccutrer ccutrer requested a review from a team as a code owner October 3, 2024 16:32
and only accept xy, not xyY, to match xyToKelvin (the function that is most
closely related to this one)

Signed-off-by: Cody Cutrer <[email protected]>
@holgerfriedrich holgerfriedrich added the enhancement An enhancement or new feature of the Core label Oct 6, 2024
Signed-off-by: Cody Cutrer <[email protected]>
Copy link
Member

@holgerfriedrich holgerfriedrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking again at this PR, I am asking myself why it is necessary to use BigDecimal for the computations - especially using MathContext.DECIMAL128 which ensures that at least 34 digits are used.
I did a quick rewrite to double, and the tests still passed.

So is there a reason to use the computationally much more demanding BigDecimals? Are you aware of any instability caused by special input values, etc.?

I added my implementation as comments, please feel free to resolve those conversations without change in case you have a good reason to go for the BigDecimals.

@ccutrer
Copy link
Contributor Author

ccutrer commented Oct 7, 2024

Nope, no good reason. I suspect the original code started out as float (single precision), and there were accuracy issues, and so it jumped all the way to BigDecimal when double would have been plenty sufficient.

@holgerfriedrich
Copy link
Member

Nope, no good reason. I suspect the original code started out as float (single precision), and there were accuracy issues, and so it jumped all the way to BigDecimal when double would have been plenty sufficient.

Then I would prefer the implementation based on double. It has way better performance and is easier to read.

Maybe you could remove all unused BigDecimal constants as well.
Thanks!

Copy link
Member

@holgerfriedrich holgerfriedrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@holgerfriedrich holgerfriedrich merged commit ea5480d into openhab:main Oct 7, 2024
5 checks passed
@holgerfriedrich holgerfriedrich added this to the 4.3 milestone Oct 7, 2024
@ccutrer ccutrer deleted the color-util-duv branch October 7, 2024 22:09
@andrewfg
Copy link
Contributor

andrewfg commented Oct 8, 2024

@ccutrer for the esp light bulb you may want to look at my recently merged PR in the Zigbee binding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature of the Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants