fix(color): round channel bytes in default color hex serialization - #9185
Open
Pcmhacker-piro wants to merge 1 commit into
Open
Pcmhacker-piro wants to merge 1 commit into
Pcmhacker-piro wants to merge 1 commit into
Conversation
Contributor
Author
|
Hi @limzykenneth (Color area steward) and @ksen0! 👋 Could you please take a look at this PR when you get a chance? It addresses issue #9124 by updating
Thank you so much! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #9124
Changes:
In p5.js 2.x,
toHexComponentinsrc/color/p5.Color.jsused bitwise NOT truncation (const vInt = ~~(v * 255);) to convert normalized channel values to hex bytes.Because truncation drops the fractional part toward zero:
colorMode(RGB, 100); color(50, 0, 0), where0.5 * 255 = 127.5) was truncated to127(#7f0000) instead of128(#800000).c.toString()(whichp5.Renderer2Dfeeds into canvasctx.fillStyleandctx.strokeStyle) disagreed withc.toString('#rrggbb')on the exact same color object, because the#rrggbbformatting path goes throughcolorjs.ioserialization and rounds to the nearest integer.This PR:
toHexComponentinsrc/color/p5.Color.jsto useMath.round(v * 255).test/unit/color/p5.Color.jsverifying that defaultc.toString()rounds fractional channel values correctly across custom color mode ranges (RGB, 100), standard255range (127.5), and alpha channels (127.5), ensuring strict consistency withc.toString('#rrggbb').Verification:
1. Animated Demo Video (Terminal & Test Run)
2. Terminal Output & Verification
PR Checklist
npm run lintpasses