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