Hex
Home
Classes
Hex
About
Hex class provides support for colors defined in hexadecimal format.
Usage
Next sections assume that you have some knowledge of hexadecimal numeral system.
Defining new hexadecimal color is simple as creating a new instance of Hex class. The only thing you need to do is pass your hex code as first and only one argument of the constructor. See the example below:
// Creates new HEX color
Hex red = new Hex("#FF0000");
// Creates new HEX color with 221 (DD) alpha value
Hex red = new Hex("#FF0000DD");
Thanks to an implicit conversion between Hex and System.String you can pass hex code directly as a string. There is also defined another implicit conversion between Hex and System.Drawing.Color. See example below:
// Implicit conversion between Hex and String classes
Hex blue = "#0000FF";
// Implicit conversion between Hex and Color classes
Hex red = Color.Red;
Those conversions really come in handy when you are working with WinForm’s Graphics class and it’s drawing functions. As you can see, defining colors in hexadecimal format is a much cleaner solution than using Color.FromArgb() method.
// Draws rectangle using classic Color class
g.DrawRectangle(new Pen(Color.FromArgb(255, 174, 114)), 0, 0, 100, 100);
// Draws same rectangle using Hex class
g.DrawRectangle(new Pen(new Hex("#FF9372")), 0, 0, 100, 100);
By using Value property you can always get hex code of the color. ToColor() method will explicitly convert Hex variable to classic System.Drawing.Color variable. Class also includes set of predefined colors which are taken from Google’s material design guidelines. See class content section below for complete list of colors.
Validation
Hex code can be written in many possible ways. If you are not dealing with an alpha channel (transparency), hex code is simply seven characters long string. The color itself is defined by six characters which are then prefixed with a hash (#) symbol - #RRGGBB. If you want to include an alpha channel, you can just add another two characters at the end of the string to represent alpha value - #RRGGBBAA. Except hash symbol, you can use only characters which are allowed in a hexadecimal numeral system. Those are numbers 1 - 9 and letters A - F or a - f. You can use Hex.Validate() method to ensure, that your hex code is well formatted. Hex codes are validated by ^#([0-9A-F]{6}|[0-9A-F]{8})$ regular expression. If your hex code is not valid during creation of the new Hex class instance, then new System.Format exception is thrown.
Class content
...
Constructors
Name |
Description |
|
Hex(string code) |
Initializes a new instance of the Hex class with provided hex code as a starting base color. |
|
Hex(Color color) |
Initializes a new instance of the Hex class with provided Color as a starting base color. |
|
Properties
Name |
Description |
Type |
- |
- |
- |
Fields
Name |
Description |
Type |
red |
#E53935 |
Hex |
pink |
#D81B60 |
Hex |
purple |
#8E24AA |
Hex |
deepPurple |
#5E35B1 |
Hex |
indigo |
#3949AB |
Hex |
blue |
#1E88E5 |
Hex |
seaBlue |
#304FFE |
Hex |
lightBlue |
#039BE5 |
Hex |
cyan |
#00ACC1 |
Hex |
teal |
#00897B |
Hex |
green |
#43A047 |
Hex |
lightGreen |
#7CB342 |
Hex |
lime |
#C0CA33 |
Hex |
yellow |
#FDD835 |
Hex |
amber |
#FFB300 |
Hex |
orange |
#FB8C00 |
Hex |
deepOrange |
#F4511E |
Hex |
brown |
#6D4C41 |
Hex |
grey |
#757575 |
Hex |
blueGray |
#546E7A |
Hex |
black |
#000000 |
Hex |
white |
#FFFFFF |
Hex |
Events
Name |
Description |
Type |
- |
- |
- |
Methods
Name |
Description |
Returns |
Validate(string code) |
Returns true if passed hex code have valid HEX format. |
bool |