int gui_color_assign (int *color, const char *color_name) { int flag, extra_attr, color_index, number; char *error; /* read extended attributes */ extra_attr = 0; while ((flag = gui_color_attr_get_flag (color_name[0])) > 0) { extra_attr |= flag; color_name++; } /* is it a color alias? */ number = gui_color_palette_get_alias (color_name); if (number >= 0) { *color = number | GUI_COLOR_EXTENDED_FLAG | extra_attr; return 1; } /* is it a color number? */ error = NULL; number = (int)strtol (color_name, &error, 10); if (color_name[0] && error && !error[0] && (number >= 0)) { /* color_name is a number, use this color number */ if (number > GUI_COLOR_EXTENDED_MAX) number = GUI_COLOR_EXTENDED_MAX; *color = number | GUI_COLOR_EXTENDED_FLAG | extra_attr; return 1; } else { /* search for basic WeeChat color */ color_index = gui_color_search (color_name); if (color_index >= 0) { *color = color_index | extra_attr; return 1; } } /* color not found */ return 0; }
char * gui_color_decode (const char *string, const char *replacement) { const unsigned char *ptr_string; unsigned char *out; int out_length, out_pos, length; if (!string) return NULL; out_length = (strlen ((char *)string) * 2) + 1; out = malloc (out_length); if (!out) return NULL; ptr_string = (unsigned char *)string; out_pos = 0; while (ptr_string && ptr_string[0] && (out_pos < out_length - 1)) { switch (ptr_string[0]) { case GUI_COLOR_COLOR_CHAR: ptr_string++; switch (ptr_string[0]) { case GUI_COLOR_FG_CHAR: ptr_string++; if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR) { ptr_string++; while (gui_color_attr_get_flag (ptr_string[0]) > 0) { ptr_string++; } if (ptr_string[0] && ptr_string[1] && ptr_string[2] && ptr_string[3] && ptr_string[4]) { ptr_string += 5; } } else { while (gui_color_attr_get_flag (ptr_string[0]) > 0) { ptr_string++; } if (ptr_string[0] && ptr_string[1]) ptr_string += 2; } break; case GUI_COLOR_BG_CHAR: ptr_string++; if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR) { ptr_string++; if (ptr_string[0] && ptr_string[1] && ptr_string[2] && ptr_string[3] && ptr_string[4]) { ptr_string += 5; } } else { if (ptr_string[0] && ptr_string[1]) ptr_string += 2; } break; case GUI_COLOR_FG_BG_CHAR: ptr_string++; if (ptr_string[0] == GUI_COLOR_EXTENDED_CHAR) { ptr_string++; while (gui_color_attr_get_flag (ptr_string[0]) > 0) { ptr_string++; } if (ptr_string[0] && ptr_string[1] && ptr_string[2] && ptr_string[3] && ptr_string[4]) { ptr_string += 5; } } else { while (gui_color_attr_get_flag (ptr_string[0]) > 0) { ptr_string++; } if (ptr_string[0] && ptr_string[1]) ptr_string += 2; } if (ptr_string[0] == ',') { if (ptr_string[1] == GUI_COLOR_EXTENDED_CHAR) { if (ptr_string[2] && ptr_string[3] && ptr_string[4] && ptr_string[5] && ptr_string[6]) { ptr_string += 7; } } else { if (ptr_string[1] && ptr_string[2]) ptr_string += 3; } } break; case GUI_COLOR_EXTENDED_CHAR: if ((isdigit (ptr_string[1])) && (isdigit (ptr_string[2])) && (isdigit (ptr_string[3])) && (isdigit (ptr_string[4])) && (isdigit (ptr_string[5]))) ptr_string += 6; break; case GUI_COLOR_BAR_CHAR: ptr_string++; switch (ptr_string[0]) { case GUI_COLOR_BAR_FG_CHAR: case GUI_COLOR_BAR_BG_CHAR: case GUI_COLOR_BAR_DELIM_CHAR: case GUI_COLOR_BAR_START_INPUT_CHAR: case GUI_COLOR_BAR_START_INPUT_HIDDEN_CHAR: case GUI_COLOR_BAR_MOVE_CURSOR_CHAR: case GUI_COLOR_BAR_START_ITEM: case GUI_COLOR_BAR_START_LINE_ITEM: ptr_string++; break; } break; case GUI_COLOR_RESET_CHAR: ptr_string++; break; default: if (isdigit (ptr_string[0]) && isdigit (ptr_string[1])) ptr_string += 2; break; } if (replacement && replacement[0]) { out[out_pos] = replacement[0]; out_pos++; } break; case GUI_COLOR_SET_ATTR_CHAR: case GUI_COLOR_REMOVE_ATTR_CHAR: ptr_string++; if (ptr_string[0]) ptr_string++; if (replacement && replacement[0]) { out[out_pos] = replacement[0]; out_pos++; } break; case GUI_COLOR_RESET_CHAR: ptr_string++; if (replacement && replacement[0]) { out[out_pos] = replacement[0]; out_pos++; } break; default: length = utf8_char_size ((char *)ptr_string); if (length == 0) length = 1; memcpy (out + out_pos, ptr_string, length); out_pos += length; ptr_string += length; break; } } out[out_pos] = '\0'; return (char *)out; }
const char * gui_color_get_custom (const char *color_name) { int fg, bg, fg_term, bg_term, term_color; static char color[32][32]; static int index_color = 0; char color_fg[32], color_bg[32]; char *pos_delim, *str_fg, *pos_bg, *error, *color_attr; const char *ptr_color_name; /* attribute or other color name (GUI dependent) */ index_color = (index_color + 1) % 32; color[index_color][0] = '\0'; if (!color_name || !color_name[0]) return color[index_color]; if (string_strcasecmp (color_name, "reset") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c", GUI_COLOR_RESET_CHAR); } else if (string_strcasecmp (color_name, "resetcolor") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c", GUI_COLOR_COLOR_CHAR, GUI_COLOR_RESET_CHAR); } else if (string_strcasecmp (color_name, "bold") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c", GUI_COLOR_SET_ATTR_CHAR, GUI_COLOR_ATTR_BOLD_CHAR); } else if (string_strcasecmp (color_name, "-bold") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c", GUI_COLOR_REMOVE_ATTR_CHAR, GUI_COLOR_ATTR_BOLD_CHAR); } else if (string_strcasecmp (color_name, "reverse") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c", GUI_COLOR_SET_ATTR_CHAR, GUI_COLOR_ATTR_REVERSE_CHAR); } else if (string_strcasecmp (color_name, "-reverse") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c", GUI_COLOR_REMOVE_ATTR_CHAR, GUI_COLOR_ATTR_REVERSE_CHAR); } else if (string_strcasecmp (color_name, "italic") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c", GUI_COLOR_SET_ATTR_CHAR, GUI_COLOR_ATTR_ITALIC_CHAR); } else if (string_strcasecmp (color_name, "-italic") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c", GUI_COLOR_REMOVE_ATTR_CHAR, GUI_COLOR_ATTR_ITALIC_CHAR); } else if (string_strcasecmp (color_name, "underline") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c", GUI_COLOR_SET_ATTR_CHAR, GUI_COLOR_ATTR_UNDERLINE_CHAR); } else if (string_strcasecmp (color_name, "-underline") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c", GUI_COLOR_REMOVE_ATTR_CHAR, GUI_COLOR_ATTR_UNDERLINE_CHAR); } else if (string_strcasecmp (color_name, "bar_fg") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c%c", GUI_COLOR_COLOR_CHAR, GUI_COLOR_BAR_CHAR, GUI_COLOR_BAR_FG_CHAR); } else if (string_strcasecmp (color_name, "bar_delim") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c%c", GUI_COLOR_COLOR_CHAR, GUI_COLOR_BAR_CHAR, GUI_COLOR_BAR_DELIM_CHAR); } else if (string_strcasecmp (color_name, "bar_bg") == 0) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c%c", GUI_COLOR_COLOR_CHAR, GUI_COLOR_BAR_CHAR, GUI_COLOR_BAR_BG_CHAR); } else { /* custom color name (GUI dependent) */ fg_term = -1; bg_term = -1; fg = -1; bg = -1; color_attr = NULL; color_fg[0] = '\0'; color_bg[0] = '\0'; /* read extra attributes (bold, ..) */ ptr_color_name = color_name; while (gui_color_attr_get_flag (ptr_color_name[0]) > 0) { ptr_color_name++; } if (ptr_color_name != color_name) { color_attr = string_strndup (color_name, ptr_color_name - color_name); } pos_delim = strchr (ptr_color_name, ','); if (!pos_delim) pos_delim = strchr (ptr_color_name, ':'); if (pos_delim) { if (pos_delim == ptr_color_name) str_fg = NULL; else str_fg = string_strndup (ptr_color_name, pos_delim - ptr_color_name); pos_bg = pos_delim + 1; } else { str_fg = strdup (ptr_color_name); pos_bg = NULL; } if (str_fg) { fg_term = gui_color_palette_get_alias (str_fg); if (fg_term < 0) { error = NULL; term_color = (int)strtol (str_fg, &error, 10); if (error && !error[0]) { fg_term = term_color; if (fg_term < 0) fg_term = 0; else if (fg_term > GUI_COLOR_EXTENDED_MAX) fg_term = GUI_COLOR_EXTENDED_MAX; } else fg = gui_color_search (str_fg); } } if (pos_bg) { bg_term = gui_color_palette_get_alias (pos_bg); if (bg_term < 0) { error = NULL; term_color = (int)strtol (pos_bg, &error, 10); if (error && !error[0]) { bg_term = term_color; if (bg_term < 0) bg_term = 0; else if (bg_term > GUI_COLOR_EXTENDED_MAX) bg_term = GUI_COLOR_EXTENDED_MAX; } else bg = gui_color_search (pos_bg); } } if (fg_term >= 0) { snprintf (color_fg, sizeof (color_fg), "%c%s%05d", GUI_COLOR_EXTENDED_CHAR, (color_attr) ? color_attr : "", fg_term); } else if (fg >= 0) { snprintf (color_fg, sizeof (color_fg), "%s%02d", (color_attr) ? color_attr : "", fg); } if (bg_term >= 0) { snprintf (color_bg, sizeof (color_bg), "%c%05d", GUI_COLOR_EXTENDED_CHAR, bg_term); } else if (bg >= 0) { snprintf (color_bg, sizeof (color_bg), "%02d", bg); } if (color_fg[0] && color_bg[0]) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c%s,%s", GUI_COLOR_COLOR_CHAR, GUI_COLOR_FG_BG_CHAR, color_fg, color_bg); } else if (color_fg[0]) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c%s", GUI_COLOR_COLOR_CHAR, GUI_COLOR_FG_CHAR, color_fg); } else if (color_bg[0]) { snprintf (color[index_color], sizeof (color[index_color]), "%c%c%s", GUI_COLOR_COLOR_CHAR, GUI_COLOR_BG_CHAR, color_bg); } if (color_attr) free (color_attr); if (str_fg) free (str_fg); } return color[index_color]; }