The ColorConverter class is used to convert colors from one data type to another. This class is inherited from the TypeConverter class, which defines the functionality for conversion of type and accessing values and properties of types. The TypeConverter class serves as a base class for many conversion classes, and ColorConverter and FontConverter are two of them. We will discuss FontConverter in more detail later in this article. Some of the common methods of TypeConverter class (which are available in the ColorConverter class) are described in Table5.2.
TABLE 5.2 Common TypeConverter methods
|
Method |
Description |
|
CanConvertForm |
Takes a type as a parameter and returns true if the converter can convert an object to the type of the converter; otherwise returns false |
|
CanConverTo |
Takes a type as a parameter and returns true if the converter can converts an object to a given type; other wise return false. |
|
ConvertFrom |
Converts an object to the type of converter and returns the converted object |
|
ConvetTo |
Converts a specified object to the new and returns the object |
|
GetStanderdValues |
Returns a collection of standard values (collection type) for the data type for which this type converter is designed. |
|
GetStandardValuesSupported |
Identifies whether this object supports a standard set of values. |
Listing 5.4 uses the ColorConverter class methods to convert colors. We store in string and call the ConverterFromString method, which returns the Color object. Later we will use the Color objects to create two brushes that we will use to fill a rectangle and an ellipse.
LISTING 5.4 Using the ColorConverter class to convert colors
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Public Class Form1
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
Dim str As [String] = "#FF00FF"
Dim clrConverter As New ColorConverter()
Dim clr1 As Color = DirectCast(clrConverter.ConvertFromString(str), Color)
'Use colors
Dim clr2 As New SolidBrush(clr1)
Dim clr3 As New SolidBrush(clr1)
'Draw GDI+ objects
g.FillEllipse(clr2, 10, 10, 50, 50)
g.FillRectangle(clr3, 60, 10, 50, 50)
'Dispose of objects
clr2.Dispose()
clr3.Dispose()
g.Dispose()
End Sub
End Class
FIGURE 5.4: Converting colors
Figure 5.4 shows the output from Listing 5.4.
The ColorTranslator class provides methods to translate colors to and from HTML, OLE, and Win32 color values. These methods are useful when you are using legacy color structures that pre-date the .NET Framework. For example, you may have legacy code that gives the HTML color representation of a color. Table 5.3 describes the methods of the ColorTranslator class. All of the methods are static.
Listing 5.5 uses the ColorTranslator class to translate colors from Win32 and HTML colors. Later these colors will be used to create brushes.
LISTING 5.5 Translating colors
Private Sub ColorTranslator_click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim g As Graphics = Me.CreateGraphics()
'Translate colors
Dim win32color__1 As Color = ColorTranslator.Formwin32(oxFF0033)
Dim htmlColor As Color = ColorTranslator.FromHtml("#00AAFF")
'use Colors
Dim clr1 As New SolidBrush(win32Color)
Dim clr2 As New SolidBrush(htmlColor)
'Draw GDI+
g.FillEllipse(clr1, 10, 10, 50, 50)
g.FillRectangle(clr2, 60, 10, 50, 50)
'dispose of object
clr1.Dispose()
clr2.Dispose()
g.Dispose()
End Sub
TABLE 5:3 ColorTranslator methods
| Method |
Description |
|
FromHtml |
Translates from an HTML color representation to a Color structure |
|
FromOle |
Translates from an OLE color value to a Color structure |
|
FromWin32 |
Translates from an windows color to a Color structure |
|
ToHtml |
Translates from a Color structure to HTML color representation. |
|
ToOle |
Translates from a Color structure to OLE color. |
|
ToWin32 |
Translates from a Color structure to window color. |
In a manner similar to the "from" methods just discussed, you can translate a Color structure into Win32, HTML and OLE values using the Towin32, ToHtml, and ToOle method, respectively.
Note: You can also transform colors using transformation methods. Some of transformation methods are for scaling, translating, rotating, and shearing.
Conclusion
Hope the article would have helped you in understanding ColorConverter and ColorTranslater Classes in GDI+. Read other articles on GDI+ on the website.