Skip to content

[csslib] Fix Rgba/Hsla ARGB conversions and keep exact alpha - #2826

Merged
kevmoo merged 4 commits into
dart-lang:mainfrom
palmoni5:csslib/color-conversions
Nov 16, 2026
Merged

kevmoo merged 4 commits into
dart-lang:mainfrom
palmoni5:csslib/color-conversions

Conversation

@palmoni5

Copy link
Copy Markdown
Contributor

Fixes several channel and alpha bugs in csslib's color conversion helpers:

  • Rgba.fromArgbValue read the alpha byte as red and shifted the other channels (0x804080c0 gave r == 128).
  • Rgba.argbValue truncated fractional alpha to 0 before encoding it (a: 0.5 became 0x00).
  • Hsla.color passed 0..1 HSL fractions to Color.createHsla, which expects degrees and percentages, so every color came out near black (Hsla(0, 1, .5).color was ff010101).
  • Hsla.fromRgba dropped alpha for grayscale colors.
  • Color.rgba, Color.hsla and Hsla.fromArgbValue rounded alpha to two digits, so ARGB round-trips were lossy (0x7f became 0x80).

Rgba.fromString and Hsla.fromString now parse the CSS color once instead of re-parsing the already converted ARGB string (harmless today, but wrong as soon as #rrggbbaa is read as CSS specifies). Color.css now throws a FormatException for unsupported colors instead of a null-check error.

Behavior note: cssExpression now prints the exact alpha (e.g. 0.5019607843137255 instead of 0.5).

Tests: test/color_conversion_test.dart covers each case; all of them fail without the change.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request improves color handling in csslib by ensuring exact 8-bit alpha precision is preserved across Color, Rgba, and Hsla conversions, throwing a FormatException for unsupported CSS colors, and fixing alpha preservation for grayscale colors. The review feedback highlights critical cross-platform issues on Web/JS targets where bitwise operations treat numbers as signed 32-bit integers. To prevent incorrect negative values or clamping when the alpha channel is 0x80 or greater, the reviewer recommends masking bitwise operations with & 0xffffffff in Rgba.fromArgbValue, Rgba.argbValue, and Hsla.fromArgbValue, and adding corresponding test cases to prevent regressions.

Comment thread pkgs/csslib/lib/src/property.dart Resolved
Comment thread pkgs/csslib/lib/src/property.dart Resolved
Comment thread pkgs/csslib/lib/src/property.dart Resolved
Comment thread pkgs/csslib/CHANGELOG.md Resolved
Comment thread pkgs/csslib/lib/src/property.dart Resolved
Comment thread pkgs/csslib/lib/src/property.dart Resolved
Comment thread pkgs/csslib/lib/src/property.dart Resolved
palmoni5 added a commit to Otzaria/tools that referenced this pull request Nov 14, 2026
Replays the review fixes pushed to the dart-lang/tools topic branches
(dart-lang#2826-dart-lang#2836, dart-lang#2838, dart-lang#2842, dart-lang#2843, dart-lang#2845, dart-lang#2846, dart-lang#2850, dart-lang#2852, dart-lang#2853,
dart-lang#2857, dart-lang#2862, dart-lang#2863) onto the integration branch:

- csslib: round converted alpha to two digits again, drop rgb()
  percentages and non-degree hue units, drop the Color.rebeccaPurple
  constant, keep sharing the family list in Font.scale, throw
  FormatException for unknown color functions, remove unreachable
  comment branches, and trim tests and benchmarks.
- html: reuse isWhitespaceCC, drop the classes.contains override,
  simplify :nth-child, and trim tests. The :empty/:blank fix stays here
  until dart-lang#2734 lands.
- markdown: drop the redundant autolink early return and two tests.

CHANGELOG sections are rebuilt from the topic branches' entries.
# Conflicts:
#	pkgs/csslib/CHANGELOG.md
@kevmoo
kevmoo merged commit d53e092 into dart-lang:main Nov 16, 2026
3 checks passed
copybara-service Bot pushed a commit to dart-lang/sdk that referenced this pull request Nov 17, 2026
Revisions updated by `dart tools/rev_sdk_deps.dart`.

http (https://github.com/dart-lang/http/compare/e81e6fe..e319115):
  e319115  Mon Nov 16 12:31:18 2026 -0800  Brian Quinlan  chore(http2): release 3.2.0 (dart-lang/http#2043)

shelf (https://github.com/dart-lang/shelf/compare/b175e9f..2856b69):
  2856b69  Mon Nov 16 15:08:37 2026 -0800  Kevin Moore  test(shelf_static): verify sub-second file mtime on If-Modified-Since (dart-lang/shelf#560)

tools (https://github.com/dart-lang/tools/compare/1d77f5d..e97f625):
  e97f62582  Tue Nov 17 01:12:37 2026 +0200  palmoni5  [markdown] Split fenced code info strings at a tab as well as a space (dart-lang/tools#2861)
  8b4bd9035  Tue Nov 17 01:12:08 2026 +0200  palmoni5  [markdown] Scan whitespace directly for blank lines and dedent (dart-lang/tools#2856)
  0ccdf5a2f  Tue Nov 17 01:11:40 2026 +0200  palmoni5  [markdown] Fast-path single-line text in HtmlRenderer and reset state on reuse (dart-lang/tools#2857)
  9ccd1f06f  Tue Nov 17 01:11:10 2026 +0200  palmoni5  [markdown] Keep first-reference order for footnotes with non-ASCII labels (dart-lang/tools#2862)
  1611e7840  Tue Nov 17 01:10:41 2026 +0200  palmoni5  [markdown] Don't double-encode percent escapes in extended autolinks (dart-lang/tools#2863)
  91de4afe0  Tue Nov 17 01:09:24 2026 +0200  palmoni5  [html] Avoid duplicate code-unit buffers and serialization copies (dart-lang/tools#2848)
  6ae9b3661  Tue Nov 17 01:08:47 2026 +0200  palmoni5  [html] Extract DOM text iteratively to avoid stack overflows (dart-lang/tools#2853)
  9ae835ddf  Tue Nov 17 01:08:18 2026 +0200  palmoni5  [html] Keep parent links consistent and move node lists in linear time (dart-lang/tools#2849)
  514a1441a  Tue Nov 17 01:07:48 2026 +0200  palmoni5  [html] Look up elements by ID, tag and class without the CSS parser (dart-lang/tools#2850)
  4d5fd4800  Tue Nov 17 01:07:17 2026 +0200  palmoni5  [html] Fix structural selectors and split class values on ASCII whitespace (dart-lang/tools#2852)
  c4545deae  Tue Nov 17 00:56:28 2026 +0200  palmoni5  [csslib] Index keyword tables by length in TokenKind matchers (dart-lang/tools#2835)
  f04ce9c23  Tue Nov 17 00:56:01 2026 +0200  palmoni5  [csslib] Avoid per-character allocations when tokenizing identifiers (dart-lang/tools#2836)
  bb9fa57b0  Tue Nov 17 00:55:33 2026 +0200  palmoni5  [csslib] Skip comments and whitespace without recursion (dart-lang/tools#2838)
  017d0c369  Tue Nov 17 00:55:04 2026 +0200  palmoni5  [csslib] Keep line height and family list in Font.scale (dart-lang/tools#2845)
  f38c2b28f  Tue Nov 17 00:54:35 2026 +0200  palmoni5  [csslib] Serialize partial Font styles as valid CSS (dart-lang/tools#2847)
  29d0ed107  Tue Nov 17 00:54:06 2026 +0200  palmoni5  [csslib] Hash all Font fields compared by == (dart-lang/tools#2846)
  b0d3be20f  Tue Nov 17 00:53:37 2026 +0200  palmoni5  [csslib] Parse CSS hsl() angles and percentages in Color.css (dart-lang/tools#2829)
  d53e0929a  Tue Nov 17 00:53:09 2026 +0200  palmoni5  [csslib] Fix Rgba/Hsla ARGB conversions and keep exact alpha (dart-lang/tools#2826)
  b81adb5fe  Tue Nov 17 00:52:40 2026 +0200  palmoni5  [csslib] Keep numeric Color alpha and normalize CSS hex colors (dart-lang/tools#2828)
  2a4c12867  Tue Nov 17 00:52:11 2026 +0200  palmoni5  [csslib] Simplify TokenKind.decimalToHex and stop hanging on negatives (dart-lang/tools#2831)
  65f097ca3  Tue Nov 17 00:51:43 2026 +0200  palmoni5  [csslib] Index the named color table for color lookups (dart-lang/tools#2832)
  0ba8cf932  Tue Nov 17 00:51:15 2026 +0200  palmoni5  [csslib] Recognize the rebeccapurple color keyword (dart-lang/tools#2834)
  c745e413e  Tue Nov 17 00:50:47 2026 +0200  palmoni5  [csslib] Parse unit-like type selectors at the start of a rule (dart-lang/tools#2830)
  59f7f0698  Tue Nov 17 00:50:20 2026 +0200  palmoni5  [csslib] Emit valid @namespace directives in the pretty printer (dart-lang/tools#2827)
  a7a5db527  Tue Nov 17 00:49:49 2026 +0200  palmoni5  [csslib] Deep-clone selectors and keep legacy pseudo-elements (dart-lang/tools#2842)
  c66db3c58  Tue Nov 17 00:49:21 2026 +0200  palmoni5  [csslib] Keep group terms and declaration flags when cloning (dart-lang/tools#2843)
  484417860  Tue Nov 17 00:48:54 2026 +0200  palmoni5  [csslib] Fix the @left-bottom page-margin directive (dart-lang/tools#2833)

web is held back at 4e60e61, before dart-lang/web#596, because it changes
`package:web` signatures (e.g. `innerHTML=`, `parseFromString`, `Worker()`)
from `JSAny` to `String` in ways that break SDK and google3 callers (see
reverted roll in fe6e88e).

Change-Id: I42ad0ebf774fac1acdf6a411aea26001d8093d4f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/578343
Reviewed-by: Kevin Moore <kevmoo@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Nate Bosch <nbosch@google.com>
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.

2 participants