Skip to content
Using color in PDF documents

Using color in PDF documents

Every creator call that takes a color takes a creator.Color, and six are already declared:

blackColor := creator.ColorBlack
whiteColor := creator.ColorWhite
redColor := creator.ColorRed
greenColor := creator.ColorGreen
blueColor := creator.ColorBlue
yellowColor := creator.ColorYellow

Beyond those, build your own from RGB, CMYK or gray.

RGB

Three constructors, differing only in how you write the value.

red := creator.ColorRGBFrom8bit(255, 0, 0)             // 0-255 per channel
green := creator.ColorRGBFromArithmetic(0.0, 1.0, 0.0) // 0.0-1.0 per channel
blue := creator.ColorRGBFromHex("#0000ff")             // #rrggbb or #rgb

ColorRGBFromHex does not return an error. A string that is not 4 or 7 characters long, or does not start with #, logs at debug level and gives you black, so a typo in a hex code shows up as black text rather than as a failure. ColorRGBFrom8bit takes byte parameters, which is what stops an out-of-range value in the first place, and ColorRGBFromArithmetic clamps to 0.0 - 1.0.

CMYK

cyan := creator.ColorCMYKFrom8bit(100, 0, 0, 0)                // 0-100 per channel
magenta := creator.ColorCMYKFromArithmetic(0.0, 1.0, 0.0, 0.0) // 0.0-1.0 per channel

Despite the name, ColorCMYKFrom8bit is on a 0 to 100 scale, not 0 to 255, and clamps anything above 100. A CMYK color stays CMYK in the output: it is written as a DeviceCMYK color rather than converted to RGB on the way out, which is what you want for print work.

Gray

ColorGrayFrom8bit, ColorGrayFromArithmetic and ColorGrayFromHex produce a DeviceGray color. The hex form takes one or two digits, #f or #ff, and falls back to black on anything else in the same silent way as the RGB one.

ColorGrayFromArithmetic does no clamping, unlike its RGB and CMYK counterparts. A value outside 0.0 - 1.0 is passed straight through to the content stream.

Last updated on