Udemy logo

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.

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.

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.

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.

Page Last Updated: April 2014

Top courses in C#

C# Intermediate: Classes, Interfaces and OOP
Mosh Hamedani
4.6 (26,267)
Bestseller
Complete C# Masterclass
Denis Panjuta, Tutorials.eu by Denis Panjuta
4.5 (15,752)
Design Patterns in C# and .NET
Dmitri Nesteruk
4.6 (8,177)
Bestseller
Learn Parallel Programming with C# and .NET
Dmitri Nesteruk
4.6 (2,403)
Bestseller
RPG Inventory Systems Asset Pack: Behind The Scenes
Sam Pattuzzi, GameDev.tv Team, Rick Davidson
4.6 (546)

More C# Courses

C# students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo