from static method

CSSColor? from(
  1. Object? color
)
override

Implementation

static CSSColor? from(Object? color) {
  if (color == null) return null;

  if (color is List) {
    if (color.length == 3) {
      return CSSColorRGB(
          parseInt(color[0]), parseInt(color[1]), parseInt(color[2]));
    } else if (color.length == 4) {
      return CSSColorRGBA(parseInt(color[0]), parseInt(color[1]),
          parseInt(color[2]), parseDouble(color[3]));
    } else {
      return null;
    }
  } else if (color is Map) {
    var r = findKeyValue(color, ['red', 'r'], true);
    var g = findKeyValue(color, ['green', 'g'], true);
    var b = findKeyValue(color, ['blue', 'b'], true);
    var a = findKeyValue(color, ['alpha', 'a'], true);

    if (r != null && g != null && b != null) {
      if (a != null) {
        return CSSColorRGBA(
            parseInt(r), parseInt(g), parseInt(b), parseDouble(a));
      } else {
        return CSSColorRGB(parseInt(r), parseInt(g), parseInt(b));
      }
    } else {
      return null;
    }
  }

  if (color is CSSColorRGB) {
    return color;
  } else if (color is CSSColorRGBA) {
    return color;
  } else if (color is CSSColorHEX) {
    return color;
  } else if (color is String) {
    return CSSColor.parse(color);
  }

  return null;
}