|
-
Oct 27th, 2002, 01:59 PM
#1
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!!
-
Oct 27th, 2002, 02:53 PM
#2
Sleep mode
Code:
Imports System.Drawing.Color
'---------------------
Color.YellowGreen()
-
Oct 27th, 2002, 03:06 PM
#3
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!!
-
Oct 27th, 2002, 03:28 PM
#4
-
Oct 27th, 2002, 03:46 PM
#5
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!!
-
Oct 27th, 2002, 03:58 PM
#6
PowerPoster
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
-
Oct 27th, 2002, 04:59 PM
#7
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:
Public Function GetColorList() As ArrayList
Dim nms() As String = Drawing.KnownColor.GetNames(GetType(Drawing.KnownColor))
Dim al As New ArrayList()
Dim o As String
For Each o In nms
Dim clr As Drawing.Color = Drawing.Color.FromName(o)
If Not clr.IsSystemColor Then al.Add(o)
Next
Return al
End Function
Although I don't know if this or the reflection way is faster.
-
Oct 27th, 2002, 05:17 PM
#8
PowerPoster
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));
}
}
-
Oct 27th, 2002, 05:42 PM
#9
Hyperactive Member
ya wanna convert that to vb ?
-
Oct 27th, 2002, 05:57 PM
#10
PowerPoster
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
-
Oct 27th, 2002, 06:56 PM
#11
Hyperactive Member
Thanks!
anyone to Get the Color to just show the Color name and not
"color[Blue]"
?
-
Oct 27th, 2002, 07:05 PM
#12
PowerPoster
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
-
Oct 27th, 2002, 07:12 PM
#13
Hyperactive Member
-
Oct 28th, 2002, 02:10 AM
#14
Hyperactive Member
anyone know why when i used a listView it adds like 3 per row and not just one ?
-
Oct 28th, 2002, 09:33 AM
#15
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()
-
Oct 28th, 2002, 02:24 PM
#16
PowerPoster
Forgot to add that checks to my code. Thanks
-
Oct 28th, 2002, 11:51 PM
#17
-
Oct 30th, 2002, 10:09 AM
#18
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!!
-
Oct 31st, 2002, 03:31 PM
#19
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!!
-
Oct 31st, 2002, 03:45 PM
#20
PowerPoster
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.
-
Oct 31st, 2002, 04:57 PM
#21
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!!
-
Oct 31st, 2002, 05:44 PM
#22
Maybe try sorting by RGB value???
-
Jul 11th, 2003, 07:40 AM
#23
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:
Public Class ColorSorter
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim c1 As Color = CType(x, Color)
Dim c2 As Color = CType(y, Color)
Dim resR As Integer
Dim resG As Integer
Dim resB As Integer
If c1.B < c2.B Then resB = -1
If c1.B = c2.B Then resB = 0
If c1.B > c2.B Then resB = 1
If resB <> 0 Then
Return resB
Else
If c1.G < c2.G Then resG = -1
If c1.G = c2.G Then resG = 0
If c1.G > c2.G Then resG = 1
If resG <> 0 Then
Return resG
Else
If c1.R < c2.R Then resR = -1
If c1.R = c2.R Then resR = 0
If c1.R > c2.R Then resR = 1
Return resR
End If
End If
End Function
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|