ARTICLE UNDER CONSTRUCTION
Colors
Version 6.1.0
In this article
- Using predefined colors
- From RGB or CMYK value
- Working with opacity
- From hexadecimal value
- Color channels
This article describes how the color of an object is changed and introduces the Color and Colors classes.
Using predefined colors
The Colors class holds a set of standard Color instances that can be accessed by name. You will find here the predefined colors you know from System.Drawing.Color, but as MigraDoc.DocumentObjectModel.Color instances. You can simply assign any of those properties to a property of the type Color:
font.Color = Colors.CornflowerBlue;
Note that all those colors are initialized as RGB colors.
See full UsingPredefinedColors sample in Colors in PDFsharp.Sample repository. Show resulting PDF
From RGB or CMYK value
Each Color instance, except the empty color, is created as an RGB or a CMYK color. Nevertheless, you can also read the values of the channels of the other color model (see Color channels).
You can create a new Color instance of any desired RGB value by calling the constructor with the parameters r, g and b. Those parameters are of the type byte. Their range is from 0 to 255 in the additive RGB color model.
font.Color = new Color(255, 128, 64);
Alternatively, you can use the static FromRgb method with the same signature:
font.Color = Color.FromRgb(255, 128, 64);
See full FromRgbOrCmyk sample in Colors in PDFsharp.Sample repository. Show resulting PDF
The same way, you can create Color instances of CMYK values. The appropriate constructor comes with the parameters c, m, y and k. Those are of the type double. The accepted range is from 0 to 100 in the subtractive CMYK color model.
font.Color = new Color(100, 50, 25, 12.5);
Alternatively, you can use the static FromCmyk method with the same signature:
font.Color = Color.FromCmyk(100, 50, 25, 12.5);
See full FromRgbOrCmyk sample in Colors in PDFsharp.Sample repository. Show resulting PDF
Working with opacity
By using the constructor beginning with the a value, you can set an opacity from 0 to 255 for an RGB color:
font.Color = new Color(64, 255, 128, 64);
Alternatively, you can use the static FromArgb method with the same signature:
font.Color = Color.FromArgb(64, 255, 128, 64);
See full WorkingWithOpacity sample in Colors in PDFsharp.Sample repository. Show resulting PDF
For CMYK values, you can use the constructor beginning with the alpha value, which accepts a double in the range 0-100:
font.Color = new Color(25, 100, 50, 25, 12.5);
Alternatively, you can use the static FromCmyk method with the same signature:
font.Color = Color.FromCmyk(25, 100, 50, 25, 12.5);
See full WorkingWithOpacity sample in Colors in PDFsharp.Sample repository. Show resulting PDF
You can also create a new RGB Color instance providing an existing color and a new a value:
font.Color = Color.FromRgbColor(64, color);
To create a new CMYK color, you do the same this way with a new alpha value:
font.Color = Color.FromCmykColor(25, color);
See full WorkingWithOpacity sample in Colors in PDFsharp.Sample repository. Show resulting PDF
From hexadecimal value
Using the appropriate constructor, you can initialize a new RGB Color instance from a hexadecimal value:
font.Color = new Color(0x40ff8040);
The uint holding that value is interpreted as 0xaarrggbb.
See full FromHexValue sample in Colors in PDFsharp.Sample repository. Show resulting PDF
Alternatively, you can create an RGB color by parsing different kinds of hexadecimal strings using the Parse method.
With a "0x" prefix, 7 or 8 digits of the scheme 0x(a)arrggbb are expected:
font.Color = Color.Parse("0x40ff8040");
With a "#" prefix, 8, 6 or 3 digits may follow. For 8 digits, the string is interpreted as 0xaarrggbb:
font.Color = Color.Parse("#40ff8040");
For 6 digits, the string is interpreted as 0xrrggbb with an opacity of 255:
font.Color = Color.Parse("#ff8040");
For 3 digits, the string is interpreted as 0xrgb with an opacity of 255 and each color component digit duplicated:
font.Color = Color.Parse("#f84");
Here, the value "#f84" will be interpreted as 0xffff8844.
See full FromHexValue sample in Colors in PDFsharp.Sample repository. Show resulting PDF
Color channels
The properties A, R, G and B represent the color channels of the RGB color model. They are of the type byte and return values from 0 to 255. The properties Alpha, C, M, Y and K represent the color channels of the CMYK color model. They are of the type double and return values from 0 to 100.
The properties representing the color model the color was created in, will return the set values. The properties representing the other color model will depict the original color converted to this color model.
By querying the IsCmyk property, you can determine, if a color was created as RGB or CMYK color.
See ColorChannels sample in Colors in PDFsharp.Sample repository. Show resulting PDF