Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to go to the next color?

How to go to the next color?

Scheduled Pinned Locked Moved C#
questionhelptutorial
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bouli
    wrote on last edited by
    #1

    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 to Red, that is Brown, 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.

    L L D J F 5 Replies Last reply
    0
    • B bouli

      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 to Red, that is Brown, 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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • L Lost User

        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

        B Offline
        B Offline
        bouli
        wrote on last edited by
        #3

        When you look at the color picker, you have 3 tabs, one contains the names of the colors ("Web" tab). When the user selects Red, I'd like to programatically go to the next color, that is Brown, FireBrick etc. Fred.

        There is no spoon.

        1 Reply Last reply
        0
        • B bouli

          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 to Red, that is Brown, 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.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          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 and Enum.GetNames.

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          1 Reply Last reply
          0
          • B bouli

            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 to Red, that is Brown, 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.

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            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)

            E 1 Reply Last reply
            0
            • D DaveyM69

              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)

              E Offline
              E Offline
              ElSpinos
              wrote on last edited by
              #6

              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

              B 1 Reply Last reply
              0
              • B bouli

                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 to Red, that is Brown, 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.

                J Offline
                J Offline
                Jason Henderson
                wrote on last edited by
                #7

                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

                B 1 Reply Last reply
                0
                • E ElSpinos

                  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

                  B Offline
                  B Offline
                  bouli
                  wrote on last edited by
                  #8

                  Ok, I have it. Thanks guys :) Fred.

                  There is no spoon.

                  1 Reply Last reply
                  0
                  • B bouli

                    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 to Red, that is Brown, 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.

                    F Offline
                    F Offline
                    Frank Horn
                    wrote on last edited by
                    #9

                    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.

                    B 1 Reply Last reply
                    0
                    • J Jason Henderson

                      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

                      B Offline
                      B Offline
                      bouli
                      wrote on last edited by
                      #10

                      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 its Color 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.

                      J 1 Reply Last reply
                      0
                      • F Frank Horn

                        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.

                        B Offline
                        B Offline
                        bouli
                        wrote on last edited by
                        #11

                        Yes, really good question. My algorithm described above, is working but the colors are not sorted the right way.

                        There is no spoon.

                        1 Reply Last reply
                        0
                        • B bouli

                          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 its Color 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.

                          J Offline
                          J Offline
                          Jason Henderson
                          wrote on last edited by
                          #12

                          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

                          B 1 Reply Last reply
                          0
                          • J 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

                            B Offline
                            B Offline
                            bouli
                            wrote on last edited by
                            #13

                            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.

                            D 1 Reply Last reply
                            0
                            • B bouli

                              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.

                              D Offline
                              D Offline
                              DaveyM69
                              wrote on last edited by
                              #14

                              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)

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups