I have a problem with ordering the constructor, I'm trying to find creative ways to solve it.
Basically, I have a simple Color class that stores information about RGB color and allows you to manipulate the specified color and convert it to other color spaces (24 bits, 16 bits, 4 bits, HSV, XYZ, LAB, etc.). The class itself works great.
I also have a library of predefined colors, for example:
namespace Colors { const Color Snow (255,250,250); const Color GhostWhite (248,248,255); const Color WhiteSmoke (245,245,245); const Color Gainsboro (220,220,220); const Color FloralWhite (255,250,240); const Color OldLace (253,245,230); const Color Linen (250,240,230); const Color AntiqueWhite (250,235,215); const Color PapayaWhip (255,239,213); const Color BlanchedAlmond (255,235,205); };
And they all work fine during normal use in the program.
My problem arises when I try to use these library colors in the constructor for another object. There is nothing to say that the constructor for the color of the library that I am using has been executed and color data has been assigned (it does a little preprocessing to calculate some color space values) before the constructor for another class that receives Color and assigns it a storage variable inside itself.
For example, the Color class has a constructor:
Color(const Color &c) { setColor(c.getRed(), c.getGreen(), c.getBlue()); }
And the operator = :
Color &Color::operator=(const Color &rhs) { setColor(rhs.getRed(), rhs.getGreen(), rhs.getBlue()); return *this; }
setColor() is just a small helper function that stores values ββand pre-computes alternative color space values.
When I include it in the constructor of another object, let's say:
Color _storeColor; TestClass(const Color &c) { _storeColor = c; }
or
Color _storeColor; TestClass(const Color &c) : _storeColor(c) {}
with:
TestClass myTest(Colors::WhiteSmoke);
the assigned color data (almost always) is all 0 , as if the constructor for the Color class is not already running, which is what I get completely.
So, I'm looking for ideas on how I can create my library of predefined colors so that they are available to other designers in the global area.
By the way, things like:
TestClass myTest(Color(245,245,245));
works fine, although I would prefer not to have hundreds (and hundreds) or #define macros for the color library, as this will cause a lot of unnecessary duplication of objects, and I would prefer to keep it as always referring to the same global instances when the color is reused .