I am having trouble determining which class structure to use for storing RGB/RGBA color information.
I am making a very simple 3D game engine for fun and to hone my OO-programming abilities.
I would like my engine to have support for both RGB and RGBA colors. On top of that, I would like to have it so RGB and RGBA values can be specified as either floats clamped to [0, 1] or unsigned chars. I figured that for storage purposes, it would be better to use unsigned chars as they use a fourth as much memory as floats.
So now, I have devised the following classes to use for this: Color, Color3, Color3f, Color3c, Color4, Color4f, and Color4c. Color is the superclass of Color3 and Color4, while Color3 is the superclass of Color3f and Color3c, as Color4 is with Color4f and Color4c.
This seems like a much more complicated approach than need be. I also thought for a while about using generics, so instead of Color3f and Color3c, one would use Color3<float> and Color3<unsigned char>. However, this approach didn't seem right either.
Ideally, I would like to be able to write code like:
Color3 red1(255, 0, 0);
Color3 red2(1.0f, 0.0f, 0.0f);
Color* someColor = new Color3(1.0f, 0.0f, 0.5f);
Color4 anotherColor = *someColor;
Color4 thisColor = anotherColor.toChar();
//thisColor.r = 255, thisColor.g = 0, thisColor.b = 127
thisColor = thisColor.toFloat();
//thisColor.r = 1.0f, thisColor.g = 0.0f, thisColor.b = 0.5f
Now this can't be implemented (at least to my knowledge) in C++, but how could I muster the same functionality without creating 7 separate classes? And without storing wasted information? For example, picture a 1024x1024 image in memory. That would be an array with over a million of these Colors, so how can I make a Color class hierarchy that is flexible and reusable? i.e., store RGB and RGBA values in different structures through unsigned chars, but provide functionality to retrieve float values?
Sorry if this isn't specific enough or what, this is my first question! Let me know what else would be helpful. I can post code for what I have tried so far if you guys would prefer, but hopefully you understand what I'm trying to accomplish.