Blackbox
Blackbox

Reputation: 1

What is the best design pattern for 'color' structure?

What would be the best design pattern to implement a 'color' data structure (in C#)? I would like to be able to represent a color in any of the color spaces - RGB, HSV, XYZ, Yxy, Luv, Lab, HSL etc.. and I would like to be able to convert from one format to another. I would like to support a fixed number of color spaces.

Upvotes: 0

Views: 293

Answers (1)

Marnix
Marnix

Reputation: 6547

You could create a helper class that can convert from one to another, so you can just use simple classes like Vector3 or of some sort to keep your data.

Because RGB, HSV and all are just 3 values, a Vector3 can be used for all the data. But then, it is up to the programmer to not get the wrong values into the wrong methods.

To assure the right type of value. We could use some Vector3 as the base class and then let every type (like RGB) inherit from the base class.

You could even choose to also create an "interclass" that's called Color. Which is an abstract class. The RGB class could then implement all abstract defined methods.

So you get:

  • Vector3 (most languages have some sort of class like this)
  • abstract Color extends Vector3 (has abstract methods)
  • RGB extends Color (implements all methods for conversion to other classes).

The abstract methods in Color that convert, will just return a Color itself. So you can override the method just like that.

Upvotes: 1

Related Questions