Skip to content

fix(node): color loop direction default and hue wrap - #5074

Open
Apollon77 wants to merge 3 commits into
mainfrom
fix/color-loop-direction-default
Open

Apollon77 wants to merge 3 commits into
mainfrom
fix/color-loop-direction-default

Conversation

@Apollon77

@Apollon77 Apollon77 commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Three defects in how ColorControlServer moves the hue.

  • Wrong color loop direction default. colorLoopDirection defaults to Increment. Matter 1.4.2 removed the default from the specification, and the server took over the old model defaults as its own State values. For this attribute it took Increment, but the old model default was 0, which is Decrement. The CHIP SDK all-clusters app also uses 0x00. The attribute is nonvolatile, so devices that already stored a value keep it; only new nodes start with Decrement.
  • Hue move does not start at a bound. Transitions.start returned without starting when a property sat at min moving down or at max moving up. For the cyclic hue properties this is wrong: MoveHue, EnhancedMoveHue and the color loop run until stopped and wrap around ("If the hue reaches the minimum allowed for the device, then wraparound and proceed from its maximum allowed value"). With the Decrement default, the first color loop of a new device starting at hue 0 did not move at all.
  • Timer transitions end early. A transition that Transitions runs with its timer ended one step (100 ms) before the requested time, because the first step already applied two step intervals of change. It also ended as soon as the published, rounded value equaled the target, so a move by one unit, such as MoveToHue from 254 up to 0, ended after half its time.
  • Hue wrap skips a value. addValueWithOverflow and the hue distance calculations treated min and max as one value, so a cycle had 254 steps for the 255 hue values 0 to 254. Going down from 0 gave 253 instead of 254. The specification continues from the maximum, so max and min are neighbors, not the same value.

Fix

The cyclic range now includes both min and max everywhere: addValueWithOverflow, the default Transitions.calculateCyclicDistance and the Down, Shortest and Longest distances in ColorControlServer. A cyclic value between steps is fractional and can lie between max and max + 1, so the rounded value that goes into state is wrapped too.

ColorControlServer passed its own calculateCyclicDistance for MoveToHue and StepHue, which computed the same distance as the Transitions default because the sign of the rate is the chosen direction. It now uses the default, so the range rule exists once in Transitions and once in the hue distance that chooses the direction.

The timer now ends a transition the way the CHIP SDK does, on time instead of on the rounded value: the first step applies one step interval, and the step ends the transition when the value reaches the target. Less than half a millisecond of change left counts as reached, because adding up the steps leaves rounding errors. The end is reported after the step that wrote the final value has committed, so a client never sees a remaining time of zero before the final value. A $Changed event still ends a transition when its value is the target and the timer did not publish it, which keeps the integration where hardware updates the state. A report at a full second now carries the value of the step before it, 100 ms older than the report time.

addValueWithOverflow is exported from @matter/general, and its result changes for every input that wraps (8 + 5 in 0..10 gives 2 instead of 3). No caller outside Transitions and ColorControlServer exists in the repository. The change is listed as a fix because the old result contradicted the specification's wrap rule.

A StepHue with a StepSize of 255 is a full turn and now ends at the start hue without a transition. Before, it moved by one. Both differ from a full turn over the transition time; this case is left as is.

🤖 Generated with Claude Code

Apollon77 and others added 2 commits November 28, 2026 18:39
- ColorControlServer defaults colorLoopDirection to Decrement, the old
  model default and the CHIP SDK default.
- Transitions starts a cyclic transition without a target at its min or
  max, so MoveHue, EnhancedMoveHue and the color loop start at hue 0
  moving down or at the highest hue moving up.
- The cyclic range includes both min and max: addValueWithOverflow, the
  default calculateCyclicDistance, the hue distances and the rounded
  state value no longer skip a value at the wrap. ColorControlServer uses
  the Transitions default distance instead of its own copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 28, 2026 17:39
@mergify

mergify Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

An upward hue transition from 254 to 0 can finish halfway through its requested duration due to rounding, requiring a completion-condition fix and a slow boundary-transition regression test.

1 open issue
What changed in this PR

This PR corrects hue movement in ColorControlServer and aligns shared cyclic arithmetic with inclusive minimum and maximum values.

Changes:

  • Default new color loops to Decrement while preserving stored direction values.
  • Allow cyclic movement to start at bounds and wrap without skipping hue values.
  • Add regression tests and changelog entries for the corrected behavior.

Build, lint, formatting checks, and tests were not run in this review environment.

File Description
packages/​node/​test/​behaviors/​color-control/​ColorControlServerTest.ts Tests direction defaults, wrap paths, and movement from bounds.
packages/​node/​test/​behavior/​cluster/​ClusterBehaviorFeaturesTest.ts Updates the expected color-loop direction default.
packages/​node/​src/​behaviors/​color-control/​ColorControlServer.ts Corrects direction defaults and inclusive hue distances.
packages/​node/​src/​behavior/​Transitions.ts Updates cyclic distance, boundary starts, and rounded-state wrapping.
packages/​general/​test/​util/​NumberTest.ts Tests inclusive wrapping, offset ranges, and multiple cycles.
packages/​general/​src/​util/​Number.ts Implements inclusive cyclic addition using modulo arithmetic.
CHANGELOG.md Documents color-control and public numeric utility fixes.

Review effort: Balanced

- The first step applies one step interval, not two, so a transition no
  longer ends one step early.
- The timer step ends a transition when the value reaches the target,
  with half a millisecond of change as tolerance for rounding errors,
  and only after the step's final value is committed.
- The $Changed observer ends a transition only for values the timer did
  not publish, so a rounded value equal to the target no longer ends a
  transition early; hardware updates still end it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Transitions can report completion before committed state reaches the target, while persistent cached values can suppress external feedback and prevent transitions from finishing.

2 open issues
1 resolved since last review

Review effort: Balanced

Comment on lines +615 to +617
if (this.#propertyStates[state.name] === state) {
this.finish(state.name);
}

if (newValue === targetValue) {
// A value the timer published is rounded, so only the timer step ends its own transition
if (newValue === targetValue && newValue !== this.#publishedValues.get(name)) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants