Mohrail Alfy
Mohrail Alfy

Reputation: 11

Convert from Hexa value to color name in Flutter

is there a way to convert from Hex value to the color name using any package in flutter?

Upvotes: 1

Views: 1310

Answers (2)

Nabin Dhakal
Nabin Dhakal

Reputation: 2202

You can achieve without using any pacakges.You can convert hex color as follows:

 class HexColor extends Color {
  static int _getColorFromHex(String hexColor) {
    hexColor = hexColor.toUpperCase().replaceAll("#", "");
    if (hexColor.length == 6) {
      hexColor = "FF" + hexColor;
    }
    return int.parse(hexColor, radix: 16);
  }

  HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}

Then access as:

 static Color cardColor = HexColor('#D4AA3A');

Upvotes: -1

Jared
Jared

Reputation: 189

There is this package called color_convert 1.0.2 that should help? However, it only takes color names found on a GitHub list from what I understand. So I don't think it can be used for a wide range of colors (the GitHub list has around 147 colors in RGB which I think can be converted to HexCode and then Text), but you could certainly take this color converter and work off of it to build your own for more specific colors. As of current I don't think there is a package that does exactly what you are looking for, but this is a good start.

Upvotes: 1

Related Questions