13

I'm rather new to C# and find it almost unspeakable that there isn't a simple way for converting an RGB color or system.color to a WdColor!

VB is simple, C# - is it really that hard to do?

I do not want to reference VB in my project.

I'm using this in some word automation project to color a font, e.g.

tmpRange.Find.Replacement.Font.Color = Color.FromArgb(100, 150, 75); 

But this above line isn't possible, it needs to be a WdColor.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1320651
  • 808
  • 2
  • 15
  • 42
  • 3
    Neither System.Color, nor Microsoft.Office.Interop.Word.WdColor are part of the C# programming language. Whatever you could do with WdColor with VB.NET, you can do with C#. – John Saunders Oct 25 '12 at 19:05
  • Understand, but I cant see how to do this in C#. VB its very simple to do, C# there countless articles and not found a single reference that does what I need it to do. Yet there are 1000's on VB NONE in C# that I can find... – user1320651 Oct 25 '12 at 19:13
  • 1
    Is it really that hard for you to translate the VB to C#? – John Saunders Oct 25 '12 at 19:14
  • RGB function isnt there in C# – user1320651 Oct 25 '12 at 19:14

3 Answers3

21
Color c = Colors.Blue;
var wdc = (Microsoft.Office.Interop.Word.WdColor)(c.R + 0x100 * c.G + 0x10000 * c.B);
Cole Cameron
  • 2,213
  • 1
  • 13
  • 12
  • Can you tell what you've done in (c.R + 0x100 * c.G + 0x10000 * c.B), have you created a hexValue ? – pxm Feb 26 '14 at 13:55
  • 1
    I'm summing the Red, Green, and Blue values of the named color, multiplying the Green & Blue values by hexadecimal modifiers so that the value is effectively 0xBBGGRR (base doesn't really matter, an integer's an integer), then casting that to the `WdColor` enumeration type. – Cole Cameron Feb 27 '14 at 14:03
6

Add a Reference to Microsoft.VisualBasic dll

using Microsoft.VisualBasic;

int rgbColor = Information.RGB(100, 150, 75);
Word.WdColor wdColor = (Word.WdColor)rgbColor;
Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
4

I thought people might like an extension method:

    public static void ColorRGB(this Wd.Font font, int red, int green, int blue)
    {
        font.Color = (Wd.WdColor)(red + 0x100 * green + 0x10000 * blue);
    }
Matt Fitzmaurice
  • 1,319
  • 4
  • 19
  • 40
  • 1
    An extension method with side-effects is a bad extension method. A good extension method would simply intake a `System.Drawing.Color` and **return** a `WdColor` value. – Mathieu Guindon May 02 '16 at 20:17
  • Can you explain that to me? What are the side-effects? Should I have renamed the method SetColorFromRGB(..)? Doesn't that mean it's an intentional effect rather than a side-effect? – Matt Fitzmaurice May 04 '16 at 07:06
  • Put it this way then - which is more flexible and useful, an extension method that returns a `WdColor` given a `Color`, or one that sets the font color given a `Font` and red/green/blue values? With the former you only need to code that logic once; with the latter, you'll be copying/pasting the logic when you need another method to set the color of, say, a `Border`. – Mathieu Guindon May 04 '16 at 13:43
  • I like the sound of what you are suggesting, but I don't see how a Color to WdColor extension method helps. The problem I was solving was to take Red Green and Blue values. – Matt Fitzmaurice May 05 '16 at 04:07