From d81bd296fcafb9a8342fc48d0f7a8674afef020b Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 10 Mar 2012 16:15:56 -0800 Subject: [PATCH] Tweak rgb_color_t back down to 4 bytes --- color.cpp | 18 +++++++++++++++--- color.h | 27 ++++++++++++++++++--------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/color.cpp b/color.cpp index 1b855db43..b6ffcb608 100644 --- a/color.cpp +++ b/color.cpp @@ -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""; } -} \ No newline at end of file +} diff --git a/color.h b/color.h index f0e17990b..2e86a6753 100644 --- a/color.h +++ b/color.h @@ -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 {