diff --git a/src/vte-private.h b/src/vte-private.h
index 9192fb3..42a52ef 100644
--- a/src/vte-private.h
+++ b/src/vte-private.h
@@ -223,19 +223,14 @@ struct _VteTerminalPrivate {
 		long insert_delta;	/* insertion offset */
 		VteCell defaults;	/* default characteristics
 					   for insertion of any new
-					   characters; colors not yet
-					   adjusted for bold/dim/standout */
+					   characters */
 		VteCell color_defaults;	/* original defaults
 					   plus the current
-					   fore/back already adjusted for
-					   bold/dim/standout */
+					   fore/back */
 		VteCell fill_defaults;	/* original defaults
 					   plus the current
-					   fore/back already adjusted for
-					   bold/dim/standout with no
+					   fore/back with no
 					   character data */
-		gboolean fg_sgr_extended, bg_sgr_extended;	/* whether fg/bg were set by
-								   256 color sequences */
 		gboolean alternate_charset;
 		gboolean status_line;
 		GString *status_line_contents;
diff --git a/src/vte.c b/src/vte.c
index a8337cb..a402654 100644
--- a/src/vte.c
+++ b/src/vte.c
@@ -319,6 +319,10 @@ G_DEFINE_TYPE(VteTerminal, vte_terminal, GTK_TYPE_WIDGET)
 #endif
 #endif /* GTK 3.0 */
 
+/* Indexes in the "palette" color array for the dim colors. 
+ * Only the first %VTE_LEGACY_COLOR_SET_SIZE colors have dim versions.  */ 
+static const guchar corresponding_dim_index[] = {16,88,28,100,18,90,30,102}; 
+
 static void
 vte_g_array_fill(GArray *array, gconstpointer item, guint final_size)
 {
@@ -371,8 +375,6 @@ _vte_terminal_set_default_attributes(VteTerminal *terminal)
 	screen->defaults = basic_cell.cell;
 	screen->color_defaults = screen->defaults;
 	screen->fill_defaults = screen->defaults;
-	screen->fg_sgr_extended = FALSE;
-	screen->bg_sgr_extended = FALSE;
 }
 
 /* Cause certain cells to be repainted. */
@@ -6294,6 +6296,24 @@ vte_terminal_copy_cb(GtkClipboard *clipboard, GtkSelectionData *data,
 	}
 }
 
