C# Color Struct and Class Tutorial

c# colorIn the C# programming language, colors is both a class and a struct. In this guide, we’ll talk a little bit about both and demonstrate some of the ways you can use C# color, along with related concepts such as methods and operators. You’ll also find a full list of system defined colors you can call in C#, plus their associated ARGB values.

For more, check out this introductory C# and .NET programming course for beginners.

What is C#?

C# or C Sharp is a functional, object, class, and component-oriented programming language developed by Microsoft for its .NET initiative. It is implemented in Visual C# and the .NET Framework, and influenced heavily by C++ and Java, among others.

What is .NET?

C# and .NET go hand in hand. Basically, the .NET framework – developed by Microsoft – is a huge library written in multiple programming languages, used for building and deploying a variety of desktop applications and Web services that operate with .NET technology. You can learn the fundamentals of C# in this introductory course.

C# Color Struct

In the C# programming language, color is a struct that allows you to access, convert, and parse known colors in the system. A struct is a complex data type in C, and other derivatives of C, that packages together lists of related data and puts them in one block of memory. You would use this for something like grouping the coordinates of a shape. Confusing? Check out this guide to learn about the basics of C#.

In terms of syntax, defining a struct would look like this:

struct Point {
   int a;
   int b;
};

The colors that the C# color struct can define are described in terms of four channels: alpha, red, green, and blue, where alpha stores transparency and red, green, and blue store their respective RGB values.

C# System Defined Colors & ARGB Values

