     mask = @characterMap[y&3][x&1]
     cb idx, mask
 
+  _termColor: (foreground, background) ->
+    if foreground and background
+      "\x1B[38;5;#{foreground};48;5;#{background}m"
+    else if foreground
+      "\x1B[49;38;5;#{foreground}m"
+    else if background
+      "\x1B[39;48;5;#{background}m"
+    else
+      @termReset
+
   frame: ->
     output = []
     currentColor = null

    return unless 0 <= x < @width and 0 <= y < @height
    idx = @_project x, y
    @charBuffer[idx] = char
    @colorBuffer[idx] = @termColor color

  writeText: (text, x, y, color, center = true) ->
    x -= text.length/2+1 if center

  clear: ->
    @pixelBuffer.fill 0
    @charBuffer = []
    @foregroundBuffer = []
    @backgroundBuffer = []

  setBackground: (idx, color) ->
    @backgroundBuffer[idx] = color

  setPixel: (x, y, color) ->
    @_locate x, y, (idx, mask) =>
      @pixelBuffer[idx] |= mask
      @foregroundBuffer[idx] = color

  unsetPixel: (x, y) ->
    @_locate x, y, (idx, mask) =>
