Last active
July 11, 2022 11:03
-
-
Save StanislawNagorski/2d13e1259a4270d308d02821dbf42866 to your computer and use it in GitHub Desktop.
Color parser to and from #hex String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:ui'; | |
//creates hexString like #C6DEE5 | |
extension ColorExtension on Color{ | |
String toHexString() { | |
final hex = value.toRadixString(16).toUpperCase(); | |
return hex.replaceFirst('FF','#'); | |
} | |
} | |
//creates Color from hexString | |
extension StringExtension on String { | |
Color toColorFromHexString() { | |
final hexString = this; | |
final buffer = StringBuffer(); | |
if (hexString.length == 6 || hexString.length == 7) buffer.write('FF'); | |
buffer.write(hexString.replaceFirst('#', '')); | |
return Color(int.parse(buffer.toString(), radix: 16)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment