Colour
Any modern graphics system has to have a good way of defining and using colours. Usually, colours are defined as a blend of Red, Green and Blue which make the additive primary colours.
Colours must always be thought of as a mixture of these three primary values in specific percentages. For example 0% of all three colours is black. 100% of all three is white. When you create colours it's by using a specific mixture of red, green and blue.
The values of each of the primary colours might be expressed in different ways. Sometime you might see a colour value that's a floating point value in the range 0-1 where 0 is 0% and 1 is 100%. Sometimes you may see the primary colour expressed as an integer value between 0 and 255. This is because many imaging systems use collections of bytes to hold colour values and the maximum number a byte can hold is 255. In this case 0% is 0 and 100% is 255. In either case, if you try to think in percentages you'll be on the same page no matter what numerical standard is used.
Added complexity.
Just to make things more interesting, the colour system in GDI+ adds another level of complexity, Alpha. Alpha is the level of transparency of a colour. This should also be thought of as a percentage with 0% alpha being opaque and 100% alpha being totally transparent. Whenever you create a colour you have the opportunity to use an alpha setting which will make brushes and pens semi-transparent if you wish.
The Color structure.
The Color structure contains four primary properties and five other properties that may be used to manipulate or get information about the colour:
-
A. The alpha component that specifies the transparency of the colour.
-
R. The red component.
-
G. The green component
-
B. The blue component.
-
Name. One of the known colour names such as "Red" or "Tan"
-
IsEmpty. True when a Color structure has been created but not initialized
-
IsKnownColor. True when the colour has been taken from one of the list of named colours provided by the system.
-
IsNamedColor. True when the colour has been taken from one of the list of named colours provided by the system.
-
IsSystemColor. True when the colour has been created from one of the special system colours such as ActiveBorder or HotTrack.
The Color structure also contains a complete set of shared or static properties that define a plethora of colours which you can use easily in your programs without having to look for the colour definition. The table show in Figure 1 has these colours and their names.

Figure 1. The named colours.
In addition to this set of named colours, the system defines colours that depend upon your user preferences such as the colour of the title-bar or the window background. For my system the colour table for the system is shown in Figure 2. This may be different on your machine.

Figure 2. System Colours.
Creating a colour.
In addition to the values provided for you by the system, you can create your own colours using the FromArgb method of the Color structure. Using one of the overloads of this method you can create any combination of colour and transparency by specifying the exact values desired. If you don't want transparency at-all you can use Color.FromArgb(<r>,<g>,<b>) where the colour components are in the range 0-255. If you want transparency you can use Color.FromArgb(<a>,<some-colour>). This overload enables you to create a semi-transparent pink with the code Color.FromArgb(128,Color.Pink). Alternatively, you can specify the whole ARGB colour set using Color.FromArgb(<a><r>,<g>,<b>).
Using Colour.
Probably the most common use for colours is as a parameter for the creation of brushes and pens. You can fill an area with named colours by selecting one of the known values such as Color.Red or Color.DarkGreen. Alternatively you can use made-up ones using one of the Fill methods and a brush object. Figure 3 shows an application that creates colours at random and uses them to fill random shapes.

Figure 3. Shapes filled with random colours and transparencies.
The code that created this effect can be seen in Listing 1.
protected override void OnPaint(PaintEventArgs e)
{
Random r=new Random(1); //it looks random but the pattern will always be the same
for(int c=0; c<100; c++)
{
SolidBrush sb=new SolidBrush(Color.FromArgb(r.Next(8)*32,
r.Next(255),r.Next(255),r.Next(255)));
if(r.Next(2)==1)
{
e.Graphics.FillRectangle(sb,
r.Next(this.ClientRectangle.Width),
r.Next(this.ClientRectangle.Height),
1+r.Next(200), 1+r.Next(200));
}
else
{
e.Graphics.FillEllipse(sb,
r.Next(this.ClientRectangle.Width),
r.Next(this.ClientRectangle.Height),
1+r.Next(200), 1+r.Next(200));
}
sb.Dispose();
}
base.OnPaint (e);
}
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim r As New Random(1) 'it looks random but the pattern will always be the same
Dim c As Integer
For c = 0 To 99
Dim sb As New SolidBrush(Color.FromArgb(r.Next(8) * 32, _
r.Next(255), r.Next(255), r.Next(255)))
If r.Next(2) = 1 Then
e.Graphics.FillRectangle(sb, _
r.Next(Me.ClientRectangle.Width), _
r.Next(Me.ClientRectangle.Height), _
1 + r.Next(200), 1 + r.Next(200))
Else
e.Graphics.FillEllipse(sb, _
r.Next(Me.ClientRectangle.Width), _
r.Next(Me.ClientRectangle.Height), _
1 + r.Next(200), 1 + r.Next(200))
End If
sb.Dispose()
Next c
MyBase.OnPaint(e)
End Sub
Return to the Beginners Guide index. |