In my VSTO application, I was struggling to convert from WdColor enum to the Color struct. WdColor uses 24-bit integers but they are not RGB.
As it turns out, WdColor seems to use BGR (the bytes are "backwards").
There are surely better mathematical ways of accomplishing this, but for what it's worth here's my solution:
public class WdColorColorConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
if (!(value is WdColor))
else if ((WdColor)value == WdColor.wdColorAutomatic)
int intValue = ((WdColor)value).GetHashCode();
string hexValue = intValue.ToString("X").PadLeft(6, '0');
string redHex = hexValue.Substring(4, 2);
string greenHex = hexValue.Substring(2, 2);
string blueHex = hexValue.Substring(0, 2);
byte redByte = byte.Parse(redHex, System.Globalization.NumberStyles.AllowHexSpecifier);
byte greenByte = byte.Parse(greenHex, System.Globalization.NumberStyles.AllowHexSpecifier);
byte blueByte = byte.Parse(blueHex, System.Globalization.NumberStyles.AllowHexSpecifier);
Color c = Color.FromRgb(redByte, greenByte, blueByte);
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
throw new NotImplementedException();
This code works very well except when the wdcolor is negative sometimes. In that case using this function, some strange color is generated. Do you have any idea about fixing it?
Thanks in Advance
Microsoft is conducting an online survey to understand your opinion of the Msdn Web site. If you choose to participate, the online survey will be presented to you when you leave the Msdn Web site.
Would you like to participate?