'Handle the foreground color state.
Foreground colors can consist of up to two digits and may optionally
end in a I{,}. Any non-digit or non-comma characters are treated as
invalid input and result in the state being reset to "text".
@param ch: The character being processed.'

def state_COLOR_FOREGROUND(self, ch):

    if (ch.isdigit() and (len(self._buffer) < 2)):
        self._buffer += ch
    else:
        if self._buffer:
            col = (int(self._buffer) % len(_IRC_COLORS))
            self.foreground = getattr(attributes.fg, _IRC_COLOR_NAMES[col])
        else:
            self.foreground = self.background = None
        if ((ch == ',') and self._buffer):
            self._buffer = ''
            self.state = 'COLOR_BACKGROUND'
        else:
            self._buffer = ''
            self.state = 'TEXT'
            self.emit()
            self.process(ch)
