none
GradientStops.Insert How to convert a system color to integer C#

    Question

  • Hi

    I'm trying to figure out how to convert a system color to integer.

    I need to fill a serie in a chart by some color and I'm using gradient

    so my code looks like

     serieFill.GradientStops.Insert(RGB: System.Drawing.Color.DarkOrange.ToArgb(), Position: 0);
    serieFill.GradientStops.Insert(RGB: System.Drawing.Color.Orange.ToArgb(), Position: 0.5F);
     serieFill.GradientStops.Insert(RGB: System.Drawing.Color.DarkOrange.ToArgb(), Position: 1);
    

    as you can see I use Orange color. Unfortunately the Insert method accepts only integer value of a color so I need to convert the color to integer. I thought that the 'ToArgb()' method would be the best but it doesn't work for me.

    I should get a chart (I mean a serie)  filled by orange color but the result is BLUE color

    I'm confused, what do I need to do?

    Thanks


    Visual Studio 2010 Professional, Office 2007
    Friday, November 26, 2010 12:40 PM

Answers

  • Hi

    thanks for response, unfortunately it doesn't work

    But my colleague found a solution, look at the code

    int rgbDark = ColorTranslator.ToOle(Color.DarkOrange);        
    int rgbLight = ColorTranslator.ToOle(Color.Yellow);
    

    and then I can use it this way

    serieFill.GradientStops.Insert(RGB: rgbDark, Position: 0);
    serieFill.GradientStops.Insert(RGB: rgbLight, Position: 0.5F);
    serieFill.GradientStops.Insert(RGB: rgbDark, Position: 1);
    
    So the method 'ToOle' solves my problem the way as I expected


    Visual Studio 2010 Professional, Office 2007
    • Marked as answer by PetLahev Monday, November 29, 2010 11:42 AM
    Monday, November 29, 2010 11:42 AM

All replies

  • Hi

    Seems like it can't handle the alpha channel (ARGB, Alpha Red Green Blue).

    Mask the ARGB value to get the RGB value:

                int argb = Color.Orange.ToArgb();

                int rgb = argb & 0x00FFFFFF;

    Hope this helps.

    Regards,

    Mauro


    castagnasso.wordpress.com | If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, November 26, 2010 6:14 PM
  • Hi

    thanks for response, unfortunately it doesn't work

    But my colleague found a solution, look at the code

    int rgbDark = ColorTranslator.ToOle(Color.DarkOrange);        
    int rgbLight = ColorTranslator.ToOle(Color.Yellow);
    

    and then I can use it this way

    serieFill.GradientStops.Insert(RGB: rgbDark, Position: 0);
    serieFill.GradientStops.Insert(RGB: rgbLight, Position: 0.5F);
    serieFill.GradientStops.Insert(RGB: rgbDark, Position: 1);
    
    So the method 'ToOle' solves my problem the way as I expected


    Visual Studio 2010 Professional, Office 2007
    • Marked as answer by PetLahev Monday, November 29, 2010 11:42 AM
    Monday, November 29, 2010 11:42 AM
  • Hi,

    Thanks for posting your findings.

    Regards,

    Mauro


    castagnasso.wordpress.com | If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, November 29, 2010 4:02 PM