We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,055 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to loop through colors?

i want to loop through each avaiable color in Color struct.
Is that possible?

5
Contributors
10
Replies
3 Years
Discussion Span
3 Months Ago
Last Updated
13
Views
Question
Answered
serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
Skill Endorsements: 0

Depends on what you want Serkan.
This http://www.daniweb.com/code/snippet1022.html loops through what is called the KnowColors.
All the colors would need 256*256*256 iterations I guess.

ddanbe
Industrious Poster
4,293 posts since Oct 2008
Reputation Points: 2,121
Solved Threads: 723
Skill Endorsements: 26

i dont have knowncolor, i am using on compact framework. i have System.Drawing.Color struct. and i has properties. i want to read those property values maybe using reflection.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
Skill Endorsements: 0

You also could create a file and read it in.
One of the lines would be the values of this:

Color c = Color.CornflowerBlue;
            int Alpha = c.A;
            int Red = c.R;
            int Green = c.G;
            int Blue = c.B;
ddanbe
Industrious Poster
4,293 posts since Oct 2008
Reputation Points: 2,121
Solved Threads: 723
Skill Endorsements: 26

no, what i want is something simple, we have colors in System.Drawing.Color. all i want is to create a color picker.
I get angry when something gets hard for no reason as now.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
Skill Endorsements: 0

what i want is something like this :

foreach(color c in system.drawing.color)
{
listviewitem lvi = new listviewitem(c.name);
lvi.forecolor = c;
}
it shouldnt be hard, should it?

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
Skill Endorsements: 0

i found this ####ing thing :

foreach (PropertyInfo p in typeof(Color).GetProperties())
			{
				Color c = new Color();
				if (p.PropertyType == typeof(Color))
				{
					ListViewItem lvi = new ListViewItem(p.Name);
					lvi.ForeColor =(Color) p.GetValue(c,null);
					listView1.Items.Add(lvi);
				}
			}

Project is attached

Attachments ColorPicker.zip (26.7KB)
serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
Skill Endorsements: 0
Question Answered as of 3 Years Ago by ddanbe

How about a color wheel. The angle on the wheel is used to calculate the RGB required to display that color!

You select a ilumination level, 0.0=black 1.0=white, and then rotate through a 360 degree wheel at that luminance level!

wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
Skill Endorsements: 0

i found this ####ing thing :

foreach (PropertyInfo p in typeof(Color).GetProperties())
			{
				Color c = new Color();
				if (p.PropertyType == typeof(Color))
				{
					ListViewItem lvi = new ListViewItem(p.Name);
					lvi.ForeColor =(Color) p.GetValue(c,null);
					listView1.Items.Add(lvi);
				}
			}

Project is attached

Sir can u explain the ur code that how its work especially what is propertyinfo and what is its function.

Thanks in advance.

qauaan
Newbie Poster
12 posts since May 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

this is called reflection, you can search google with "c# reflection"
for further information.

serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
Skill Endorsements: 0

Serkan, great example. I just noticed what is clearly a typo on your code. You want to iterate through the properties of the Colors (not Color) class. Maybe an edit would help people grasp this better. Thanks for the code. Helped.

Mauricio Feijo
Newbie Poster
1 post since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0882 seconds using 2.7MB