Click to See Complete Forum and Search --> : Color code converter issue vb/wpf


anurodhora
March 14th, 2009, 05:05 AM
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:



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

gurge60
March 16th, 2009, 07:19 PM
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.

anurodhora
March 17th, 2009, 08:37 AM
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

gurge60
March 27th, 2009, 08:05 PM
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:


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:


int origVBColor = 235;
ctrl.Brush = new SolidColorBrush(ColorFromVB(origVBColor));