Esempio n. 1
0
File: pal.c Progetto: l3acon/radare2
R_API char *r_cons_pal_parse(const char *str) {
	int i;
	ut8 r, g, b;
	char out[128];
	char *s = strdup (str);
	char *p = strchr (s+1, ' ');
	out[0] = 0;
	if (p) *p++ = 0;
	if (!strcmp (str, "random")) {
		free (s);
		return r_cons_color_random (0);
	}
	if (!strncmp (s, "rgb:", 4)) {
		r = rgbnum (s[4]);
		g = rgbnum (s[5]);
		b = rgbnum (s[6]);
		r_cons_rgb_str (out, r, g, b, 0);
	}
	if (p && !strncmp (p, "rgb:", 4)) {
		r = rgbnum (p[4]);
		g = rgbnum (p[5]);
		b = rgbnum (p[6]);
		r_cons_rgb_str (out+strlen (out), r, g, b, 1);
	}
	for (i=0; colors[i].name; i++) {
		if (!strcmp (s, colors[i].name))
			strcat (out, colors[i].code);
		if (p && !strcmp (p, colors[i].name))
			strcat (out, colors[i].bgcode);
	}
	free (s);
	return *out? strdup (out): NULL;
}
Esempio n. 2
0
R_API char *r_cons_pal_parse(const char *str) {
	int i;
	ut8 r, g, b;
	char out[128];
	char *s = r_str_trim_head_tail (strdup (str));
	r_str_split (s, ' ');
	int length = strlen (s);
	out[0] = 0;
	if (!strcmp (str, "random")) {
		free (s);
		return r_cons_color_random (0);
	}
	if (!strncmp (s, "rgb:", 4)) {
		int correct = 0;
		if (length == 7) {
			r = rgbnum (s[4],s[4]);
			g = rgbnum (s[5],s[5]);
			b = rgbnum (s[6],s[6]);
			correct = 1;
		} else if (length == 10) {
			r = rgbnum(s[4],s[5]);
			g = rgbnum(s[6],s[7]);
			b = rgbnum(s[8],s[9]);
			correct = 1;
		}
		if (correct) {
			r_cons_rgb_str (out, r, g, b, 0);
		} else {
			eprintf ("Invalid rgb string (%s)\n", str);
		}
	}
	for (i=0; colors[i].name; i++) {
		if (!strcmp (s, colors[i].name))
			strcat (out, colors[i].code);
	}
	free (s);
	return *out? strdup (out): NULL;
}
Esempio n. 3
0
/* Return NULL if outcol is given */
R_API char *r_cons_pal_parse(const char *str, RColor *outcol) {
	int i;
	RColor rcolor = (RColor) RColor_BLACK;
	char *fgcolor;
	char *bgcolor;
	char *attr = NULL;
	char out[128];
	if (!str) {
		return NULL;
	}
	fgcolor = strdup (str);
	if (!fgcolor) {
		return NULL;
	}
	bgcolor = strchr (fgcolor + 1, ' ');
	out[0] = 0;
	if (bgcolor) {
		*bgcolor++ = '\0';
		attr = strchr (bgcolor, ' ');
		if (attr) {
			*attr++ = '\0';
		}
	}

	// Handle first color (fgcolor)
	if (!strcmp (fgcolor, "random")) {
		rcolor = r_cons_color_random (ALPHA_FG);
		if (!outcol) {
			r_cons_rgb_str (out, sizeof (out), &rcolor);
		}
	} else if (!strncmp (fgcolor, "#", 1)) { // "#00ff00" HTML format
		if (strlen (fgcolor) == 7) {
			i = sscanf (fgcolor + 1, "%02hhx%02hhx%02hhx", &rcolor.r, &rcolor.g, &rcolor.b);
			if (i != 3) {
				eprintf ("Error while parsing HTML color: %s\n", fgcolor);
			}
			if (!outcol) {
				r_cons_rgb_str (out, sizeof (out), &rcolor);
			}
		} else {
			eprintf ("Invalid html color code\n");
		}
	} else if (!strncmp (fgcolor, "rgb:", 4)) { // "rgb:123" rgb format
		if (strlen (fgcolor) == 7) {
			rcolor.r = rgbnum (fgcolor[4], '0');
			rcolor.g = rgbnum (fgcolor[5], '0');
			rcolor.b = rgbnum (fgcolor[6], '0');
			if (!outcol) {
				r_cons_rgb_str (out, sizeof (out), &rcolor);
			}
		} else if (strlen (fgcolor) == 10) {
			rcolor.r = rgbnum (fgcolor[4], fgcolor[5]);
			rcolor.g = rgbnum (fgcolor[6], fgcolor[7]);
			rcolor.b = rgbnum (fgcolor[8], fgcolor[9]);
			if (!outcol) {
				r_cons_rgb_str (out, sizeof (out), &rcolor);
			}
		}
	}
	// Handle second color (bgcolor)
	if (bgcolor && !strncmp (bgcolor, "rgb:", 4)) { // "rgb:123" rgb format
		if (strlen (bgcolor) == 7) {
			rcolor.a |= ALPHA_BG;
			rcolor.r2 = rgbnum (bgcolor[4], '0');
			rcolor.g2 = rgbnum (bgcolor[5], '0');
			rcolor.b2 = rgbnum (bgcolor[6], '0');
			if (!outcol) {
				size_t len = strlen (out);
				r_cons_rgb_str (out + len, sizeof (out) - len, &rcolor);
			}
		} else if (strlen (bgcolor) == 10) {
			rcolor.a |= ALPHA_BG;
			rcolor.r2 = rgbnum (bgcolor[4], bgcolor[5]);
			rcolor.g2 = rgbnum (bgcolor[6], bgcolor[7]);
			rcolor.b2 = rgbnum (bgcolor[8], bgcolor[9]);
			if (!outcol) {
				size_t len = strlen (out);
				r_cons_rgb_str (out + len, sizeof (out) - len, &rcolor);
			}
		}
	}
	// No suitable format, checking if colors are named
	for (i = 0; colors[i].name; i++) {
		if (!strcmp (fgcolor, colors[i].name)) {
			rcolor.r = colors[i].rcolor.r;
			rcolor.g = colors[i].rcolor.g;
			rcolor.b = colors[i].rcolor.b;
			if (!outcol) {
				strncat (out, colors[i].code,
					sizeof (out) - strlen (out) - 1);
			}
		}
		if (bgcolor && !strcmp (bgcolor, colors[i].name)) {
			rcolor.a |= ALPHA_BG;
			rcolor.r2 = colors[i].rcolor.r; // Initial color doesn't
			rcolor.g2 = colors[i].rcolor.g; // have r2, g2, b2
			rcolor.b2 = colors[i].rcolor.b;
			if (!outcol) {
				strncat (out, colors[i].bgcode,
					sizeof (out) - strlen (out) - 1);
			}
		}
	}
	if (attr) {
		// Parse extra attributes.
		const char *p = attr;
		while (p) {
			if (!strncmp(p, "bold", 4)) {
				rcolor.attr |= R_CONS_ATTR_BOLD;
			} else if (!strncmp(p, "dim", 3)) {
				rcolor.attr |= 1u << 2;
			} else if (!strncmp(p, "italic", 6)) {
				rcolor.attr |= 1u << 3;
			} else if (!strncmp(p, "underline", 9)) {
				rcolor.attr |= 1u << 4;
			} else if (!strncmp(p, "blink", 5)) {
				rcolor.attr |= 1u << 5;
			} else {
				eprintf ("Failed to parse terminal attributes: %s\n", p);
				break;
			}
			p = strchr (p, ' ');
			if (p) {
				p++;
			}
		}
	}
	if (outcol) {
		if (outcol->a == ALPHA_BG && !bgcolor) {
			rcolor.a = ALPHA_BG;
		}
		*outcol = rcolor;
	}
	free (fgcolor);
	return (*out && !outcol) ? strdup (out) : NULL;
}
Esempio n. 4
0
R_API char *r_cons_pal_parse (const char *str) {
	int i;
	ut8 r, g, b;
	char out[128];
	char *s = strdup (str);
	if (!s) return NULL;
	char *p = strchr (s + 1, ' ');
	out[0] = 0;
	if (p) *p++ = 0;
	if (!strcmp (str, "random")) {
		free (s);
		return r_cons_color_random (0);
	}
	if (!strncmp (s, "#", 1)) {
		if (strlen (s) == 7) {
#define C(x) (x >> 4)
			int R, G, B;
			sscanf (s, "%02x%02x%02x", &R, &G, &B);
			r_cons_rgb_str (out, C(R), C(G), C(B), 0);
		} else {
			eprintf ("Invalid html color code\n");
		}
	} else if (!strncmp (s, "rgb:", 4)) {
		if (strlen (s) == 7) {
			r = rgbnum (s[4], '0');
			g = rgbnum (s[5], '0');
			b = rgbnum (s[6], '0');
			r_cons_rgb_str (out, r, g, b, 0);
		} else if (strlen (s) == 10) {
			r = rgbnum (s[4], s[5]);
			g = rgbnum (s[6], s[7]);
			b = rgbnum (s[8], s[9]);
			r_cons_rgb_str (out, r, g, b, 0);
		}
	}
	if (p && !strncmp (p, "rgb:", 4)) {
		if (strlen (s) == 7) {
			r = rgbnum (p[4], '0');
			g = rgbnum (p[5], '0');
			b = rgbnum (p[6], '0');
			r_cons_rgb_str (out + strlen (out), r, g, b, 1);
		} else if (strlen (s) == 10) {
			r = rgbnum (p[4], p[5]);
			g = rgbnum (p[6], p[7]);
			b = rgbnum (p[8], p[9]);
			r_cons_rgb_str (out + strlen (out), r, g, b, 1);
		}
	}
	for (i = 0; colors[i].name; i++) {
		if (!strcmp (s, colors[i].name)) {
			strncat (out, colors[i].code,
				sizeof (out) - strlen (out) - 1);
		}
		if (p && !strcmp (p, colors[i].name)) {
			strncat (out, colors[i].bgcode,
				sizeof (out) - strlen (out) - 1);
		}
	}
	free (s);
	return *out ? strdup (out) : NULL;
}