+/* Index is most likely a color index, but can encode actual color in case SGR truecolor extension was used.
+ * Convert to RGB in either case. */
+static void
+vte_terminal_get_rgb_from_index(const VteTerminal *terminal, guint index, PangoColor *color)
+{
+	if (index >= VTE_PALETTE_SIZE && index < VTE_PALETTE_SIZE + 16)
+		index -= VTE_PALETTE_SIZE;
+	if (index < VTE_PALETTE_SIZE) {
+		memcpy(color, &terminal->pvt->palette[index], sizeof(PangoColor));
+	} else if (index >= 0x1000000 && index < 0x2000000) {
+		color->red = ((index >> 16) & 0xFF) * 257;
+		color->green = ((index >> 8) & 0xFF) * 257;
+		color->blue = (index & 0xFF) * 257;
+	} else {
+		g_assert_not_reached();
+	}
+}
+
 /**
  * VteSelectionFunc:
  * @terminal: terminal in which the cell is.
@@ -6362,7 +6382,7 @@ vte_terminal_get_text_range_maybe_wrapped(VteTerminal *terminal,
 	const VteCell *pcell = NULL;
 	GString *string;
 	struct _VteCharAttributes attr;
-	PangoColor fore, back, *palette;
+	PangoColor fore, back;
 
 	if (!is_selected)
 		is_selected = always_selected;
@@ -6375,7 +6395,6 @@ vte_terminal_get_text_range_maybe_wrapped(VteTerminal *terminal,
 	string = g_string_new(NULL);
 	memset(&attr, 0, sizeof(attr));
 
-	palette = terminal->pvt->palette;
 	col = start_col;
 	for (row = start_row; row < end_row + 1; row++, col = 0) {
 		const VteRowData *row_data = _vte_terminal_find_row_data (terminal, row);
@@ -6395,8 +6414,8 @@ vte_terminal_get_text_range_maybe_wrapped(VteTerminal *terminal,
 				 * the selection. */
 				if (!pcell->attr.fragment && is_selected(terminal, col, row, data)) {
 					/* Store the attributes of this character. */
-					fore = palette[pcell->attr.fore];
-					back = palette[pcell->attr.back];
+					vte_terminal_get_rgb_from_index(terminal, pcell->attr.fore, &fore);
+					vte_terminal_get_rgb_from_index(terminal, pcell->attr.back, &back);
 					attr.fore.red = fore.red;
 					attr.fore.green = fore.green;
 					attr.fore.blue = fore.blue;
@@ -9401,6 +9420,29 @@ vte_terminal_determine_colors_internal(VteTerminal *terminal,
 			back = VTE_DEF_FG;
 	}
 
+	/* Handle bold by using set bold color or brightening */
+	if (cell->attr.bold) {
+		if (fore == VTE_DEF_FG)
+			fore = VTE_BOLD_FG;
+		else if (fore >= VTE_PALETTE_SIZE && fore < VTE_PALETTE_SIZE + VTE_LEGACY_COLOR_SET_SIZE) {
+			fore += VTE_COLOR_BRIGHT_OFFSET;
+		}
+	}
+
+	/* Handle half similarly */
+	if (cell->attr.half) {
+		if (fore == VTE_DEF_FG)
+			fore = VTE_DIM_FG;
+		else if (fore >= VTE_PALETTE_SIZE && fore < VTE_PALETTE_SIZE + VTE_LEGACY_COLOR_SET_SIZE)
+			fore = corresponding_dim_index[fore - VTE_PALETTE_SIZE];
+	}
+
+	/* And standout */
+	if (cell->attr.standout) {
+		if (fore >= VTE_PALETTE_SIZE && back < VTE_PALETTE_SIZE + VTE_LEGACY_COLOR_SET_SIZE)
+			back += VTE_COLOR_BRIGHT_OFFSET;
+	}
+
 	/* Reverse cell? */
 	if (cell->attr.reverse) {
 		swap (&fore, &back);
@@ -9521,18 +9563,22 @@ vte_terminal_draw_graphic(VteTerminal *terminal, vteunistr c,
         double adjust;
         cairo_t *cr = _vte_draw_get_context (terminal->pvt->draw);
 
+        PangoColor fg, bg;
+        vte_terminal_get_rgb_from_index(terminal, fore, &fg);
+        vte_terminal_get_rgb_from_index(terminal, back, &bg);
+
         width = column_width * columns;
 
 	if ((back != VTE_DEF_BG) || draw_default_bg) {
 		vte_terminal_fill_rectangle(terminal,
-					    &terminal->pvt->palette[back],
+					    &bg,
 					    x, y, width, row_height);
 	}
 
         cairo_save (cr);
 
         cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
-        _vte_draw_set_source_color_alpha (terminal->pvt->draw, &terminal->pvt->palette[fore], VTE_DRAW_OPAQUE);
+        _vte_draw_set_source_color_alpha (terminal->pvt->draw, &fg, VTE_DRAW_OPAQUE);
 
         // FIXME wtf!?
         x += terminal->pvt->inner_border.left;
@@ -10014,9 +10060,9 @@ vte_terminal_draw_graphic(VteTerminal *terminal, vteunistr c,
 
                 /* Now take the inside out */
                 cairo_set_source_rgba (cr,
-                                       terminal->pvt->palette[back].red / 65535.,
-                                       terminal->pvt->palette[back].green / 65535.,
-                                       terminal->pvt->palette[back].blue / 65535.,
+                                       bg.red / 65535.,
+                                       bg.green / 65535.,
+                                       bg.blue / 65535.,
                                        1.);
                 cairo_set_line_width(cr, inner_line_width);
                 cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER);
@@ -10171,9 +10217,9 @@ vte_terminal_draw_graphic(VteTerminal *terminal, vteunistr c,
         case 0x2592: /* medium shade */
         case 0x2593: /* dark shade */
                 cairo_set_source_rgba (cr,
-                                       terminal->pvt->palette[fore].red / 65535.,
-                                       terminal->pvt->palette[fore].green / 65535.,
-                                       terminal->pvt->palette[fore].blue / 65535.,
+                                       fg.red / 65535.,
+                                       fg.green / 65535.,
+                                       fg.blue / 65535.,
                                        (c - 0x2590) / 4.);
                 cairo_rectangle(cr, x, y, width, row_height);
                 cairo_fill (cr);
@@ -10278,7 +10324,7 @@ vte_terminal_draw_cells(VteTerminal *terminal,
 {
 	int i, x, y, ascent;
 	gint columns = 0;
-	PangoColor *fg, *bg, *defbg;
+	PangoColor fg, bg, *defbg;
 
 	g_assert(n > 0);
 	_VTE_DEBUG_IF(VTE_DEBUG_CELLS) {
@@ -10296,8 +10342,8 @@ vte_terminal_draw_cells(VteTerminal *terminal,
 	}
 
 	bold = bold && terminal->pvt->allow_bold;
-	fg = &terminal->pvt->palette[fore];
-	bg = &terminal->pvt->palette[back];
+	vte_terminal_get_rgb_from_index(terminal, fore, &fg);
+	vte_terminal_get_rgb_from_index(terminal, back, &bg);
 	defbg = &terminal->pvt->palette[VTE_DEF_BG];
 	ascent = terminal->char_ascent;
 
@@ -10312,20 +10358,20 @@ vte_terminal_draw_cells(VteTerminal *terminal,
 			items[i].y += terminal->pvt->inner_border.top;
 			columns += items[i].columns;
 		}
-		if (clear && (draw_default_bg || bg != defbg)) {
+		if (clear && (draw_default_bg || back != VTE_DEF_BG)) {
 			gint bold_offset = _vte_draw_has_bold(terminal->pvt->draw,
 									VTE_DRAW_BOLD) ? 0 : bold;
 			_vte_draw_fill_rectangle(terminal->pvt->draw,
 					x + terminal->pvt->inner_border.left,
                                         y + terminal->pvt->inner_border.top,
 					columns * column_width + bold_offset, row_height,
-					bg, VTE_DRAW_OPAQUE);
+					&bg, VTE_DRAW_OPAQUE);
 		}
 	} while (i < n);
 
 	_vte_draw_text(terminal->pvt->draw,
 			items, n,
-			fg, VTE_DRAW_OPAQUE,
+			&fg, VTE_DRAW_OPAQUE,
 			_vte_draw_get_style(bold, italic));
 
 	for (i = 0; i < n; i++) {
@@ -10345,7 +10391,7 @@ vte_terminal_draw_cells(VteTerminal *terminal,
 			}
 			if (underline) {
 				vte_terminal_draw_line(terminal,
-						&terminal->pvt->palette[fore],
+						&fg,
 						x,
 						y + terminal->pvt->underline_position,
 						x + (columns * column_width) - 1,
@@ -10353,7 +10399,7 @@ vte_terminal_draw_cells(VteTerminal *terminal,
 			}
 			if (strikethrough) {
 				vte_terminal_draw_line(terminal,
-						&terminal->pvt->palette[fore],
+						&fg,
 						x,
 						y + terminal->pvt->strikethrough_position,
 						x + (columns * column_width) - 1,
@@ -10361,7 +10407,7 @@ vte_terminal_draw_cells(VteTerminal *terminal,
 			}
 			if (hilite) {
 				vte_terminal_draw_line(terminal,
-						&terminal->pvt->palette[fore],
+						&fg,
 						x,
 						y + row_height - 1,
 						x + (columns * column_width) - 1,
@@ -10369,7 +10415,7 @@ vte_terminal_draw_cells(VteTerminal *terminal,
 			}
 			if (boxed) {
 				vte_terminal_draw_rectangle(terminal,
-						&terminal->pvt->palette[fore],
+						&fg,
 						x, y,
 						MAX(0, (columns * column_width)),
 						MAX(0, row_height));
@@ -10720,15 +10766,17 @@ vte_terminal_draw_rows(VteTerminal *terminal,
 					j += cell ? cell->attr.columns : 1;
 				}
 				if (back != VTE_DEF_BG) {
+					PangoColor bg;
 					gint bold_offset = _vte_draw_has_bold(terminal->pvt->draw,
 											VTE_DRAW_BOLD) ? 0 : bold;
+					vte_terminal_get_rgb_from_index(terminal, back, &bg);
 					_vte_draw_fill_rectangle (
 							terminal->pvt->draw,
 							x + i * column_width,
 							y,
 							(j - i) * column_width + bold_offset,
 							row_height,
-							&terminal->pvt->palette[back], VTE_DRAW_OPAQUE);
+							&bg, VTE_DRAW_OPAQUE);
 				}
 				/* We'll need to continue at the first cell which didn't
 				 * match the first one in this set. */
@@ -10747,12 +10795,14 @@ vte_terminal_draw_rows(VteTerminal *terminal,
 				}
 				vte_terminal_determine_colors(terminal, NULL, selected, &fore, &back);
 				if (back != VTE_DEF_BG) {
+					PangoColor bg;
+					vte_terminal_get_rgb_from_index(terminal, back, &bg);
 					_vte_draw_fill_rectangle (terminal->pvt->draw,
 								  x + i *column_width,
 								  y,
 								  (j - i)  * column_width,
 								  row_height,
-								  &terminal->pvt->palette[back], VTE_DRAW_OPAQUE);
+								  &bg, VTE_DRAW_OPAQUE);
 				}
 				i = j;
 			} while (i < end_column);
diff --git a/src/vterowdata.h b/src/vterowdata.h
index 4150095..7ba50bf 100644
--- a/src/vterowdata.h
+++ b/src/vterowdata.h
@@ -53,8 +53,11 @@ typedef struct _VteCellAttr {
 				   */
 	guint32 bold: 1;
 	guint32 italic: 1;
-	guint32 fore: 9;	/* Index into color palette */
-	guint32 back: 9;	/* Index into color palette. */
+	guint32 fore: 25;
+	guint32 back: 25;	/* 0 .. 255: SGR 256-color extension, index into color palette */
+				/* 256 .. VTE_PALETTE_SIZE-1: special colors, index into color palette */
+				/* VTE_PALETTE_SIZE .. VTE_PALETTE_SIZE+15: legacy 16 colors */
+				/* 2^24 .. 2^25-1: SGR truecolor extension, direct RGB values */
 
 	guint32 standout: 1;
 	guint32 underline: 1;
@@ -66,11 +69,11 @@ typedef struct _VteCellAttr {
 
 	guint32 invisible: 1;
 } VteCellAttr;
-G_STATIC_ASSERT (sizeof (VteCellAttr) == 4);
+G_STATIC_ASSERT (sizeof (VteCellAttr) == 8);
 
 typedef union _VteIntCellAttr {
+	guint64 i;
 	VteCellAttr s;
-	guint32 i;
 } VteIntCellAttr;
 G_STATIC_ASSERT (sizeof (VteCellAttr) == sizeof (VteIntCellAttr));
 
@@ -79,23 +82,22 @@ G_STATIC_ASSERT (sizeof (VteCellAttr) == sizeof (VteIntCellAttr));
  */
 
 typedef struct _VteCell {
-	vteunistr c;
 	VteCellAttr attr;
+	vteunistr c;
 } VteCell;
-G_STATIC_ASSERT (sizeof (VteCell) == 8);
+G_STATIC_ASSERT (sizeof (VteCell) == 12);
 
 typedef union _VteIntCell {
 	VteCell cell;
-	struct {
+	struct __attribute__((__packed__)) /* FIXME: why is it required? */ {
+		guint64 attr;
 		guint32 c;
-		guint32 attr;
 	} i;
 } VteIntCell;
 G_STATIC_ASSERT (sizeof (VteCell) == sizeof (VteIntCell));
 
 static const VteIntCell basic_cell = {
 	{
-		0,
 		{
 			0, /* fragment */
 			1, /* columns */
@@ -113,7 +115,8 @@ static const VteIntCell basic_cell = {
 			0, /* half */
 
 			0  /* invisible */
-		}
+		},
+		0
 	}
 };
 
diff --git a/src/vteseq.c b/src/vteseq.c
index 38f9ba1..6604677 100644
--- a/src/vteseq.c
+++ b/src/vteseq.c
@@ -493,53 +493,6 @@ vte_sequence_handler_set_mode_internal(VteTerminal *terminal,
 }
 
 
-/* Indexes in the "palette" color array for the dim colors.
- * Only the first %VTE_LEGACY_COLOR_SET_SIZE colors have dim versions.  */
-static const guchar corresponding_dim_index[] = {16,88,28,100,18,90,30,102};
-
-static void
-vte_adjust_colors(VteTerminal *terminal)
-{
-	guint fore, back;
-
-	fore = terminal->pvt->screen->defaults.attr.fore;
-	back = terminal->pvt->screen->defaults.attr.back;
-
-	/* Handle bold by using set bold color or brightening */
-	if (terminal->pvt->screen->defaults.attr.bold) {
-		if (fore == VTE_DEF_FG) {
-			fore = VTE_BOLD_FG;
-		} else if (!terminal->pvt->screen->fg_sgr_extended
-			  && fore < VTE_LEGACY_COLOR_SET_SIZE) {
-				fore += VTE_COLOR_BRIGHT_OFFSET;
-		}
-	}
-
-	/* Handle half similarly */
-	if (terminal->pvt->screen->defaults.attr.half) {
-		if (fore == VTE_DEF_FG) {
-			fore = VTE_DIM_FG;
-		} else if (!terminal->pvt->screen->fg_sgr_extended
-			  && fore < VTE_LEGACY_COLOR_SET_SIZE) {
-			fore = corresponding_dim_index[fore];
-		}
-	}
-
-	/* And standout */
-	if (terminal->pvt->screen->defaults.attr.standout) {
-		if (!terminal->pvt->screen->bg_sgr_extended
-		  && back < VTE_LEGACY_COLOR_SET_SIZE) {
-			back += VTE_COLOR_BRIGHT_OFFSET;
-		}
-	}
-
-	/* Save the adjusted colors. */
-	terminal->pvt->screen->color_defaults.attr.fore = fore;
-	terminal->pvt->screen->color_defaults.attr.back = back;
-	terminal->pvt->screen->fill_defaults.attr.fore = fore;
-	terminal->pvt->screen->fill_defaults.attr.back = back;
-}
-
 /*
  * Sequence handling boilerplate
  */
@@ -2456,33 +2409,54 @@ vte_sequence_handler_character_attributes (VteTerminal *terminal, GValueArray *p
 		case 35:
 		case 36:
 		case 37:
-			terminal->pvt->screen->fg_sgr_extended = FALSE;
-			terminal->pvt->screen->defaults.attr.fore = param - 30;
+			terminal->pvt->screen->defaults.attr.fore = VTE_PALETTE_SIZE + param - 30;
 			break;
 		case 38:
 		{
-			/* The format looks like: ^[[38;5;COLORNUMBERm,
-			   so look for COLORNUMBER here. */
-			if ((i + 2) < params->n_values){
-				GValue *value1, *value2;
-				long param1, param2;
+			/* The format looks like: ^[[38;5;COLORNUMBERm or ^[[38;2;RED;GREEN;BLUEm
+			   so look for the parameters here. */
+			if ((i + 1) < params->n_values) {
+				GValue *value1, *value2, *value3, *value4;
+				long param1, param2, param3, param4;
 				value1 = g_value_array_get_nth(params, i + 1);
-				value2 = g_value_array_get_nth(params, i + 2);
-				if (G_UNLIKELY (!(G_VALUE_HOLDS_LONG(value1) && G_VALUE_HOLDS_LONG(value2))))
+				if (G_UNLIKELY (!G_VALUE_HOLDS_LONG(value1)))
 					break;
 				param1 = g_value_get_long(value1);
-				param2 = g_value_get_long(value2);
-				if (G_LIKELY (param1 == 5 && param2 >= 0 && param2 < 256)) {
-					terminal->pvt->screen->fg_sgr_extended = TRUE;
-					terminal->pvt->screen->defaults.attr.fore = param2;
+				switch (param1) {
+				case 2:
+					if (G_UNLIKELY ((i + 4) >= params->n_values))
+						break;
+					value2 = g_value_array_get_nth(params, i + 2);
+					value3 = g_value_array_get_nth(params, i + 3);
+					value4 = g_value_array_get_nth(params, i + 4);
+					if (G_UNLIKELY (!(G_VALUE_HOLDS_LONG(value2) && G_VALUE_HOLDS_LONG(value3) && G_VALUE_HOLDS_LONG(value4))))
+						break;
+					param2 = g_value_get_long(value2);
+					param3 = g_value_get_long(value3);
+					param4 = g_value_get_long(value4);
+					if (G_LIKELY (param2 >= 0 && param2 < 256 && param3 >= 0 && param3 < 256 && param4 >= 0 && param4 < 256 ))
+						terminal->pvt->screen->defaults.attr.fore = (1 << 24) | (param2 << 16) | (param3 << 8) | param4;
+					i += 4;
+					break;
+				case 5:
+					if (G_UNLIKELY ((i + 2) >= params->n_values))
+						break;
+					value2 = g_value_array_get_nth(params, i + 2);
+					if (G_UNLIKELY (!G_VALUE_HOLDS_LONG(value2)))
+						break;
+					param2 = g_value_get_long(value2);
+					if (G_LIKELY (param2 >= 0 && param2 < 256))
+						terminal->pvt->screen->defaults.attr.fore = param2;
+					i += 2;
+					break;
+				default:
+					break;
 				}
-				i += 2;
 			}
 			break;
 		}
 		case 39:
 			/* default foreground */
-			terminal->pvt->screen->fg_sgr_extended = FALSE;
 			terminal->pvt->screen->defaults.attr.fore = VTE_DEF_FG;
 			break;
 		case 40:
@@ -2493,33 +2467,54 @@ vte_sequence_handler_character_attributes (VteTerminal *terminal, GValueArray *p
 		case 45:
 		case 46:
 		case 47:
-			terminal->pvt->screen->bg_sgr_extended = FALSE;
-			terminal->pvt->screen->defaults.attr.back = param - 40;
+			terminal->pvt->screen->defaults.attr.back = VTE_PALETTE_SIZE + param - 40;
 			break;
 		case 48:
 		{
-			/* The format looks like: ^[[48;5;COLORNUMBERm,
-			   so look for COLORNUMBER here. */
-			if ((i + 2) < params->n_values){
-				GValue *value1, *value2;
-				long param1, param2;
+			/* The format looks like: ^[[48;5;COLORNUMBERm or ^[[48;2;RED;GREEN;BLUEm
+			   so look for the parameters here. */
+			if ((i + 1) < params->n_values) {
+				GValue *value1, *value2, *value3, *value4;
+				long param1, param2, param3, param4;
 				value1 = g_value_array_get_nth(params, i + 1);
-				value2 = g_value_array_get_nth(params, i + 2);
-				if (G_UNLIKELY (!(G_VALUE_HOLDS_LONG(value1) && G_VALUE_HOLDS_LONG(value2))))
+				if (G_UNLIKELY (!G_VALUE_HOLDS_LONG(value1)))
 					break;
 				param1 = g_value_get_long(value1);
-				param2 = g_value_get_long(value2);
-				if (G_LIKELY (param1 == 5 && param2 >= 0 && param2 < 256)) {
-					terminal->pvt->screen->bg_sgr_extended = TRUE;
-					terminal->pvt->screen->defaults.attr.back = param2;
+				switch (param1) {
+				case 2:
+					if (G_UNLIKELY ((i + 4) >= params->n_values))
+						break;
+					value2 = g_value_array_get_nth(params, i + 2);
+					value3 = g_value_array_get_nth(params, i + 3);
+					value4 = g_value_array_get_nth(params, i + 4);
+					if (G_UNLIKELY (!(G_VALUE_HOLDS_LONG(value2) && G_VALUE_HOLDS_LONG(value3) && G_VALUE_HOLDS_LONG(value4))))
+						break;
+					param2 = g_value_get_long(value2);
+					param3 = g_value_get_long(value3);
+					param4 = g_value_get_long(value4);
+					if (G_LIKELY (param2 >= 0 && param2 < 256 && param3 >= 0 && param3 < 256 && param4 >= 0 && param4 < 256 ))
+						terminal->pvt->screen->defaults.attr.back = (1 << 24) | (param2 << 16) | (param3 << 8) | param4;
+					i += 4;
+					break;
+				case 5:
+					if (G_UNLIKELY ((i + 2) >= params->n_values))
+						break;
+					value2 = g_value_array_get_nth(params, i + 2);
+					if (G_UNLIKELY (!G_VALUE_HOLDS_LONG(value2)))
+						break;
+					param2 = g_value_get_long(value2);
+					if (G_LIKELY (param2 >= 0 && param2 < 256))
+						terminal->pvt->screen->defaults.attr.back = param2;
+					i += 2;
+					break;
+				default:
+					break;
 				}
-				i += 2;
 			}
 			break;
 		}
 		case 49:
 			/* default background */
-			terminal->pvt->screen->bg_sgr_extended = FALSE;
 			terminal->pvt->screen->defaults.attr.back = VTE_DEF_BG;
 			break;
 		case 90:
@@ -2530,8 +2525,7 @@ vte_sequence_handler_character_attributes (VteTerminal *terminal, GValueArray *p
 		case 95:
 		case 96:
 		case 97:
-			terminal->pvt->screen->fg_sgr_extended = FALSE;
-			terminal->pvt->screen->defaults.attr.fore = param - 90 + VTE_COLOR_BRIGHT_OFFSET;
+			terminal->pvt->screen->defaults.attr.fore = VTE_PALETTE_SIZE + param - 90 + VTE_COLOR_BRIGHT_OFFSET;
 			break;
 		case 100:
 		case 101:
@@ -2541,8 +2535,7 @@ vte_sequence_handler_character_attributes (VteTerminal *terminal, GValueArray *p
 		case 105:
 		case 106:
 		case 107:
-			terminal->pvt->screen->bg_sgr_extended = FALSE;
-			terminal->pvt->screen->defaults.attr.back = param - 100 + VTE_COLOR_BRIGHT_OFFSET;
+			terminal->pvt->screen->defaults.attr.back = VTE_PALETTE_SIZE + param - 100 + VTE_COLOR_BRIGHT_OFFSET;
 			break;
 		}
 	}
@@ -2551,7 +2544,14 @@ vte_sequence_handler_character_attributes (VteTerminal *terminal, GValueArray *p
 		_vte_terminal_set_default_attributes(terminal);
 	}
 	/* Save the new colors. */
-	vte_adjust_colors(terminal);
+	terminal->pvt->screen->color_defaults.attr.fore =
+		terminal->pvt->screen->defaults.attr.fore;
+	terminal->pvt->screen->color_defaults.attr.back =
+		terminal->pvt->screen->defaults.attr.back;
+	terminal->pvt->screen->fill_defaults.attr.fore =
+		terminal->pvt->screen->defaults.attr.fore;
+	terminal->pvt->screen->fill_defaults.attr.back =
+		terminal->pvt->screen->defaults.attr.back;
 }
 
 /* Move the cursor to the given column, 1-based. */
