How can I create an enumeration for colors in a window shapes application?

I ran into a problem that I think should be fairly common and wondered if anyone could help.

I am creating a simple Windows form (using VB.NET) for a friend that I can use at work. His company has about 10 specific color schemes (they have a list of RGB values) that they use for the company logo, website, etc.

I want to follow this color scheme in my application and, to simplify the development, I would like to build an enumeration of these colors in order to avoid hard coding the RBG value for each label, panel, etc.

My initial thought was as follows:

Enum ColorTypes CompanyDarkBlue = Color.FromArgB(0,56,147) CompanyBlue = Color.FromArgB(0,111,198) CompanyLightBlue = Color.FromArgB(0,145,201) End Enum 

However, it is not as simple as a constant. I looked around the Internet and I found an example of how to achieve what I need, but it seemed too complicated for what seems like a fairly common requirement for application development.

What do you think is the best way to solve this problem?

Thank you so much

+6
enumeration
source share
4 answers

You can use the extension method. First simplify the listing:

 enum ColorType { CompanyDarkBlue, CompanyBlue, CompanyLightBlue } 

The extension method will look something like this:

 public static class ColorTypeExtensions { public static Color ToColor(this ColorType colorType) { switch(colorType) { case ColorType.CompanyDarkBlue: return Color.FromArgB(0,56,147); ... } } } 

This will allow you to write:

 ColorType.CompanyDarkBlue.ToColor(); 

For more information see C #: Enumerating Enums Using Extension Methods

+3
source share

I would not say that you need a listing here. When you mentioned the β€œcolor scheme,” the usual way to do this is to use polymorphism to allow for different color schemes.

This means that you will have this interface:

  public interface IColorScheme { Color CompanyDarkBlue { get; } Color CompanyBlue { get; } Color CompanyLightBlue { get; } } 

and then implement it the way you like:

  public class MyColorScheme : IColorScheme { public virtual Color CompanyDarkBlue { get { return Color.FromArgB(0,56,147); } } public virtual Color CompanyBlue { get { return Color.FromArgB(0,56,147); } } // etc. } 

Note that I also made these properties virtual, so that you can easily override only certain colors if you need a slightly different scheme.

To make the most of this abstraction, your program should basically use the properties of IColorScheme, not knowing about the actual implementation. The correct color scheme must be created separately during initialization.

+3
source share

Put these colors in a separate class and put them as properties, not Enumeration.

  public class myColor { public static Color CompanyDarkBlue { get { return Color.FromArgb(0, 56, 147); } } public static Color CompanyBlue { get { return Color.FromArgb(0, 111, 198); } } public static Color CompanyLightBlue { get { return Color.FromArgb(0, 145, 201); } } } 
+2
source share

You can save the same information in a class with common fields / properties and achieve the same effect as when enumerating.

I don’t see the point that numbering is needed here at all

0
source share

All Articles