Skip to content

[system] rgbToHex() returns invalid hex for decimal or out-of-range channels #49755

Done

Description

@PANJA-ABHISHEK

Steps to reproduce

This is a pure function, so it can be reproduced without a live example:

import { rgbToHex } from '@mui/system';

console.log(rgbToHex('rgb(127.5, 0, 0)'));
console.log(rgbToHex('rgb(300, 0, 0)'));
  1. Run the code above.

Current behavior

The first call returns #7f.80000 and the second returns #12c0000. Neither is a valid hex color.

rgbToHex() calls toString(16) on the channel values as they are parsed, but CSS allows decimal and out-of-range rgb channels.

Expected behavior

#800000 and #ff0000. Channels should be rounded and clamped to 0-255 before converting, the same way browsers resolve these colors.

Context

I found this while working on #49753. I have a fix with regression tests ready and will open a PR.

Your environment

npx @mui/envinfo
  System:
    OS: Windows 11 10.0.26200
  Binaries:
    Node: 26.13.0
    pnpm: 12.18.0
  Browsers:
    Chrome: 156.0.8078.80
  npmPackages:
    @mui/system: master (dbf007c340)
    react: 19.3.1
    react-dom: 19.3.1

Not browser specific. Reproduced in Node and Chromium.

Search keywords: rgbToHex hex decimal colorManipulator

Activity

  1. added
    status: waiting for maintainer
    on Nov 10, 2026
  2. added
    scope: system
    on Nov 17, 2026
  3. added
    type: bug
    and removed
    status: waiting for maintainer
    on Nov 24, 2026
  4. github-actions commented on Nov 24, 2026

    @github-actions
    Contributor

    This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
    Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.


    @PANJA-ABHISHEK Thanks again for creating this issue! If you have a moment, we would love to hear your thoughts on how we handled it with this short feedback form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    scope: systemtype: bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions