hex2rgb static method

List<num> hex2rgb(
  1. dynamic arg
)

Implementation

static List<num> hex2rgb(dynamic arg) {
  if (arg is num || arg is int) {
    int intval = arg.floor();
    num r = (intval >> 16) & 0xFF;
    num g = (intval >> 8) & 0xFF;
    num b = intval & 0xFF;

    return [r, g, b];
  }
  var str = arg.toString();
  var hexstuff = RegExp('[a-f0-9]{6}|[a-f0-9]{3}', caseSensitive: false);
  var match = hexstuff.firstMatch(str);
  if (match == null) {
    return [0, 0, 0];
  }

  var colorString = match[0]!;

  if (colorString.length == 3) {
    colorString = colorString.split('').map((ch) {
      return ch + ch;
    }).join('');
  }
  int intval = int.parse(colorString, radix: 16);

  num r = (intval >> 16) & 0xFF;
  num g = (intval >> 8) & 0xFF;
  num b = intval & 0xFF;

  return [r, g, b];
}