-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[vector_graphics_compiler] Fix rgb/rgba color parsing to support modern CSS syntax #10538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1372,21 +1372,40 @@ class SvgParser { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // handle rgba() colors e.g. rgba(255, 255, 255, 1.0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (colorString.toLowerCase().startsWith('rgba')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final List<String> rawColorElements = colorString | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // handle rgba() colors e.g. rgb(255, 255, 255) and rgba(255, 255, 255, 1.0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/rgb | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (colorString.toLowerCase().startsWith('rgba') || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| colorString.toLowerCase().startsWith('rgb')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final List<int> rgba = colorString | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .substring(colorString.indexOf('(') + 1, colorString.indexOf(')')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .split(',') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .split(RegExp(r'[,/\s]')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((String rawColor) => rawColor.trim()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .where((e) => e.isNotEmpty) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .indexed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1385
to
+1401
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of the custom To ensure stricter adherence to the CSS standard, you should use
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final double opacity = parseDouble(rawColorElements.removeLast())!; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final List<int> rgb = rawColorElements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((String rawColor) => int.parse(rawColor)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (rgba.length == 3) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rgba.add(255); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Color.fromRGBO(rgb[0], rgb[1], rgb[2], opacity); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Color.fromARGB(rgba[3], rgba[0], rgba[1], rgba[2]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Conversion code from: https://github.com/MichaelFenwick/Color, thanks :) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1456,26 +1475,6 @@ class SvgParser { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // handle rgb() colors e.g. rgb(255, 255, 255) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (colorString.toLowerCase().startsWith('rgb')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final List<int> rgb = colorString | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .substring(colorString.indexOf('(') + 1, colorString.indexOf(')')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .split(',') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((String rawColor) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rawColor = rawColor.trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (rawColor.endsWith('%')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rawColor = rawColor.substring(0, rawColor.length - 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (parseDouble(rawColor)! * 2.55).round(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return int.parse(rawColor); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .toList(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // rgba() isn't really in the spec, but Firefox supported it at one point so why not. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final int a = rgb.length > 3 ? rgb[3] : 255; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return Color.fromARGB(a, rgb[0], rgb[1], rgb[2]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // handle named colors ('red', 'green', etc.). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final Color? namedColor = namedColors[colorString]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (namedColor != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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)