Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@KaranUnique
Copy link
Contributor

@KaranUnique KaranUnique commented Oct 6, 2025

What is the previous behavior before this PR?

KaTeX only supported 3-digit (#RGB), 6-digit (#RRGGBB), and named colors (red, blue, etc.). 8-digit hex colors with alpha transparency (#RRGGBBAA) were rejected by the parser with "Invalid color" errors.

Example that failed before:

\textcolor{#228B2280}{F=ma}  % Error: Invalid color '#228B2280'

What is the new behavior after this PR?

KaTeX now accepts and processes 8-digit hex colors with alpha transparency in both #RRGGBBAA and RRGGBBAA formats. The alpha channel is preserved and passed through to CSS for browser rendering.

Examples that work after:
\textcolor{#FF000080}{F=ma} % 50% transparent red
\textcolor{#228B2280}{F=ma} % 50% transparent green
\textcolor{00FF0040}{F=ma} % 25% opaque green (no # prefix)

The parser now:

Validates 8-digit hex color format
Automatically adds # prefix when missing for 8-digit colors
Maintains full backward compatibility with existing color formats
Outputs standard CSS #RRGGBBAA format for modern browser alpha support

Fixes #4067

Copy link
Member

@edemaine edemaine left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! I have a few comments.

src/Parser.js Outdated
return null;
}
const match = (/^(#[a-f0-9]{3}|#?[a-f0-9]{6}|[a-z]+)$/i).exec(res.text);
const match = (/^(#[a-f0-9]{3}|#?[a-f0-9]{6}|#?[a-f0-9]{8}|[a-z]+)$/i).exec(res.text);
Copy link
Member

Choose a reason for hiding this comment

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

There's also 4-digit hex codes. See https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color

Is there some way to avoid the repetition here? I'm not sure.

- Add 4-digit hex color support (#RGBA)
- Remove 8-digit color support without # prefix
- Optimize regex pattern for better readability
- Remove redundant parsing tests
- Inline invalid color test cases
@KaranUnique
Copy link
Contributor Author

Hi @edemaine,

Thanks for the feedback! I’ve made the requested changes:

1.Added support for 4-digit hex colors.
2.Removed support for 8-digit hex values without a leading #.
3.Optimized the regex to avoid repetition.
4.Updated tests: removed redundant parsing tests, inlined invalid color cases, and added a 4-digit alpha color test.

I’ve pushed the updates and opened a new pull request with these fixes. Please take a look when you get a chance.

@edemaine edemaine changed the title feat: support 8-digit hex colors with alpha (#4067) feat: support hex colors with alpha (#4067) Oct 9, 2025
@edemaine edemaine changed the title feat: support hex colors with alpha (#4067) feat: support hex colors with alpha Oct 9, 2025
@edemaine edemaine enabled auto-merge (squash) October 9, 2025 14:50
auto-merge was automatically disabled October 9, 2025 15:29

Pull Request is not mergeable

@edemaine
Copy link
Member

edemaine commented Oct 9, 2025

Tests are failing; could you take a look?

  • The regex line is too long (lint)
  • There's an old test that says \textcolor{#fA6f}{x} should fail. This needs to be modified or removed.

- Split regex across lines to meet 84-character limit
- Update badCustomColorExpression2 from #fA6f to #fA6f1 (5-digit invalid)
- Ensure all tests pass and linting requirements are met
@KaranUnique
Copy link
Contributor Author

fix: resolve linting and test issues

  1. Break long regex line to meet 84-character limit
  2. Fix failing test by changing #fA6f to #fA6f1 (4-digit colors now valid)

@KaranUnique
Copy link
Contributor Author

Hi @edemaine,

Thanks for approving the changes! I can see that all the screenshotter tests are passing and the deploy preview looks good.

Could you please approve the 2 pending workflows so that the testExpected check can run? Once that passes, auto-merge should trigger automatically.

@edemaine edemaine merged commit 8c9b306 into KaTeX:main Oct 12, 2025
8 checks passed
KaTeX-bot added a commit that referenced this pull request Oct 12, 2025
## [0.16.24](v0.16.23...v0.16.24) (2025-10-12)

### Features

* support hex colors with alpha ([#4090](#4090)) ([8c9b306](8c9b306)), closes [#4067](#4067) [#fA6](https://github.com/KaTeX/KaTeX/issues/fA6) [#fA6f1](https://github.com/KaTeX/KaTeX/issues/fA6f1)
@KaTeX-bot
Copy link
Member

πŸŽ‰ This PR is included in version 0.16.24 πŸŽ‰

The release is available on:

Your semantic-release bot πŸ“¦πŸš€

@KaranUnique
Copy link
Contributor Author

Hi @edemaine,

Thanks for merging the PR! 😊
I was participating in Hacktoberfest β€” could you please confirm if this repository counts, or label it as hacktoberfest-accepted if needed?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Alpha in Hex Colors

3 participants