Color code converter issue vb/wpf
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Color code converter issue vb/wpf

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    10

    Question Color code converter issue vb/wpf

    Hi,

    There is a vb control which takes a long value of 235 as its backcolor and thus gives out a color similar to red color. However in wpf we do not have any equivalent function that takes long value for color, therefore i have written following code:


    Code:
    System.Drawing.Color colorLong = System.Drawing.Color.FromArgb(235); Brush color = new SolidColorBrush(Color.FromRgb( colorLong.R, colorLong.G, colorLong.B)); ctrl.Brush = color;
    The above code works for large values but for value say 235 in vb it is giving a color similar to red one but in wpf it is giving blue color .

    What is going wrong here? What to do in this case?

    Thanks in advance,
    Anurodh

  2. #2
    Join Date
    Jan 2009
    Posts
    36

    Re: Color code converter issue vb/wpf

    You are correct, System.Drawing.Color.FromArgb(235) will definitely give you a blue color. Why? You are specifying a 32bit value of the AARRGGBB format. 235 translates to 000000EB in hex. See http://msdn.microsoft.com/en-us/library/2zys7833.aspx. Red would be 00FF0000 in hex (16711680 in decimal).

    If you are just trying to get a red color you can use Colors.Red.
    Last edited by gurge60; March 25th, 2009 at 06:16 PM.

  3. #3
    Join Date
    Mar 2009
    Posts
    10

    Question Re: Color code converter issue vb/wpf

    Thanks for the reply.

    But we have some constants defined and we do not want to alter those files. So is there any way that i can get VB like color in WPF using the long value same as in VB.

    converter code is what i am looking for.

    Thanks,
    Anurodh

  4. #4
    Join Date
    Jan 2009
    Posts
    36

    Re: Color code converter issue vb/wpf

    I just learned that the format of VB colors is BBGGRR rather than .NETs AARRGGBB, so something like the following may help get all your color bits lined up:

    Code:
    Color ColorFromVB(int VBcolor)
    {
        Color c = Color.FromArgb(0xFF, //alpha
            (byte)(VBcolor & 0xFF), //red
            (byte)((VBcolor >> 8) & 0xFF), //green
            (byte)((VBcolor >> 16) & 0xFF)); //blue
    
        return c;
    }
    Now you can do the conversion with something like:

    Code:
    int origVBColor = 235;
    ctrl.Brush = new SolidColorBrush(ColorFromVB(origVBColor));
    Last edited by gurge60; March 27th, 2009 at 07:09 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center