How to go to the next color?
-
Hello gurus, I have a problem to solve. I'm using a set of standard named colors. When the user selects a given color lets say
Red
, I'd like to know how I can select programmatically the next colors toRed
, that isBrown
,FireBrick
, etc. The named colors are from the panel "Web" of the color selector. I hope my question is clear enough. Best regards. Fred.There is no spoon.
-
Hello gurus, I have a problem to solve. I'm using a set of standard named colors. When the user selects a given color lets say
Red
, I'd like to know how I can select programmatically the next colors toRed
, that isBrown
,FireBrick
, etc. The named colors are from the panel "Web" of the color selector. I hope my question is clear enough. Best regards. Fred.There is no spoon.
-
What do you mean be "next color to red"? What kind of structure do you use, an enumeration, a .NET defined enumeration? Your question is a bit too vague. regards
-
Hello gurus, I have a problem to solve. I'm using a set of standard named colors. When the user selects a given color lets say
Red
, I'd like to know how I can select programmatically the next colors toRed
, that isBrown
,FireBrick
, etc. The named colors are from the panel "Web" of the color selector. I hope my question is clear enough. Best regards. Fred.There is no spoon.
Enumerations are just numbers, so you can try cast to number (int/long whatever), increment it, then cast back. You should probably add some checking to see if the next number is indeed a valid enumeration name. There are other approaches too. Look at
Enum.GetValues
andEnum.GetNames
.xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008) -
Hello gurus, I have a problem to solve. I'm using a set of standard named colors. When the user selects a given color lets say
Red
, I'd like to know how I can select programmatically the next colors toRed
, that isBrown
,FireBrick
, etc. The named colors are from the panel "Web" of the color selector. I hope my question is clear enough. Best regards. Fred.There is no spoon.
I've had a look and it appears there is no emumeration available for the colours or tabs. The colours themselves (the names) are each public static read only properties that return a Color rather than an enum. <EDIT> System.Drawing.KnownColor is an enum of course! </EDIT> Using reflection I can loop through the properties and get the ones that are of type Color and exclude any that are SystemColor (WindowText etc...) but the list is alphabetical and not in the order you require (same as Web tab). I think you'll have to hardcode your own list or enum and refer to the corresponding .Net colour from there.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
I've had a look and it appears there is no emumeration available for the colours or tabs. The colours themselves (the names) are each public static read only properties that return a Color rather than an enum. <EDIT> System.Drawing.KnownColor is an enum of course! </EDIT> Using reflection I can loop through the properties and get the ones that are of type Color and exclude any that are SystemColor (WindowText etc...) but the list is alphabetical and not in the order you require (same as Web tab). I think you'll have to hardcode your own list or enum and refer to the corresponding .Net colour from there.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Extending DaveyM69's advice, here's how you could go about getting the values you need:
// Get all the property elements of the Color struct
PropertyInfo[] PropertyInfo = typeof(Color).GetProperties();// Loop through the property info elements...
foreach (PropertyInfo propertyElement in PropertyInfo)
{
// We're interested in color types...
if (propertyElement.PropertyType == typeof(Color))
{
// propertyElement.Name will indicate the name of the colour, stick this into an array if needed...
string colourName = propertyElement.Name;
// Define a new Color type using the static FromName method to get the base colour value.
Color c = Color.FromName(colourName );
}
}Fernando Mendes Senior .NET Developer, Architect
-
Hello gurus, I have a problem to solve. I'm using a set of standard named colors. When the user selects a given color lets say
Red
, I'd like to know how I can select programmatically the next colors toRed
, that isBrown
,FireBrick
, etc. The named colors are from the panel "Web" of the color selector. I hope my question is clear enough. Best regards. Fred.There is no spoon.
You could sort all the colors in the enumeration by RGB value, then find Red's location then grab the next color from your sorted list.
"Make everything as simple as possible, but not simpler." - Albert Einstein
Jason Henderson
-
Extending DaveyM69's advice, here's how you could go about getting the values you need:
// Get all the property elements of the Color struct
PropertyInfo[] PropertyInfo = typeof(Color).GetProperties();// Loop through the property info elements...
foreach (PropertyInfo propertyElement in PropertyInfo)
{
// We're interested in color types...
if (propertyElement.PropertyType == typeof(Color))
{
// propertyElement.Name will indicate the name of the colour, stick this into an array if needed...
string colourName = propertyElement.Name;
// Define a new Color type using the static FromName method to get the base colour value.
Color c = Color.FromName(colourName );
}
}Fernando Mendes Senior .NET Developer, Architect
-
Hello gurus, I have a problem to solve. I'm using a set of standard named colors. When the user selects a given color lets say
Red
, I'd like to know how I can select programmatically the next colors toRed
, that isBrown
,FireBrick
, etc. The named colors are from the panel "Web" of the color selector. I hope my question is clear enough. Best regards. Fred.There is no spoon.
This raises the question how they (i.e. Microsoft) actually sort the colours in the web tab of their colour picker. Do they just sort by RGB value? It doesn't look like it: black and white would not both be at the beginning then. Maybe it's something hard coded indeed, so if you want to exactly reproduce it you just have to hard code a list like theirs. Or is it actually that you want the next "similar" colour? This would raise the question how to exactly define "similarity". Something like (R1-R2) + (G1-G2) + (B1-B2)? Or maybe a sqare sum? I really don't know what the graphics experts would do there, or what the human eye regards as similar.
-
You could sort all the colors in the enumeration by RGB value, then find Red's location then grab the next color from your sorted list.
"Make everything as simple as possible, but not simpler." - Albert Einstein
Jason Henderson
And how can you do that? I have used the following simple algorithm: - I get
KnownColor
enum values names as a string array. - I search for the text and get the index in the value names as a start point - in the loop that draws my curves (using ZedGraph) I go to then next index of the string array by incrementing it if the index goes too far, I switch to 0 to go to the begining of the array, I get the name of the color, convert it to itsColor
value Of course,KnownColor
is not ordered the same way as the "web" tab in the color picker of Visual Studio, thus, I'd like to know how it is sorted. But the colors of the curves are changing.There is no spoon.
-
This raises the question how they (i.e. Microsoft) actually sort the colours in the web tab of their colour picker. Do they just sort by RGB value? It doesn't look like it: black and white would not both be at the beginning then. Maybe it's something hard coded indeed, so if you want to exactly reproduce it you just have to hard code a list like theirs. Or is it actually that you want the next "similar" colour? This would raise the question how to exactly define "similarity". Something like (R1-R2) + (G1-G2) + (B1-B2)? Or maybe a sqare sum? I really don't know what the graphics experts would do there, or what the human eye regards as similar.
-
And how can you do that? I have used the following simple algorithm: - I get
KnownColor
enum values names as a string array. - I search for the text and get the index in the value names as a start point - in the loop that draws my curves (using ZedGraph) I go to then next index of the string array by incrementing it if the index goes too far, I switch to 0 to go to the begining of the array, I get the name of the color, convert it to itsColor
value Of course,KnownColor
is not ordered the same way as the "web" tab in the color picker of Visual Studio, thus, I'd like to know how it is sorted. But the colors of the curves are changing.There is no spoon.
Create a List of KnownColors, then sort the list by RGB value. This may not even be close, but it does move the redish to the front and the bluish to the end:
public partial class Form1 : Form { private List colorList = new List(); public Form1() { InitializeComponent(); } private void pictureBox1\_Paint(object sender, PaintEventArgs e) { e.Graphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(pictureBox1.Location, pictureBox1.Size)); int x, y; x = 0; y = 0; foreach (Color c in colorList) { e.Graphics.FillRectangle(new SolidBrush(c), new Rectangle(x, y, 25, 25)); x += 25; if ((x + 25) > pictureBox1.Width) { x = 0; y += 25; } } } private void Form1\_Load(object sender, EventArgs e) { foreach (KnownColor c in Enum.GetValues(typeof(KnownColor))) { colorList.Add(Color.FromKnownColor(c)); } colorList.Sort( delegate(Color x, Color y) { return ColorTranslator.ToWin32(x).CompareTo(ColorTranslator.ToWin32(y)); }); } }
"Make everything as simple as possible, but not simpler." - Albert Einstein
Jason Henderson
-
Create a List of KnownColors, then sort the list by RGB value. This may not even be close, but it does move the redish to the front and the bluish to the end:
public partial class Form1 : Form { private List colorList = new List(); public Form1() { InitializeComponent(); } private void pictureBox1\_Paint(object sender, PaintEventArgs e) { e.Graphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(pictureBox1.Location, pictureBox1.Size)); int x, y; x = 0; y = 0; foreach (Color c in colorList) { e.Graphics.FillRectangle(new SolidBrush(c), new Rectangle(x, y, 25, 25)); x += 25; if ((x + 25) > pictureBox1.Width) { x = 0; y += 25; } } } private void Form1\_Load(object sender, EventArgs e) { foreach (KnownColor c in Enum.GetValues(typeof(KnownColor))) { colorList.Add(Color.FromKnownColor(c)); } colorList.Sort( delegate(Color x, Color y) { return ColorTranslator.ToWin32(x).CompareTo(ColorTranslator.ToWin32(y)); }); } }
"Make everything as simple as possible, but not simpler." - Albert Einstein
Jason Henderson
-
Ok, it works fine :) It's not the same order as the web tab, but it's better than doing no sort. :) Thanks.
There is no spoon.
If you want the same sort as the Web tab why not create your own dictionary and enum. The colors will be stored in the dictionary in whatever order you want and because an enum member is really an int you can use that to get the actual Color from the dictionary.
public enum WebColors // To be completed! { Transparent, Black, White, DimGray, Gray, DarkGray, Silver, LightGray, Gainsboro, WhiteSmoke, Maroon, DarkRed, Red } public class WebColorDictionary : Dictionary<int, Color> { public WebColorDictionary() { Initialize(); } private void Initialize() // To be completed! { this.Add(0, Color.Transparent); this.Add(1, Color.Black); this.Add(2, Color.White); this.Add(3, Color.DimGray); this.Add(4, Color.Gray); this.Add(5, Color.DarkGray); this.Add(6, Color.Silver); this.Add(7, Color.LightGray); this.Add(8, Color.Gainsboro); this.Add(9, Color.WhiteSmoke); this.Add(10, Color.Maroon); this.Add(11, Color.DarkRed); this.Add(12, Color.Red); } }
You can now do something like this:
foreach (string s in Enum.GetNames(typeof(WebColors)))
Console.WriteLine(s);and:
WebColorDictionary myColors = new WebColorDictionary();
BackColor = myColors[(int)WebColors.Silver];Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)