Skip to main content
GameDev.net gamedev.net
🔒 Locked

Color spectrum operations

Started by uutee Jul 30, 2004 at 9:50 AM 8 replies 850+ views
Original Post
uutee
uutee
Hi, So far, I've been using my Vector3D class for storing both "geometric" vectors *and* RGB colors. This class neatly implements such operations as plus and minus. It also defines the dot product through multiplication, ie. a*b = a.x*b.x + a.y*b.y + a.z*.z This is all basic for geometric vectors. But for colors, the dot product seems rather "artifical". There are often times when we have two colors, and we want to get a frequency-to-frequency multiplication of them. In practical terms, this would be: a*b = Vector3D( a.x*b.x, a.y*b.y, a.z*b.z ) However, the multiplication is already reserved for dot product, so I'm currently defining a following operation: Vector3D ComponentMul( Vector3D& a, Vector3D& b ) { return Vector3D(a.x*b.x, a.y*b.y, a.z*b.z); } But recently I thought that I could, perhaps, store color information in a Spectrum class. This would not necessarily even have to be plain RGB! I thought RGB would be a realistic choice though. Then, for the spectrum class, we could define the multiplication as convenient for spectra (frequency-to-frequency): a*b = SpectrumRGB( a.r*b.r, a.g*b.g, a.b*b.b ); Now my Only Question here is: 1.) Does this kind of multiplication on spectrums have any kind of "theoretical background", ie. is it a common operation to multiply two spectra this way? - Mikko
LilBudyWizer
LilBudyWizer
That is called a direct product. The direct sum and product is a means to define a new ring from an existing ring.
Keys to success: Ability, ambition and opportunity.
uutee
uutee
Thanks for the replies, fellows! ^_^

So, do *You* guys see it as a good idea to define a new Color (or Spectrum) class which implements its multiplication as a direct product? Is it a common manner?

- Mikko
Motorherp
Motorherp
If you're not a C++aphobe you could have all the functionality that is shared between the two in a base class which is then inhereted by the vector and colour classes. Any unique functionality can then be built on top. This way it saves you having two identical copies of the code. It's often nice to do things this way as its easier to fix mistakes or make changes later since them changes will propogate through all the inheriting classes.
[size="1"] [size="4"]:: SHMUP-DEV ::
Dmytry
Dmytry
i don't think it's reasonable to "inherit" two methods.
Also note that spectrum may need more than 3 components,and it sometimes may be useful for realism.
Motorherp
Motorherp
I think you mis-understand, there would only be one base class which is inherited from, which contained all the shared functionality. Both vector and colour class would then inherit this base class thus gaining all that basic functionality. If you want to add any more functions at a later date that both vector and colour classes should share then they can be added to the base class and both vector and colour would automatically get this new function. Any functions which are specific to colour should then be added in the colour class so that they are not inherited by vector and can only be used within colour, and vice versa. The other option would be to forget a base class and have your colour class inherit your vector class. Any functions which should act differently for colour can then be overloaded. It might sound complicated but it really isn't and it'll make your code much less error prone further down the road when you are debugging and expanding.
[size="1"] [size="4"]:: SHMUP-DEV ::
Stoffel
Stoffel
"Only use polymorphism when you are going to treat derived classes as objects of the base class.
Never use polymorphism to share code."
(either Exceptional C++ or More Exceptional C++, forget which)

I could be mistaken, but it sounds to me like you're trying to make a base class with common functionality just so you can share that code in two completely different vector classes. I think this will lead to lots of trouble down the road.

Sounds to me like you should rather just make a Vector class, use it for motion and colors and whatever you want, but have all the elementary functions you want as part of that class. I wouldn't make a separate class for each usage.
Motorherp
Motorherp
Quote:
Original post by Stoffel
I could be mistaken, but it sounds to me like you're trying to make a base class with common functionality just so you can share that code....

Thats exactly what I'm suggesting to do. From the original post I get the impression that the vector and colour classes are identical for the most part (rather than completely different) and only differ in certain key functions for which you would want to share the same operator or function name. Please correct me if I'm wrong but I can't see why the method I suggest wouldn't be the most sensible solution.
[size="1"] [size="4"]:: SHMUP-DEV ::
LilBudyWizer
LilBudyWizer
More direct to your question I wouldn't define multiplication of vectors to be the dot product. Rather if I was going to define multiplication it would be the direct product. The dot product returns a scalar which is not a vector and as such does not behave as normal multiplication. The cross product does produce a vector, but that vector is not necessarily in the vector space defined by the two vectors being multiplied. The direct product is in the same vector space, i.e. the direct product is a linear combination of the two vectors being multiplied. Specifically you can solve (a0*b0,a1*b1)=c0*(a0,a1)+c1*(b0,b1) for scalars c0 and c1. Additionally that definition for multiplication works two vectors of any number of dimensions.
Keys to success: Ability, ambition and opportunity.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.