Click here to Skip to main content
15,893,722 members
Articles / Multimedia / GDI+

ColorBlender - Dynamic Gradient Color Blend Creation Control (VB.NET)

Rate me:
Please Sign up or sign in to vote.
4.90/5 (29 votes)
1 Aug 2012CPOL6 min read 89.5K   4.4K   61  
How to create a ColorBlend and two color blending UserControls to make it easier.
#Region "Imports"

Imports System.ComponentModel
Imports System.Drawing.Drawing2D
#End Region

#Region "cBlenderItems"

<System.Diagnostics.DebuggerStepThrough()> _
<TypeConverter(GetType(BlendItemsConverter))> _
Public Class cBlenderItems

#Region "New"

    Sub New()
        cbColor = New Color() {Color.AliceBlue, Color.RoyalBlue, Color.Navy}
        cbPosition = New Single() {0, 0.5, 1}
        BorderColor = Drawing.Color.Black
        FocalPoints = New cFocalPoints
        BlendGradientType = eBlendGradientType.Linear
        BlendGradientMode = LinearGradientMode.Vertical
        BlendPathShape = eBlendPathShape.Rectangle
    End Sub

    Sub New(ByVal Color As Color(), ByVal Pos As Single())
        cbColor = Color
        cbPosition = Pos
        BorderColor = Drawing.Color.Black
        FocalPoints = New cFocalPoints
        BlendGradientType = eBlendGradientType.Linear
        BlendGradientMode = LinearGradientMode.Vertical
        BlendPathShape = eBlendPathShape.Rectangle
    End Sub

    Sub New(ByVal Color As Color(), ByVal Pos As Single(), ByVal Border As Color)
        cbColor = Color
        cbPosition = Pos
        BorderColor = Border
        FocalPoints = New cFocalPoints
        BlendGradientType = eBlendGradientType.Linear
        BlendGradientMode = LinearGradientMode.Vertical
        BlendPathShape = eBlendPathShape.Rectangle
    End Sub

    Sub New(ByVal Color As Color(), ByVal Pos As Single(), ByVal Border As Color,
            ByVal FocalPts As cFocalPoints, GradientType As eBlendGradientType,
            GradientMode As LinearGradientMode, PathShape As eBlendPathShape)

        cbColor = Color
        cbPosition = Pos
        BorderColor = Border
        FocalPoints = FocalPts
        BlendGradientType = GradientType
        BlendGradientMode = GradientMode
        BlendPathShape = PathShape
    End Sub
#End Region

#Region "Properties"

    ''' <summary>cb
    ''' The Color for the relative Position
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property cbColor() As Color()

    ''' <summary>
    ''' The Position for the Color
    ''' </summary>
    ''' <value></value>
    ''' <returns>Single()</returns>
    ''' <remarks></remarks>
    Public Property cbPosition() As Single()

    ''' <summary>
    ''' For use as a an Accent Color like a Border
    ''' </summary>
    ''' <value></value>
    ''' <returns>Color</returns>
    ''' <remarks></remarks>
    Public Property BorderColor As Color

    ''' <summary>
    ''' The CenterPoint and FocusScales for the Drawing.Drawing2D.ColorBlend
    ''' </summary>
    ''' <value></value>
    ''' <returns>cFocalPoints</returns>
    ''' <remarks></remarks>
    Public Property FocalPoints() As cFocalPoints

    Enum eBlendGradientType
        Linear
        Path
    End Enum
    ''' <summary>
    ''' Type of brush used to paint color blend
    ''' </summary>
    ''' <value></value>
    ''' <returns>eBlendGradientType</returns>
    ''' <remarks></remarks>
    Public Property BlendGradientType() As eBlendGradientType

    ''' <summary>
    ''' Type of linear gradient color blend
    ''' </summary>
    ''' <value></value>
    ''' <returns>LinearGradientMode</returns>
    ''' <remarks></remarks>
    Public Property BlendGradientMode() As LinearGradientMode = LinearGradientMode.Vertical

    Enum eBlendPathShape
        Rectangle
        Ellipse
        Triangle
        Polygon
    End Enum
    ''' <summary>
    ''' Shape of path for the color blend
    ''' </summary>
    ''' <value></value>
    ''' <returns>eBlendPathShape</returns>
    ''' <remarks></remarks>
    Public Property BlendPathShape() As eBlendPathShape = eBlendPathShape.Rectangle

#End Region

#Region "Public Methods"

    Public Function GetColorBlendForBrush() As ColorBlend
        Dim blend As ColorBlend = New ColorBlend()
        'Add the Array of Color
        blend.Colors = cbColor
        'Add the Array Single (0-1) colorpoints to place each Color
        blend.Positions = cbPosition
        Return blend
    End Function
#End Region

#Region "Public Overrides"

    Public Overrides Function ToString() As String
        ' build the string as "BorderColor|Color1;Color2;Color3|Pt1;Pt2;Pt3|CP.x, CP.y, " 
        Dim bColors As New ArrayList
        Dim bPoints As New ArrayList
        For Each bColor As Color In cbColor
            If bColor.IsNamedColor Then
                bColors.Add(bColor.Name)
            Else
                'Catch for when the color sometimes loses its NamedColor status internally
                bColor = bColor.GetColorBest(True)
                If bColor.IsNamedColor Then
                    bColors.Add(bColor.Name)
                Else
                    bColors.Add(String.Format("{0},{1},{2},{3}",
                                              bColor.A, bColor.R, bColor.G, bColor.B))
                End If
            End If
        Next
        For Each bPoint As Single In cbPosition
            bPoints.Add(bPoint.ToString)
        Next
        Return String.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}",
                             BorderColor.GetColorBestName(),
                             Join(bColors.ToArray, ";"),
                             Join(bPoints.ToArray, ";"),
                             FocalPoints.ToString,
                             BlendGradientType.ToString,
                             BlendGradientMode.ToString,
                             BlendPathShape.ToString)

    End Function

    Public Overrides Function Equals(ByVal obj As Object) As Boolean

        Dim eObj As cBlenderItems = CType(obj, cBlenderItems)
        If cbColor.Length <> eObj.cbColor.Length _
            OrElse cbPosition.Length <> eObj.cbPosition.Length Then
            Return False
        Else
            For i As Integer = 0 To cbColor.Length - 1
                If cbColor(i) <> eObj.cbColor(i) OrElse cbPosition(i) <> eObj.cbPosition(i) Then
                    Return False
                End If
            Next
            If BorderColor.Equals(eObj.BorderColor) AndAlso
                FocalPoints.Equals(eObj.FocalPoints) AndAlso
                BlendGradientType.Equals(eObj.BlendGradientType) AndAlso
                BlendGradientMode.Equals(eObj.BlendGradientMode) AndAlso
                BlendPathShape.Equals(eObj.BlendPathShape) Then

                Return True
            Else
                Return False
            End If
        End If
        'Red|AliceBlue;RoyalBlue;Navy|0;0.5;1|0.5, 0.5, 0, 0|Linear|Vertical|Rectangle
    End Function
#End Region

End Class

#End Region

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I first got hooked on programing with the TI994A. After it finally lost all support I reluctantly moved to the Apple IIe. Thank You BeagleBros for getting me through. I wrote programs for my Scuba buisness during this time. Currently I am a Database manager and software developer. I started with VBA and VB6 and now having fun with VB.NET/WPF/C#...

Comments and Discussions