Below you’ll find a full list of the system defined colors you can access in C# as part of the .NET framework, along with their ARGB values. Don’t understand what RGB means? Check out this guide on both the RGB and CMYK color models.

  • AliceBlue – #FFF0F8FF
  • AntiqueWhite – #FFFAEBD7
  • Aqua – #FF00FFFF
  • Aquamarine – #FF7FFFD4
  • Azure – #FFF0FFFF
  • Beige – #FFF5F5DC
  • Bisque – #FFFFE4C4
  • Black – #FF000000
  • BlanchedAlmond – #FFFFEBCD
  • Blue – #FF0000FF
  • BlueViolet – #FF8A2BE2
  • Brown – #FFA52A2A
  • BurlyWood – #FFDEB887
  • CadetBlue – #FF5F9EA0
  • Chartreuse – #FF7FFF00
  • Chocolate – #FFD2691E
  • Coral – #FFFF7F50
  • CornflowerBlue – #FF6495ED
  • Cornsilk – #FFFFF8DC
  • Crimson – #FFDC143C
  • Cyan – #FF00FFFF
  • DarkBlue – #FF00008B
  • DarkCyan – #FF008B8B
  • DarkGoldenrod – #FFB8860B
  • DarkGray – #FFA9A9A9
  • DarkGreen – #FF006400
  • DarkKhaki – #FFBDB76B
  • DarkMagenta – #FF8B008B
  • DarkOliveGreen – #FF556B2F
  • DarkOrange – #FFFF8C00
  • DarkOrchid – #FF9932CC
  • DarkRed – #FF8B0000
  • DarkSalmon – #FFE9967A
  • DarkSeaGreen – #FF8FBC8F
  • DarkSlateBlue – #FF483D8B
  • DarkSlateGray – #FF2F4F4F
  • DarkTurquoise – #FF00CED1
  • DarkViolet – #FF9400D3
  • DeepPink – #FFFF1493
  • DeepSkyBlue – #FF00BFFF
  • DimGray – #FF696969
  • DodgerBlue – #FF1E90FF
  • Firebrick – #FFB22222
  • FloralWhite – #FFFFFAF0
  • ForestGreen – #FF228B22
  • Fuchsia – #FFFF00FF
  • Gainsboro – #FFDCDCDC
  • GhostWhite – #FFF8F8FF
  • Gold – #FFFFD700
  • Goldenrod – #FFDAA520
  • Gray – #FF808080
  • Green – #FF008000
  • GreenYellow – #FFADFF2F
  • Honeydew – #FFF0FFF0
  • HotPink – #FFFF69B4
  • IndianRed – #FFCD5C5C
  • Indigo – #FF4B0082
  • Ivory – #FFFFFFF0
  • Khaki – #FFF0E68C
  • Lavender – #FFE6E6FA
  • LavenderBlush – #FFFFF0F5
  • LawnGreen – #FF7CFC00
  • LemonChiffon – #FFFFFACD
  • LightBlue – #FFADD8E6
  • LightCoral – #FFF08080
  • LightCyan – #FFE0FFFF
  • LightGoldenrodYellow – #FFFAFAD2
  • LightGray – #FFD3D3D3
  • LightGreen – #FF90EE90
  • LightPink – #FFFFB6C1
  • LightSalmon – #FFFFA07A
  • LightSeaGreen – #FF20B2AA
  • LightSkyBlue – #FF87CEFA
  • LightSlateGray – #FF778899
  • LightSteelBlue – #FFB0C4DE
  • LightYellow – #FFFFFFE0
  • ime – #FF00FF00
  • LimeGreen – #FF32CD32
  • Linen – #FFFAF0E6
  • Magenta – #FFFF00FF
  • Maroon – #FF800000
  • MediumAquamarine – #FF66CDAA
  • MediumBlue – #FF0000CD
  • MediumOrchid – #FFBA55D3
  • MediumPurple – #FF9370DB
  • MediumSeaGreen – #FF3CB371
  • MediumSlateBlue – #FF7B68EE
  • MediumSpringGreen – #FF00FA9A
  • MediumTurquoise – #FF48D1CC
  • MediumVioletRed – #FFC71585
  • MidnightBlue – #FF191970
  • MintCream – #FFF5FFFA
  • MistyRose – #FFFFE4E1
  • Moccasin – #FFFFE4B5
  • NavajoWhite – #FFFFDEAD
  • Navy – #FF000080
  • OldLace – #FFFDF5E6
  • Olive – #FF808000
  • OliveDrab – #FF6B8E23
  • Orange – #FFFFA500
  • OrangeRed – #FFFF4500
  • Orchid – #FFDA70D6
  • PaleGoldenrod – #FFEEE8AA
  • PaleGreen – #FF98FB98
  • PaleTurquoise – #FFAFEEEE
  • PaleVioletRed – #FFDB7093
  • PapayaWhip – #FFFFEFD5
  • PeachPuff – #FFFFDAB9
  • Peru – #FFCD853F
  • Pink – #FFFFC0CB
  • Plum – #FFDDA0DD
  • PowderBlue – #FFB0E0E6
  • Purple – #FF800080
  • Red – #FFFF0000
  • RosyBrown – #FFBC8F8F
  • RoyalBlue – #FF4169E1
  • SaddleBrown – #FF8B4513
  • Salmon – #FFFA8072
  • SandyBrown – #FFF4A460
  • SeaGreen – #FF2E8B57
  • SeaShell – #FFFFF5EE
  • Sienna – #FFA0522D
  • Silver – #FFC0C0C0
  • SkyBlue – #FF87CEEB
  • SlateBlue – #FF6A5ACD
  • SlateGray – #FF708090
  • Snow – #FFFFFAFA
  • SpringGreen – #FF00FF7F
  • SteelBlue – #FF4682B4
  • Tan – #FFD2B48C
  • Teal – #FF008080
  • Thistle – #FFD8BFD8
  • Tomato – #FFFF6347
  • Transparent – #00FFFFFF
  • Turquoise – #FF40E0D0
  • Violet – #FFEE82EE
  • Wheat – #FFF5DEB3
  • White – #FFFFFFFF
  • WhiteSmoke – #FFF5F5F5
  • Yellow – #FFFFFF00
  • YellowGreen – #FF9ACD32

