1
#include <Fl/Enumerations.H>
class Color
{
    public:
        static Color amber () {return fl_rgb_color (255, 204, 0);}
        static Color lighter_gray () {return fl_rgb_color (40, 40, 40); }
        static Color light_gray () {return fl_rgb_color (179, 179, 179);}
        static Color gray () {return fl_rgb_color (100, 100, 100);}
        static Color light_blue () {return fl_rgb_color (107, 107, 255);}
        static Color white () {return FL_WHITE;}
        static Color off_white() { return fl_rgb_color(225, 225, 225); }
        static Color cream() { return fl_rgb_color(204, 236, 255); }
        static Color black () {return FL_BLACK;}
        static Color red () {return FL_RED;}
        static Color green () {return FL_GREEN;}
        static Color dark_green () {return fl_rgb_color (0, 169, 45);}
        static Color blue () {return FL_BLUE;}
        static Color background () {return FL_BACKGROUND_COLOR;}

        Color (const Fl_Color& c = Color::black ()) : fl_color_ (c) {}
        operator Fl_Color () const {return fl_color_;}
        void make_current () const;

    private:
        Fl_Color fl_color_;
};

这是 Fl/Enumerations.h 中重要部分的摘录

enum Fl_Color { // standard colors
  // These are used as default colors in widgets and altered as necessary
  FL_FOREGROUND_COLOR   = 0,
  FL_BACKGROUND2_COLOR  = 7,
  FL_INACTIVE_COLOR = 8,
  FL_SELECTION_COLOR    = 15,

  // boxtypes generally limit themselves to these colors so
  // the whole ramp is not allocated:
  FL_GRAY0      = 32,   // 'A'
  FL_DARK3      = 39,   // 'H'
  FL_DARK2      = 45,   // 'N'
  FL_DARK1      = 47,   // 'P'
  FL_BACKGROUND_COLOR   = 49,   // 'R' default background color
  FL_LIGHT1     = 50,   // 'S'
  FL_LIGHT2     = 52,   // 'U'
  FL_LIGHT3     = 54,   // 'W'

  // FLTK provides a 5x8x5 color cube that is used with colormap visuals
  FL_BLACK      = 56,
  FL_RED        = 88,
  FL_GREEN      = 63,
  FL_YELLOW     = 95,
  FL_BLUE       = 216,
  FL_MAGENTA        = 248,
  FL_CYAN       = 223,
  FL_DARK_RED       = 72,

  FL_DARK_GREEN     = 60,
  FL_DARK_YELLOW    = 76,
  FL_DARK_BLUE      = 136,
  FL_DARK_MAGENTA   = 152,
  FL_DARK_CYAN      = 140,

  FL_WHITE      = 255
};

#define FL_FREE_COLOR       (Fl_Color)16
#define FL_NUM_FREE_COLOR   16
#define FL_GRAY_RAMP        (Fl_Color)32
#define FL_NUM_GRAY     24
#define FL_GRAY         FL_BACKGROUND_COLOR
#define FL_COLOR_CUBE       (Fl_Color)56
#define FL_NUM_RED      5
#define FL_NUM_GREEN        8
#define FL_NUM_BLUE     5

FL_EXPORT Fl_Color fl_inactive(Fl_Color c);
FL_EXPORT Fl_Color fl_contrast(Fl_Color fg, Fl_Color bg);
FL_EXPORT Fl_Color fl_color_average(Fl_Color c1, Fl_Color c2, float weight);
inline Fl_Color fl_lighter(Fl_Color c) { return fl_color_average(c, FL_WHITE, .67f); }
inline Fl_Color fl_darker(Fl_Color c) { return fl_color_average(c, FL_BLACK, .67f); }
inline Fl_Color fl_rgb_color(uchar r, uchar g, uchar b) {

  if (!r && !g && !b) return FL_BLACK;
  else return (Fl_Color)(((((r << 8) | g) << 8) | b) << 8);
}
inline Fl_Color fl_rgb_color(uchar g) {
  if (!g) return FL_BLACK;
  else return (Fl_Color)(((((g << 8) | g) << 8) | g) << 8);
}
inline Fl_Color fl_gray_ramp(int i) {return (Fl_Color)(i+FL_GRAY_RAMP);}
inline Fl_Color fl_color_cube(int r, int g, int b) {
  return (Fl_Color)((b*FL_NUM_RED + r) * FL_NUM_GREEN + g + FL_COLOR_CUBE);}

关于返回 FL_COLOR 枚举的静态类定义,我一直无法理解下一个类中发生了什么。我看不出 Fl_Color 如何对 Color 类有任何了解,以及编译器如何知道如何将 Fl_Color 转换为 Color。我确实意识到 Color 隐式转换为 Fl_Color,但我不认为转换是双向的。这是如何运作的?编译器是否只是调用构造函数并将返回值作为第一个参数传入?

4

4 回答 4

3

返回 all的静态方法返回Color对 的调用的结果fl_rgb_color

fl_rgb_color返回 aFl_ColorColor具有单个参数构造函数,该构造函数未标记explicit并引用 const Fl_Color,因此这是有效的隐式转换。

的返回值fl_rgb_color是一个临时值,但是因为通过引用Color获取Fl_Color参数,所以将临时值const绑定到构造函数参数是合法的。它的生命周期一直持续到Color对象(本身也是返回值的临时对象)的构造完成为止。

于 2009-12-03T18:55:57.323 回答
0

在返回之前隐式地在 F1_color 上运行 Color 构造函数

于 2009-12-03T18:53:50.603 回答
0

Color 的构造函数采用 Fl_Color 参数,因此编译器将在必要时隐式使用它来将 Fl_Color 转换为 Color。这是采用单个参数且未使用显式关键字标记的 C++ 构造函数的正常行为。

于 2009-12-03T18:55:43.297 回答
0

Fl_Color可以隐式转换为Color,因为具有作为参数的Color非显式构造函数。Fl_Color

于 2009-12-03T18:56:08.837 回答