2016-04-05 07:55:40 +08:00
|
|
|
// Color class.
|
2012-02-13 10:05:59 +08:00
|
|
|
#ifndef FISH_COLOR_H
|
|
|
|
#define FISH_COLOR_H
|
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <string.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <string>
|
2016-04-21 14:00:54 +08:00
|
|
|
|
2012-02-13 10:05:59 +08:00
|
|
|
#include "common.h"
|
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
// 24-bit color.
|
|
|
|
struct color24_t {
|
2014-09-20 06:37:31 +08:00
|
|
|
unsigned char rgb[3];
|
|
|
|
};
|
2012-02-13 10:05:59 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// 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, type_rgb, type_normal, type_reset };
|
|
|
|
unsigned char type : 4;
|
|
|
|
|
|
|
|
// Flags
|
2017-01-27 12:00:43 +08:00
|
|
|
enum {
|
|
|
|
flag_bold = 1 << 0,
|
|
|
|
flag_underline = 1 << 1,
|
|
|
|
flag_italics = 1 << 2,
|
|
|
|
flag_dim = 1 << 3,
|
|
|
|
flag_reverse = 1 << 4
|
|
|
|
};
|
2016-12-31 03:33:25 +08:00
|
|
|
unsigned char flags : 5;
|
2016-04-28 06:28:34 +08:00
|
|
|
|
|
|
|
union {
|
|
|
|
unsigned char name_idx; // 0-10
|
2014-09-20 06:37:31 +08:00
|
|
|
color24_t color;
|
2012-02-13 10:05:59 +08:00
|
|
|
} data;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Try parsing a special color name like "normal".
|
2012-02-13 10:05:59 +08:00
|
|
|
bool try_parse_special(const wcstring &str);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Try parsing an rgb color like "#F0A030".
|
2012-02-13 10:05:59 +08:00
|
|
|
bool try_parse_rgb(const wcstring &str);
|
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Try parsing an explicit color name like "magenta".
|
2012-02-13 10:05:59 +08:00
|
|
|
bool try_parse_named(const wcstring &str);
|
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Parsing entry point.
|
2012-03-14 05:22:53 +08:00
|
|
|
void parse(const wcstring &str);
|
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Private constructor.
|
|
|
|
explicit rgb_color_t(unsigned char t, unsigned char i = 0);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
public:
|
|
|
|
/// Default constructor of type none.
|
2012-03-11 08:15:56 +08:00
|
|
|
explicit rgb_color_t() : type(type_none), flags(), data() {}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Parse a color from a string.
|
2012-02-13 10:05:59 +08:00
|
|
|
explicit rgb_color_t(const wcstring &str);
|
2012-03-14 05:22:53 +08:00
|
|
|
explicit rgb_color_t(const std::string &str);
|
2012-02-13 10:05:59 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns white.
|
2012-02-13 10:05:59 +08:00
|
|
|
static rgb_color_t white();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns black.
|
2012-02-13 10:05:59 +08:00
|
|
|
static rgb_color_t black();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns the reset special color.
|
2012-02-13 10:05:59 +08:00
|
|
|
static rgb_color_t reset();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns the normal special color.
|
2012-02-13 10:05:59 +08:00
|
|
|
static rgb_color_t normal();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns the none special color.
|
2012-02-13 10:05:59 +08:00
|
|
|
static rgb_color_t none();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns whether the color is the normal special color.
|
|
|
|
bool is_normal(void) const { return type == type_normal; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns whether the color is the reset special color.
|
|
|
|
bool is_reset(void) const { return type == type_reset; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns whether the color is the none special color.
|
|
|
|
bool is_none(void) const { return type == type_none; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns whether the color is a named color (like "magenta").
|
|
|
|
bool is_named(void) const { return type == type_named; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns whether the color is specified via RGB components.
|
|
|
|
bool is_rgb(void) const { return type == type_rgb; }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns whether the color is special, that is, not rgb or named.
|
|
|
|
bool is_special(void) const { return type != type_named && type != type_rgb; }
|
2012-02-13 10:05:59 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns a description of the color.
|
2012-02-13 10:05:59 +08:00
|
|
|
wcstring description() const;
|
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns the name index for the given color. Requires that the color be named or RGB.
|
2012-02-13 10:05:59 +08:00
|
|
|
unsigned char to_name_index() const;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns the term256 index for the given color. Requires that the color be RGB.
|
2012-02-13 10:05:59 +08:00
|
|
|
unsigned char to_term256_index() const;
|
2014-09-20 06:37:31 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns the 24 bit color for the given color. Requires that the color be RGB.
|
2014-09-20 06:37:31 +08:00
|
|
|
color24_t to_color24() const;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns whether the color is bold.
|
2016-10-21 12:14:40 +08:00
|
|
|
bool is_bold() const { return static_cast<bool>(flags & flag_bold); }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Set whether the color is bold.
|
|
|
|
void set_bold(bool x) {
|
|
|
|
if (x)
|
|
|
|
flags |= flag_bold;
|
|
|
|
else
|
|
|
|
flags &= ~flag_bold;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns whether the color is underlined.
|
2016-10-21 12:14:40 +08:00
|
|
|
bool is_underline() const { return static_cast<bool>(flags & flag_underline); }
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Set whether the color is underlined.
|
|
|
|
void set_underline(bool x) {
|
|
|
|
if (x)
|
|
|
|
flags |= flag_underline;
|
|
|
|
else
|
|
|
|
flags &= ~flag_underline;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-12-31 03:33:25 +08:00
|
|
|
/// Returns whether the color is italics.
|
|
|
|
bool is_italics() const { return static_cast<bool>(flags & flag_italics); }
|
|
|
|
|
|
|
|
/// Set whether the color is italics.
|
|
|
|
void set_italics(bool x) {
|
|
|
|
if (x)
|
|
|
|
flags |= flag_italics;
|
|
|
|
else
|
|
|
|
flags &= ~flag_italics;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether the color is dim.
|
|
|
|
bool is_dim() const { return static_cast<bool>(flags & flag_dim); }
|
|
|
|
|
|
|
|
/// Set whether the color is dim.
|
|
|
|
void set_dim(bool x) {
|
|
|
|
if (x)
|
|
|
|
flags |= flag_dim;
|
|
|
|
else
|
|
|
|
flags &= ~flag_dim;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether the color is reverse.
|
|
|
|
bool is_reverse() const { return static_cast<bool>(flags & flag_reverse); }
|
|
|
|
|
|
|
|
/// Set whether the color is reverse.
|
|
|
|
void set_reverse(bool x) {
|
|
|
|
if (x)
|
|
|
|
flags |= flag_reverse;
|
|
|
|
else
|
|
|
|
flags &= ~flag_reverse;
|
|
|
|
}
|
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Compare two colors for equality.
|
|
|
|
bool operator==(const rgb_color_t &other) const {
|
|
|
|
return type == other.type && !memcmp(&data, &other.data, sizeof data);
|
2012-02-13 10:05:59 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Compare two colors for inequality.
|
|
|
|
bool operator!=(const rgb_color_t &other) const { return !(*this == other); }
|
2013-02-15 07:50:24 +08:00
|
|
|
|
2016-04-28 06:28:34 +08:00
|
|
|
/// Returns the names of all named colors.
|
2013-02-15 07:50:24 +08:00
|
|
|
static wcstring_list_t named_color_names(void);
|
2012-02-13 10:05:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|