C# Color Struct Methods

Below you’ll find a list of the methods the C# color struct allows you to call. For more, check out this C# programming course for beginners. If you don’t know what a method is, consider checking out this programming course for non-programmers to learn some coding fundamentals.

  • Add – Adds together two color structs.
  • AreClose – Compares two color structs for equality.
  • Clamp – If the color’s ScRGB channels are outside the 0 to 1 range, it sets it within that range.
  • Equals(Color) – Tests whether or not a color structure is identical to a color.
  • Equals(Object) – Tests whether or not an object is a color structure equivalent to that color.
  • Equals(Color, Color) –  Checks to see if two color structures are identical or not.
  • FromArgb –  Creates a new color structure using specified  color channel values and the sRGB alpha channel.
  • FromAValues – Uses specified color and alpha channel values, plus color profile, to create a new color struct.
  • FromRgb – Uses the specified values of the sRGB color channel to create a new color struct.
  • FromScRgb – Same as above, only with ScRGB color channels.
  • FromValues – Uses color channel values and profile to create new color struct.
  • GetHashCode – Retrieve a hash code for the specified color struct.
  • GetNativeColorValues – Retrieves the color channel values for a color.
  • GetType – Retrieves the type for the current instance, which is inherited from object.
  • Multiply – Takes a color struct and multiplies its ARGB channels by a specified value.
  • Subtract – Subtracts one color structure from another.
  • ToString() – Uses the sRGB channels  to create a string representation of a color struct.
  • ToString(IFormatProvider) – Same as above, except it uses the specified format provider as well.

Check out this guide to learn about extension methods in C#.

C# Color Struct Operators

These are some of your standard programming operators, and don’t require much explanation. If you’re feeling lost, consider checking out this programming for complete beginners tutorial, which teaches using the C# programming language.

  • Addition – Add together two color structs.
  • Subtraction – Subtracts one color struct from another color struct.
  • Equality – Checks whether or not two color structs are identical.
  • Inequality – Checks whether or not two color structs are not identical.
  • Multiply – Multiples a specific color struct’s alpha, red, blue, and green channels by a designated value.

C# Color Examples

So what can you actually do with all of these colors? To give us a better idea of how the C# color struct actually works, and what you can do with system defined colors, here’s a simple program that will access a system defined color and describe some of its attributes:

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
	Color color = Color.AliceBlue;
	Console.WriteLine(color);
	Console.WriteLine(color.ToArgb());
	Console.WriteLine(color.A);
	Console.WriteLine(color.R);
	Console.WriteLine(color.G);
	Console.WriteLine(color.B);
	Console.WriteLine(color.GetBrightness());
	Console.WriteLine(color.GetHue());
	Console.WriteLine(color.GetSaturation());
    }
}

In the example above, we first tell the program to get the color AliceBlue, and then describe some of its attributes, such as its individual ARGB byte values, its brightness, its hue, and its saturation. This will return the following:

Color [AliceBlue]
-984833
255
240
248
255
0.9705882
208
1

Color Class – Painting With C# Colors

Let’s say you wanted to paint a red rectangle in C#. You could do this by using one of the predefined brushes. For example:

Rectangle myBrush = new Rectangle();
myBrush.Width = 150;
myBrush.Height = 150;
myBrush.Fill = Brushes.Red;

Here, we simply denote the color Red. What if we wanted to paint a blue rectangle by creating a new SolidColorBrush and manually defining its RGB values?

Rectangle myRectangle = new Rectangle();
myRectangle.Width = 150;
myRectangle.Height = 150;
SolidColorBrush myBrush = new SolidColorBrush();

myBrush.Color = Color.FromArgb(255, 0, 0, 255);
myRectangle.Fill = myBrush;

These are only some of the things you can do using the C# color struct and class. For more on C# programming, check out part 1 of this C# fundamentals course. Don’t forget to check out part 2 and part 3 as well.