MigraDoc - Setting Cell Colour from Hex
Asked Answered
K

1

5

Have a HEX colour code string in the database ("#ADD8E6") and I want to use this to change the background colour of a MigraDoc cell. I have found Color.Parse() function, however it isn't changing the colour of my cell. I have had to do the following:

string colourHex = (database.HexCode).Replace("#", "0x");
var colourObject = MigraDoc.DocumentObjectModel.Color.Parse(colourHex);

Cell.Shading.Color = colourObject;

I know that the Cell.Shading.Color is correct because if I apply Cell.Shading.Color = Colors.AliceBlue then the cell does change colour as expected. I understand the Color.Parse requires the HEX code to start with 0x rather than #. I tried using the # and it failed... At least with what I have got it is rendering... just not with my colour.

Keek answered 2/3, 2018 at 23:24 Comment(3)
I think your code is correct, so something you are assuming must be wrong.Overhead
I think the question is on-topic and provides enough information - at least to those familiar with MigraDoc. Several senseless question never got closed (my close votes expired).Scute
Your question inspired me to enhance the Color.Parse method (see my answer). Thanks for asking your question.Scute
S
9

You have to replace "#" with "0xff" to get what you want.

With your short numbers (three components only) the alpha channel will always be 0 and the colour will be completely transparent. With 0xff followed by six hex digits for the RGB colours you get a colour with full opacity.

Update: With the current version of MigraDoc 1.50 (beta 5b or later) you can also use the hash sign followed by 3, 6, or 8 hex digits. 8 digits include the alpha channel, with 3 or 6 digits an alpha setting of FF is used.
With the new version, the code #ADD8E6 will have the expected effect.
Nothing has changed when the 0x prefix is used.

Scute answered 3/3, 2018 at 8:59 Comment(1)
Amazing - many thanks for that! Worked perfectly...! Not sure why this is considered 'Off-Topic'??Keek

© 2022 - 2024 — McMap. All rights reserved.