Skip to content

Conversation

@AlexDochioiu
Copy link

@AlexDochioiu AlexDochioiu commented Nov 30, 2025

  • Consolidate rgb() and rgba() parsing into single implementation
  • Add support for space-separated color values
  • Add support for percentage-based RGB values
  • Add support for slash (/) separator before alpha channel
  • Handle both comma and space delimiters
  • Add comprehensive test coverage for various color formats

This PR solves flutter/flutter#179261

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2 3

@github-actions github-actions bot added p: vector_graphics triage-engine Should be looked at in engine triage labels Nov 30, 2025
Copy link

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

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 refactors the color parsing logic for rgb() and rgba() to support modern CSS syntax, including space-separated values, percentage-based values, and the slash separator for the alpha channel. The implementation is consolidated, and the old rgb parsing logic is removed. Comprehensive tests have been added to cover the new parsing capabilities.

The changes are well-structured and the added tests are thorough. I have one suggestion to improve the strictness of the parsing to better align with CSS standards.

Comment on lines +1385 to +1401
.map((indexedColor) {
var (index, rawColor) = indexedColor;
if (rawColor.endsWith('%')) {
rawColor = rawColor.substring(0, rawColor.length - 1);
return (parseDouble(rawColor)! * 2.55).round();
}
if (index == 3) {
// if alpha is not percentage, it means it's a double between 0 and 1
final double opacity = parseDouble(rawColor)!;
if (opacity < 0 || opacity > 1) {
throw StateError('Invalid "opacity": $opacity');
}
return (opacity * 255).round();
}
// If rgb is not percentage, it means it's an integer between 0 and 255
return int.parse(rawColor);
})

Choose a reason for hiding this comment

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

medium

The use of the custom parseDouble from numbers.dart is not ideal for parsing color components. This function is designed to strip units like px or em, but according to the CSS specification, color components in rgb() or rgba() functions should not have units. This could lead to incorrect parsing of invalid color strings (e.g., rgb(50px, 0, 0) would be parsed as rgb(50, 0, 0)).

To ensure stricter adherence to the CSS standard, you should use double.parse from dart:core instead, which will correctly throw an error for values with units.

Suggested change
.map((indexedColor) {
var (index, rawColor) = indexedColor;
if (rawColor.endsWith('%')) {
rawColor = rawColor.substring(0, rawColor.length - 1);
return (parseDouble(rawColor)! * 2.55).round();
}
if (index == 3) {
// if alpha is not percentage, it means it's a double between 0 and 1
final double opacity = parseDouble(rawColor)!;
if (opacity < 0 || opacity > 1) {
throw StateError('Invalid "opacity": $opacity');
}
return (opacity * 255).round();
}
// If rgb is not percentage, it means it's an integer between 0 and 255
return int.parse(rawColor);
})
.map((indexedColor) {
var (index, rawColor) = indexedColor;
if (rawColor.endsWith('%')) {
rawColor = rawColor.substring(0, rawColor.length - 1);
return (double.parse(rawColor) * 2.55).round();
}
if (index == 3) {
// if alpha is not percentage, it means it's a double between 0 and 1
final double opacity = double.parse(rawColor);
if (opacity < 0 || opacity > 1) {
throw StateError('Invalid "opacity": $opacity');
}
return (opacity * 255).round();
}
// If rgb is not percentage, it means it's an integer between 0 and 255
return int.parse(rawColor);
})

Copy link
Author

Choose a reason for hiding this comment

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

This parseDouble usage was retained from the existing rbg parsing implementation.

final List<int> rgba = colorString
.substring(colorString.indexOf('(') + 1, colorString.indexOf(')'))
.split(',')
.split(RegExp(r'[,/\s]'))
Copy link
Author

Choose a reason for hiding this comment

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

valid delimiters are: ,, (empty space) and / (used to delimit the opacity)

@stuartmorgan-g
Copy link
Collaborator

Thanks for the contribution! Once you've completed the checklist, please mark the PR as ready for review.

Please make sure to follow the linked style guide.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft November 30, 2025 15:53
@AlexDochioiu AlexDochioiu marked this pull request as ready for review December 1, 2025 07:53
@AlexDochioiu AlexDochioiu marked this pull request as draft December 1, 2025 07:53
…rn CSS syntax

- Consolidate rgb() and rgba() parsing into single implementation
- Add support for space-separated color values
- Add support for percentage-based RGB values
- Add support for slash (/) separator before alpha channel
- Handle both comma and space delimiters
- Add comprehensive test coverage for various color formats
@AlexDochioiu AlexDochioiu marked this pull request as ready for review December 1, 2025 08:10
@AlexDochioiu
Copy link
Author

@stuartmorgan-g Completed the checklist. This is my first PR to any flutter/dart repo. If anything is not exactly correct, please let me know and I'll address.

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

Labels

p: vector_graphics triage-engine Should be looked at in engine triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants