0 Replies - 3563 Views - Last Post: 29 June 2016 - 06:05 PM

#1 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1536
  • View blog
  • Posts: 3,864
  • Joined: 01-February 13

Getting a List of KnownColors or SystemColors

Posted 29 June 2016 - 06:05 PM

If you are reading this you may already have found that there really is not a simple one liner code to get a list or array of KnownColors or SystemColors. This is a question i have seen a few variations of over the last few years so, i decided to throw together this snippet just to help others looking for a way to get different combinations of KnownColors or SystemColors.

You can get a List of All KnownColors, KnownColors without SystemColors, or SystemColors only. You can also choose to include or exclude Color.Transparent.

Color.Transparent is only in the KnownColors so, you really can not actually exclude it if you are only getting a List of SystemColors.

You can test the example below in a new Form Project with 1 Button and 1 RichTextBox added to the Form. If you have any questions, please feel free to ask.
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'this will get a list of all KnownColors excluding any SystemColors and does not include Color.Transparent
        Dim Clrs As List(Of Color) = ColorHelper.GetColors(ColorHelper.ColorTypes.NoSystemColors, False)

        Dim sb As New System.Text.StringBuilder
        For Each c As Color In Clrs
        RichTextBox1.Text = sb.ToString
    End Sub
End Class

Public Class ColorHelper
    Public Enum ColorTypes As Integer
        AllKnownColors = 0
        NoSystemColors = 1
        SystemColorsOnly = 2
    End Enum

    ''' <summary>Gets a list of the specified colors.</summary>
    ''' <param name="ClrTypes">Specifies the colors to retrieve a list of.</param>
    ''' <param name="IncludeTransparent">True to include Color.Transparent or False to exclude it. For the SystemColors this parameter is ignored.</param>
    Public Shared Function GetColors(ByVal ClrTypes As ColorTypes, ByVal IncludeTransparent As Boolean) As List(Of Color)
        Dim clrlst As New List(Of Color)
        Dim clr As Color = Color.Empty
        For Each c As KnownColor In [Enum].GetValues(GetType(KnownColor))
            clr = Color.FromKnownColor(c)
            If ClrTypes = ColorTypes.AllKnownColors Then
            ElseIf ClrTypes = ColorTypes.NoSystemColors Then
                If Not clr.IsSystemColor Then clrlst.Add(clr)
            ElseIf clr.IsSystemColor Then
            End If
        If Not IncludeTransparent Then clrlst.Remove(Color.Transparent)
        Return clrlst
    End Function
End Class

Is This A Good Question/Topic? 1
  • +

Page 1 of 1