Results 1 to 23 of 23

Thread: how can I get a list of all colors?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    how can I get a list of all colors?

    aah, I'm getting dumb
    I want to get a list of all the colors in Drawing.Color and perhaps add them to a listbox. How can I do this?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Code:
    Imports System.Drawing.Color
    
    
    '---------------------
    Color.YellowGreen()

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by pirate
    Code:
    Imports System.Drawing.Color
    
    
    '---------------------
    Color.YellowGreen()
    eeeh??????!!!!
    I said I want to list them in a list box. Typing all of them is for sure a dumb thing to do there is a way to get a list of all of them by code, I just dont remember it
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    well yeah I tried that, but GetNames and GetValues arent members of Color. Color is a type
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Code:
        //**************************************
        //     
        // Name: Use Reflection to list all Colo
        //     r objects
        // Description:This is a good example in
        //     the power and flexibility of using Refle
        //     ction. This example will display in a co
        //     nsole a complete list of all the Colors 
        //     available in the System.Drawing.Color na
        //     mespace. Put into a file colors.vb and c
        //     ompile at the command line using 'vbc co
        //     lors.vb' then run the colors.exe
        // By: Chris Andersen
        //
        //This code is copyrighted and has    // limited warranties.Please see http://
        //     www.Planet-Source-Code.com/xq/ASP/txtCod
        //     eId.482/lngWId.10/qx/vb/scripts/ShowCode
        //     .htm    //for details.    //**************************************
        //     
        
        Imports System
        Imports System.Reflection
        Public Module ListColors
        	Public Sub Main(ByVal args() As String)
        		GetColors()	
        	End Sub
        	
        	Public Sub GetColors()
        		Dim asm As [Assembly] ' Assembly is reserved vb word so use the brackets to override
        		Dim myClasses() As Type
        		Dim myProperties() As PropertyInfo
        		
        		Dim myTypeInfo As Type
        		Dim myPropertyInfo As PropertyInfo
        		
        		' Lets load the System.Drawing dll
        		asm = [Assembly].LoadWithPartialName("System.Drawing")
        		If Not(asm Is Nothing) Then
        			' get an array of base classes
        			myClasses = asm.GetTypes()
        		End If
        		
        		' Loop through each class
        		For Each myTypeInfo In myClasses
        			' if it is the color class
        			If myTypeInfo.ToString() = "System.Drawing.Color" Then
        				' get an array of all the properties
        				myProperties = myTypeInfo.GetProperties()
        				' loop the properties
        				For Each myPropertyInfo In myProperties
        					' if the property returns a Color object, then lets output it
        					' to the console as their are some non color properties we
        					' dont want to list
        					If myPropertyInfo.PropertyType.ToString() = "System.Drawing.Color" Then
        						Console.Writeline(myPropertyInfo.Name)
        					End If
        				Next myPropertyInfo
        			End If
        		Next myTypeInfo
        	End Sub
        End Module

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Lethal's code should work, but also the colors are in the Drawing.KnownColors enum. Although if you want just the named ones then you'd have to test individually for IsSystemColor.

    VB Code:
    1. Public Function GetColorList() As ArrayList
    2.         Dim nms() As String = Drawing.KnownColor.GetNames(GetType(Drawing.KnownColor))
    3.         Dim al As New ArrayList()
    4.         Dim o As String
    5.         For Each o In nms
    6.             Dim clr As Drawing.Color = Drawing.Color.FromName(o)
    7.             If Not clr.IsSystemColor Then al.Add(o)
    8.         Next
    9.         Return al
    10. End Function

    Although I don't know if this or the reflection way is faster.

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Here's a short method I figured out using reflection:

    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
    System.Reflection.PropertyInfo[] colors = typeof(System.Drawing.Color).GetProperties
    		(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
    	
    foreach(System.Reflection.PropertyInfo color in colors)
    {
    	this.listBox1.Items.Add ((System.Drawing.Color) color.GetValue(null,null));				
    } 
    }

  9. #9
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    ya wanna convert that to vb ?

  10. #10
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Since you asked nicely..

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim properties() As System.Reflection.PropertyInfo = GetType(System.Drawing.Color).GetProperties _
                (Reflection.BindingFlags.Public Or Reflection.BindingFlags.Static)
            Dim color As System.Reflection.PropertyInfo
    
            For Each color In properties
                ListBox1.Items.Add(color.GetValue(Nothing, Nothing))
            Next
        End Sub

  11. #11
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    Thanks!

    anyone to Get the Color to just show the Color name and not
    "color[Blue]"
    ?

  12. #12
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Sure:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim properties() As System.Reflection.PropertyInfo = GetType(System.Drawing.Color).GetProperties _
                (Reflection.BindingFlags.Public Or Reflection.BindingFlags.Static)
            Dim color As System.Reflection.PropertyInfo
    
            For Each color In properties
                ListBox1.Items.Add(color.Name())
            Next
        End Sub

  13. #13
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    thank you

  14. #14
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    USA
    Posts
    432
    anyone know why when i used a listView it adds like 3 per row and not just one ?

  15. #15
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    One problem with the second code Lethal posted. some of the things that will show up in the listbox wont be colors. That is why in my code, the first code Lethal posted from psc, I have

    If myPropertyInfo.PropertyType.ToString() = "System.Drawing.Color"


    that makes sure the return type is an actual color type. So that is needed also.

    in Lethal version, that translates to

    colors.PropertyType.ToString()
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  16. #16
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Forgot to add that checks to my code. Thanks

  17. #17

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    hey...thanks all. I think I choose Edneeis way, it's shorter

    umm, is there away to get the colors in order of their color? like when you want to change the color of a control, VS.NET's color dialog shows the colors in order (in the WEB section). Is it possible to do something like this?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  18. #18

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    bump
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  19. #19

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    and yet again, bump
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  20. #20
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    I'm not at my home computer right now, but what kind of order are they in? If alphabetical, you could just dump the array into an array list and sort them that way.

  21. #21

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    no not alphabetical. For example LightBlue and Blue would have to be next to each other, because they're both blue. I guess it's hard to sort it that way
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  22. #22
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Maybe try sorting by RGB value???

  23. #23
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840

    Wink How to sort colors

    Hi Mr. Polite.

    I noticed, that the default palette for 256 colors, in VB .NET's own imageeditor they are sorted like this :

    #000000
    #800000
    #808000
    #000080

    From this I can see, that first B as compared. If that is
    equal, then G is compared, and if that is equal then R is
    compared.

    If you add this class to you project, and create an array
    of colors, you can use this class to sort colors.
    The code may not be the pretties, but it works.

    VB Code:
    1. Public Class ColorSorter
    2.     Implements IComparer
    3.  
    4.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
    5.         Dim c1 As Color = CType(x, Color)
    6.         Dim c2 As Color = CType(y, Color)
    7.  
    8.         Dim resR As Integer
    9.         Dim resG As Integer
    10.         Dim resB As Integer
    11.  
    12.         If c1.B < c2.B Then resB = -1
    13.         If c1.B = c2.B Then resB = 0
    14.         If c1.B > c2.B Then resB = 1
    15.  
    16.         If resB <> 0 Then
    17.             Return resB
    18.         Else
    19.             If c1.G < c2.G Then resG = -1
    20.             If c1.G = c2.G Then resG = 0
    21.             If c1.G > c2.G Then resG = 1
    22.             If resG <> 0 Then
    23.                 Return resG
    24.             Else
    25.                 If c1.R < c2.R Then resR = -1
    26.                 If c1.R = c2.R Then resR = 0
    27.                 If c1.R > c2.R Then resR = 1
    28.                 Return resR
    29.             End If
    30.         End If
    31.  
    32.     End Function
    33. End Class

    Hopes this helps.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width