Created
September 17, 2021 16:15
-
-
Save wess/416a239c470463c7dbf3f19c829f08dd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// color.dart | |
// foundation | |
// | |
// Author: Wess Cope ([email protected]) | |
// Created: 09/16/2021 | |
// | |
// Copywrite (c) 2021 Wess.io | |
// | |
import 'package:flutter/material.dart'; | |
Color _colorFromHex(String hex) { | |
hex = hex.replaceAll('#', ''); | |
switch(hex.length) { | |
case 3: | |
hex = "FF" + hex + hex; | |
break; | |
case 6: | |
hex = "FF" + hex; | |
break; | |
case 8: | |
break; | |
default: | |
throw Exception("Color hex code can only be 3, 6 or 8 characters long (not counting #)"); | |
} | |
return Color( | |
int.parse("0x$hex") | |
); | |
} | |
extension ColorExt on Color { | |
static Color from(String hexString) { | |
return _colorFromHex(hexString); | |
} | |
} | |
extension StringColorExt on String { | |
toColor() { | |
return _colorFromHex(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment