version 1.13, 2016/06/06 10:12:58 |
version 1.14, 2016/07/15 00:42:56 |
|
|
|
|
/* If we have hit the colour exactly, return early. */ |
/* If we have hit the colour exactly, return early. */ |
if (cr == r && cg == g && cb == b) |
if (cr == r && cg == g && cb == b) |
return (16 + (36 * qr) + (6 * qg) + qb); |
return ((16 + (36 * qr) + (6 * qg) + qb) | COLOUR_FLAG_256); |
|
|
/* Work out the closest grey (average of RGB). */ |
/* Work out the closest grey (average of RGB). */ |
grey_avg = (r + g + b) / 3; |
grey_avg = (r + g + b) / 3; |
|
|
idx = 232 + grey_idx; |
idx = 232 + grey_idx; |
else |
else |
idx = 16 + (36 * qr) + (6 * qg) + qb; |
idx = 16 + (36 * qr) + (6 * qg) + qb; |
return (idx); |
return (idx | COLOUR_FLAG_256); |
} |
} |
|
|
/* Set grid cell foreground colour. */ |
/* Join RGB into a colour. */ |
void |
int |
colour_set_fg(struct grid_cell *gc, int c) |
colour_join_rgb(u_char r, u_char g, u_char b) |
{ |
{ |
if (c & 0x100) |
return ((((int)((r) & 0xff)) << 16) | |
gc->flags |= GRID_FLAG_FG256; |
(((int)((g) & 0xff)) << 8) | |
gc->fg = c; |
(((int)((b) & 0xff))) | COLOUR_FLAG_RGB); |
} |
} |
|
|
/* Set grid cell background colour. */ |
/* Split colour into RGB. */ |
void |
void |
colour_set_bg(struct grid_cell *gc, int c) |
colour_split_rgb(int c, u_char *r, u_char *g, u_char *b) |
{ |
{ |
if (c & 0x100) |
*r = (c >> 16) & 0xff; |
gc->flags |= GRID_FLAG_BG256; |
*g = (c >> 8) & 0xff; |
gc->bg = c; |
*b = c & 0xff; |
} |
} |
|
|
/* Convert colour to a string. */ |
/* Convert colour to a string. */ |
|
|
colour_tostring(int c) |
colour_tostring(int c) |
{ |
{ |
static char s[32]; |
static char s[32]; |
|
u_char r, g, b; |
|
|
if (c & 0x100) { |
if (c & COLOUR_FLAG_RGB) { |
xsnprintf(s, sizeof s, "colour%d", c & ~0x100); |
colour_split_rgb(c, &r, &g, &b); |
|
xsnprintf(s, sizeof s, "#%02x%02x%02x", r, g, b); |
return (s); |
return (s); |
} |
} |
|
|
|
if (c & COLOUR_FLAG_256) { |
|
xsnprintf(s, sizeof s, "colour%u", c & 0xff); |
|
return (s); |
|
} |
|
|
switch (c) { |
switch (c) { |
case 0: |
case 0: |
return ("black"); |
return ("black"); |
|
|
n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &r, &g, &b); |
n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &r, &g, &b); |
if (n != 3) |
if (n != 3) |
return (-1); |
return (-1); |
return (colour_find_rgb(r, g, b) | 0x100); |
return (colour_join_rgb(r, g, b)); |
} |
} |
|
|
if (strncasecmp(s, "colour", (sizeof "colour") - 1) == 0) { |
if (strncasecmp(s, "colour", (sizeof "colour") - 1) == 0) { |
n = strtonum(s + (sizeof "colour") - 1, 0, 255, &errstr); |
n = strtonum(s + (sizeof "colour") - 1, 0, 255, &errstr); |
if (errstr != NULL) |
if (errstr != NULL) |
return (-1); |
return (-1); |
return (n | 0x100); |
return (n | COLOUR_FLAG_256); |
} |
} |
|
|
if (strcasecmp(s, "black") == 0 || strcmp(s, "0") == 0) |
if (strcasecmp(s, "black") == 0 || strcmp(s, "0") == 0) |