Skip to content

Instantly share code, notes, and snippets.

@wess
Created September 17, 2021 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wess/416a239c470463c7dbf3f19c829f08dd to your computer and use it in GitHub Desktop.
Save wess/416a239c470463c7dbf3f19c829f08dd to your computer and use it in GitHub Desktop.
//
// color.dart
// foundation
//
// Author: Wess Cope (me@wess.io)
// 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