The world is a colorful place, and in VB.NET we can represent these colors with the Color type. This type is part of the System.Drawing
namespace.
The System.Drawing
namespace is available on .NET on all platforms, including Windows, macOS and Linux. It makes working with colors easier.
To begin, we must use the "imports" keyword at the top to bring in the System.Drawing
namespace. This makes the program compile correctly.
class
, we can access different named colors like AliceBlue
, a light blue shade.Console
.AliceBlue
for this example).imports System.Drawing
Module Module1
Sub Main()
' Step 1: get a color with the Color type.
Dim color as Color = Color.AliceBlue
' Step 2: print color.
Console.WriteLine(color)
' Step 3: print ARGB data.
Console.WriteLine(color.ToArgb())
Console.WriteLine(color.A)
Console.WriteLine(color.R)
Console.WriteLine(color.G)
Console.WriteLine(color.B)
' Step 4: print brightness, hue and saturation data.
Console.WriteLine(color.GetBrightness())
Console.WriteLine(color.GetHue())
Console.WriteLine(color.GetSaturation())
End Sub
End ModuleColor [AliceBlue]
-984833
255
240
248
255
0.9705882
208
1
FromName
Suppose we have a name for a color like "white" or "yellow." We can convert this an actual Color type instance with the FromName
function.
imports System.Drawing
Module Module1
Sub Main()
' Get color from a string.
Dim yellow as Color = Color.FromName("yellow")
' Print RGB values for the color.
Console.WriteLine(yellow.R)
Console.WriteLine(yellow.G)
Console.WriteLine(yellow.B)
End Sub
End Module255
255
0
The Color type in VB.NET provides many properties to learn more about a specific color. We can convert colors from Strings with FromName
.