color method

Color color(
  1. dynamic c
)

Parses a color, so a string representing a color becomes a color.

Parameters: string: a string of the specified color. Returns: color Example: color("#aaa"); Output: #aaa

Implementation

Color color(dynamic c) {
  final re = RegExp(r'^#([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3,4})$',
      caseSensitive: false);

  if (c is Quoted && re.hasMatch(c.value)) {
    final val = c.value.substring(1);
    return Color(val, null, '#$val');
  }

  final _c = (c is Color)
      ? c
      : Color.fromKeyword(c.value); //c.value must be a color name
  if (_c != null) {
    _c.value = null;
    return _c;
  }
  throw LessExceptionError(LessError(
      type: 'Argument',
      message:
          'argument must be a color keyword or 3|4|6|8 digit hex e.g. #FFF'));

// 3.8.0 20180729
//  color: function(c) {
//      if ((c instanceof Quoted) &&
//          (/^#([A-Fa-f0-9]{8}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3,4})$/i.test(c.value))) {
//          var val = c.value.slice(1);
//          return new Color(val, undefined, '#' + val);
//      }
//      if ((c instanceof Color) || (c = Color.fromKeyword(c.value))) {
//          c.value = undefined;
//          return c;
//      }
//      throw {
//          type:    'Argument',
//          message: 'argument must be a color keyword or 3|4|6|8 digit hex e.g. #FFF'
//      };
//  },
}