Skip to content

fix: round Color components to the nearest byte in toNumber and toHex - #12400

Open
SwastikTripathi wants to merge 1 commit into
pixijs:devfrom
SwastikTripathi:fix/color-round-int
Open

SwastikTripathi wants to merge 1 commit into
pixijs:devfrom
SwastikTripathi:fix/color-round-int

Conversation

@SwastikTripathi

Copy link
Copy Markdown
Description of change

Color converts a component to a byte two different ways. toUint8RgbArray(), toRgbaString() and toBgrNumber() round, but the cached integer behind toNumber(), toHex(), toHexa(), toLittleEndianNumber() and toPremultiplied() truncates ((r * 255) << 16). Any component that isn't an exact multiple of 1/255 gives two different colors:

const color = new Color([0.5, 0.25, 0.75]);

color.toRgbaString();         // 'rgba(128,64,191,1)'
color.toHex();                // '#7f3fbf' (expected '#8040bf')
color.toBgrNumber();          // 0xbf4080
color.toLittleEndianNumber(); // 0xbf3f7f, should equal toBgrNumber()

This shows up in rendering. A container tint goes through toBgrNumber() (rounded), while a Graphics fill goes through toNumber() and the canvas renderer through toHex() (both truncated). So the same color is one step darker on a filled shape than on a tinted sprite, and differs between the WebGL/WebGPU and canvas renderers. The toHex() docs also say "Values are rounded to nearest hex value".

_refreshInt() now uses Math.round, like toUint8RgbArray(). Exact byte values (numbers, hex strings, CSS colors) are unchanged, since v / 255 * 255 already lands on v. I checked all 256 values with the components stored as a Float32Array.

The multiply test expected 0x3f3f3f for 0.25. 0.25 * 255 = 63.75, which rounds to 0x40, so it now expects 0x404040. The new test checks that every output agrees for components between two bytes. Both fail before the change.

Pre-Merge Checklist
  • Tests and/or benchmarks are included
  • Lint process passed (npm run lint)
  • Tests passed (npm run test)

I ran src/color with Jest in a plain Node environment, since the Electron runner isn't set up on my machine, plus eslint and tsc -p .configs/tsconfig.build.json. CI will run the full suite.

@pkg-pr-new

pkg-pr-new Bot commented Nov 14, 2026

Copy link
Copy Markdown

commit: a26c8fd

This branch has not been deployed

No deployments
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