Tweak rgb_color_t back down to 4 bytes

This commit is contained in:
ridiculousfish 2012-03-10 16:15:56 -08:00
parent 6d3f257439
commit d81bd296fc
2 changed files with 33 additions and 12 deletions

View file

@ -135,7 +135,17 @@ bool rgb_color_t::try_parse_named(const wcstring &str) {
return false;
}
rgb_color_t::rgb_color_t(unsigned char t, unsigned char i) : type(t), data(), flags() {
static const wchar_t *name_for_color_idx(unsigned char idx) {
size_t max = sizeof named_colors / sizeof *named_colors;
for (size_t i=0; i < max; i++) {
if (named_colors[i].idx == idx) {
return named_colors[i].name;
}
}
return L"unknown";
}
rgb_color_t::rgb_color_t(unsigned char t, unsigned char i) : type(t), flags(), data() {
data.name_idx = i;
}
@ -240,15 +250,17 @@ wcstring rgb_color_t::description() const {
case type_none:
return L"none";
case type_named:
return format_string(L"named(%d)", (int)data.name_idx);
return format_string(L"named(%d: %ls)", (int)data.name_idx, name_for_color_idx(data.name_idx));
case type_rgb:
return format_string(L"rgb(0x%02x%02x%02x)", data.rgb[0], data.rgb[1], data.rgb[2]);
case type_reset:
return L"reset";
case type_normal:
return L"normal";
case type_ignore:
return L"ignore";
default:
abort();
return L"";
}
}
}

27
color.h
View file

@ -9,8 +9,10 @@
#include "common.h"
/* A type that represents a color */
/* A type that represents a color. We work hard to keep it at a size of 4 bytes. */
class rgb_color_t {
/* Types */
enum {
type_none,
type_named,
@ -19,13 +21,20 @@ class rgb_color_t {
type_reset,
type_ignore
};
unsigned char type;
unsigned char type:4;
/* Flags */
enum {
flag_bold = 1 << 0,
flag_underline = 1 << 1
};
unsigned char flags:4;
union {
unsigned char name_idx; //0-10
unsigned char rgb[3];
} data;
unsigned flags;
/** Try parsing a special color name like "normal" */
bool try_parse_special(const wcstring &str);
@ -41,7 +50,7 @@ class rgb_color_t {
public:
/** Default constructor of type none */
explicit rgb_color_t() : type(type_none), data(), flags() {}
explicit rgb_color_t() : type(type_none), flags(), data() {}
/** Parse a color from a string */
explicit rgb_color_t(const wcstring &str);
@ -95,16 +104,16 @@ class rgb_color_t {
unsigned char to_term256_index() const;
/** Returns whether the color is bold */
bool is_bold() const { return flags & 1; }
bool is_bold() const { return flags & flag_bold; }
/** Set whether the color is bold */
void set_bold(bool x) { if (x) flags |= 1; else flags &= ~1; }
void set_bold(bool x) { if (x) flags |= flag_bold; else flags &= ~flag_bold; }
/** Returns whether the color is underlined */
bool is_underline() const { return flags & 2; }
bool is_underline() const { return !! (flags & flag_underline); }
/** Set whether the color is underlined */
void set_underline(bool x) { if (x) flags |= 2; else flags &= ~2; }
void set_underline(bool x) { if (x) flags |= flag_underline; else flags &= ~flag_underline; }
/** Compare two colors for equality */
bool operator==(const rgb_color_t &other) const {