Skip to content

Fix: Preserve numeric conversion accuracy in ColorSliderTextFieldConfiguration#11

Merged
noppefoxwolf merged 1 commit intonoppefoxwolf:mainfrom
isontheline:fix-preserve-numeric-conversion
Sep 21, 2025
Merged

Fix: Preserve numeric conversion accuracy in ColorSliderTextFieldConfiguration#11
noppefoxwolf merged 1 commit intonoppefoxwolf:mainfrom
isontheline:fix-preserve-numeric-conversion

Conversation

@isontheline
Copy link
Copy Markdown
Contributor

Hey @noppefoxwolf 👋

Thank you so much for your awesome ColorPicker library 🙏

I'm encountering an issue (isontheline/pro.webssh.net#1412) where user can't put some RGB values.

Do you think you can apply this PR on your main branch?
Have you a better way to fix it?

Have a great day ☀️

===========

Problem

When converting text input back and forth between String ↔︎ Double ↔︎ String, floating-point precision errors caused off-by-one truncations.
Example:

  • User enters 96 (percent mode)
  • Internal value becomes 0.96
  • Converting back with Int(value * 100) yields 95.9999… → truncated to 95

This resulted in unexpected value loss.

Solution

Applied rounding before casting to Int inside all valueToText closures (hex, radius, percent):

let v = (value * scale).rounded()
return "\(Int(v))"

Additionally, values are clamped to the valid ranges (0...255, 0...360, 0...100) to avoid accidental out-of-range results.

Impact

  • Fixes off-by-one conversion errors (e.g., 96 remains 96)
  • Improves reliability and user trust in the text-to-slider synchronization
  • No breaking changes to API

Testing

  • Verified with values that previously failed (e.g., 96, 255, 360, 100).
  • Round-trip conversion now returns the original value consistently.

…ation`

Fixes isontheline/pro.webssh.net#1412

When converting text input back and forth between `String` ↔︎ `Double` ↔︎ `String`, floating-point precision errors caused off-by-one truncations.
@noppefoxwolf
Copy link
Copy Markdown
Owner

@isontheline Thank you for your PR.
Looks good to me.

@noppefoxwolf noppefoxwolf merged commit 0899a6c into noppefoxwolf:main Sep 21, 2025
@isontheline isontheline deleted the fix-preserve-numeric-conversion branch November 10, 2025 20:54
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