Home > Back-end >  Convert from Hexa value to color name in Flutter
Convert from Hexa value to color name in Flutter

Time:02-26

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

CodePudding user response:

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.

CodePudding user response:

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');
  • Related