Skip to content

Commit d26d1bc

Browse files
committed
Refactored the rgba hex conversion into a reusable method.
1 parent 18de25e commit d26d1bc

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/src/hex_color.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ extension HexColor on Color {
99
return Color(int.parse(buffer.toString(), radix: 16));
1010
}
1111

12+
/// Converts an rgba value (0-255) into a 2-digit Hex code.
13+
String _hexValue(int rgbaVal) {
14+
assert(rgbaVal == rgbaVal & 0xFF);
15+
return rgbaVal.toRadixString(16).padLeft(2, '0').toUpperCase();
16+
}
17+
1218
/// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
1319
String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
14-
'${alpha.toRadixString(16).padLeft(2, '0').toUpperCase()}'
15-
'${red.toRadixString(16).padLeft(2, '0').toUpperCase()}'
16-
'${green.toRadixString(16).padLeft(2, '0').toUpperCase()}'
17-
'${blue.toRadixString(16).padLeft(2, '0').toUpperCase()}';
20+
'${_hexValue(alpha)}${_hexValue(red)}${_hexValue(green)}${_hexValue(blue)}';
1821
}

0 commit comments

Comments
 (0)