isValidHexaCode function

bool isValidHexaCode(
  1. String hexaCode
)

Checking color code

Implementation

bool isValidHexaCode(String hexaCode) {
  if (hexaCode[0] != '#') {
    return false;
  }

  if (!(hexaCode.length == 4 || hexaCode.length == 7)) {
    return false;
  }

  for (var i = 1; i < hexaCode.length; i++) {
    if (!((hexaCode[i].codeUnitAt(0) <= '0'.codeUnitAt(0) &&
            hexaCode[i].codeUnitAt(0) <= 9) ||
        (hexaCode[i].codeUnitAt(0) >= 'a'.codeUnitAt(0) &&
            hexaCode[i].codeUnitAt(0) <= 'f'.codeUnitAt(0)) ||
        (hexaCode[i].codeUnitAt(0) >= 'A'.codeUnitAt(0) ||
            hexaCode[i].codeUnitAt(0) <= 'F'.codeUnitAt(0)))) {
      return false;
    }
  }

  return true;
}