Visitar URL original
Fix alpha channel parsing in hex colors (#172) · flutter/packages@84a6550 · GitHub
Skip to content

Commit 84a6550

Browse files
authored
Fix alpha channel parsing in hex colors (#172)
1 parent 96e65c6 commit 84a6550

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

‎packages/vector_graphics_compiler/lib/src/svg/parser.dart‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,14 +1369,13 @@ class SvgParser {
13691369
final String b = colorString[3];
13701370
colorString = '#$r$r$g$g$b$b';
13711371
}
1372-
int color = int.parse(colorString.substring(1), radix: 16);
13731372

1374-
if (colorString.length == 7) {
1375-
return Color(color |= 0xFF000000);
1376-
}
1377-
1378-
if (colorString.length == 9) {
1379-
return Color(color);
1373+
if (colorString.length == 7 || colorString.length == 9) {
1374+
final int color = int.parse(colorString.substring(1, 7), radix: 16);
1375+
final int alpha = colorString.length == 9
1376+
? int.parse(colorString.substring(7, 9), radix: 16)
1377+
: 255;
1378+
return Color(color | alpha << 24);
13801379
}
13811380
}
13821381

‎packages/vector_graphics_compiler/test/parsers_test.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ void main() {
2222
expect(parser.parseColor('null', attributeName: 'foo', id: null), null);
2323
expect(parser.parseColor('red', attributeName: 'foo', id: null),
2424
const Color.fromARGB(255, 255, 0, 0));
25+
expect(parser.parseColor('#ABCDEF', attributeName: 'foo', id: null),
26+
const Color.fromARGB(255, 0xAB, 0xCD, 0xEF));
27+
// RGBA in svg/css, ARGB in this library.
28+
expect(parser.parseColor('#ABCDEF88', attributeName: 'foo', id: null),
29+
const Color.fromARGB(0x88, 0xAB, 0xCD, 0xEF));
2530
});
2631

2732
test('Colors - mapped', () async {

0 commit comments

Comments
 (0)