C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: Colors can be represented as ARGB values, which store the alpha transparency, as well as the red, green and blue values.
Bytes: These values are stored as bytes which gives them a range of 0 to 255 inclusive.
ByteInfo: If you try to execute this program and it won't compile, add the System.Drawing assembly through the interface in Visual Studio.
C# program that demonstrates Color type
using System;
using System.Drawing;
class Program
{
static void Main()
{
// Get the AliceBlue color.
Color color = Color.AliceBlue;
// Write some details about the color.
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());
}
}
Output
Color [AliceBlue]
-984833
255
240
248
255
0.9705882
208
1
And: Then convert it into a regular color. This lets you examine the components of the color programmatically.
C# program that uses known colors
using System;
using System.Drawing;
class Program
{
static void Main()
{
// You can see what the menu text color is.
Color color = Color.FromKnownColor(KnownColor.MenuText);
// All values are zero so it is black.
Console.WriteLine(color.R);
Console.WriteLine(color.G);
Console.WriteLine(color.B);
}
}
Output
0
0
0
Tip: You can persist the ARGB integer to text files or databases to save storage space. It will occupy less memory too.
And: Please notice how the ToArgb() and FromArgb() methods are used to go back and forth.
C# program that uses ARGB values
using System.Drawing;
class Program
{
static void Main()
{
// Get ARGB of black.
int argb = Color.Black.ToArgb();
// Get the color black by using the argb integer.
Color color = Color.FromArgb(argb);
}
}
Note: This won't work for color names that are not recognized in the list of properties.
C# program that uses FromName and string
using System;
using System.Drawing;
class Program
{
static void Main()
{
// Create the color from a string.
Color white = Color.FromName("white");
// Color will have appropriate R G B values.
Console.WriteLine(white.R);
Console.WriteLine(white.G);
Console.WriteLine(white.B);
}
}
Output
255
255
255
Here: This example shows one case where the color is empty, and one case where the color is not empty.
C# program that uses empty colors
using System;
using System.Drawing;
class Program
{
static void Main()
{
// Color is a struct so it cannot be null.
// ... Use Color.Empty instead.
Color color = Color.Empty;
Console.WriteLine(color.IsEmpty);
color = Color.Black;
Console.WriteLine(color.IsEmpty);
}
}
Output
True
False
Example: We use the Color.FromName method to convert the string value into an actual color. We then set the form's background to that color.
C# program that uses BackColor property
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication16
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// You can assign colors to the BackColor property in Windows Forms.
this.BackColor = Color.FromName("green");
}
}
}
So: You can access known colors, convert colors to integers, parse colors, and generally make the world a brighter place with this type.