parse static method

CSSColorHEX? parse(
  1. String color
)
override

Implementation

static CSSColorHEX? parse(String color) {
  var match = patternHex.firstMatch(color);
  if (match == null) return null;

  var hex = match.group(1);
  hex ??= match.group(2);

  if (hex!.length == 3) {
    var r = hex.substring(0, 1);
    var g = hex.substring(1, 2);
    var b = hex.substring(2, 3);

    var nR = _parseHex('$r$r');
    var nG = _parseHex('$g$g');
    var nB = _parseHex('$b$b');

    return CSSColorHEX._(nR, nG, nB);
  } else if (hex.length == 6) {
    var r = hex.substring(0, 2);
    var g = hex.substring(2, 4);
    var b = hex.substring(4, 6);

    var nR = _parseHex(r);
    var nG = _parseHex(g);
    var nB = _parseHex(b);

    return CSSColorHEX._(nR, nG, nB);
  } else if (hex.length == 8) {
    var r = hex.substring(0, 2);
    var g = hex.substring(2, 4);
    var b = hex.substring(4, 6);
    var a = hex.substring(6, 8);

    var nR = _parseHex(r);
    var nG = _parseHex(g);
    var nB = _parseHex(b);
    var nA = _parseHex(a);

    var alpha = nA / 255;

    return CSSColorHEXAlpha._(nR, nG, nB, alpha);
  }

  return null;
}