Home
VB.NET
Color Examples
This page was last reviewed on Nov 14, 2023.
Dot Net Perls
Color. 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.
Example. To begin, we must use the "imports" keyword at the top to bring in the System.Drawing namespace. This makes the program compile correctly.
Step 1 On the Color Shared class, we can access different named colors like AliceBlue, a light blue shade.
Step 2 For debugging purposes, we can print the color local variable to the Console.
Step 3 The Color internally keeps track of its ARGB data (which is alpha, red, gree and blue). With properties we can access this information.
Property
Step 4 We print information about the brightness, hue and saturation of the color (which is 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 Module
Color [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 Module
255 255 0
Summary. The Color type in VB.NET provides many properties to learn more about a specific color. We can convert colors from Strings with FromName.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 14, 2023 (new).
Home
Changes
© 2007-2024 Sam Allen.