The RENDER_SVG function reads a file in SVG (Scalable Vector Graphics) format, renders the graphics into an internal buffer, and returns a byte array containing the rendered image.

Examples


Read an SVG file within the IDL distribution and display it:

file = filepath('folder-open.svg', subdir=['resource', 'bitmaps', 'svg', 'regular'])
img = RENDER_SVG(file)
help, img
i = image(img)

IDL prints:

IMG BYTE = Array[576, 512, 4]

Now render the image into a much smaller icon. Also add a gray background color and change the foreground color to red:

img = RENDER_SVG(file, width=128, height=128, background=0xCCCCCC, foreground=0xFF0000)
help, img
i = image(img)

IDL prints:

IMG BYTE = Array[128, 128, 3]

Tip: If the image width and height do not match the aspect ratio of the original SVG, the result will be padded with the background color.

Syntax


Result = RENDER_SVG( Filename, BACKGROUND_COLOR=value, FOREGROUND_COLOR=value, HEIGHT=value, WIDTH=value)

Return Value


RENDER_SVG returns a byte array containing the rendered SVG data. The dimensions of the Result are either M x N x 3 or M x N x 4 depending upon whether the BACKGROUND_COLOR keyword was specified or not. By default the width M and height N are derived from the bounding box in the SVG file. However, you can override the width or height (or both) if you want to render the SVG into a different size.

Tip: If the image width and height do not match the aspect ratio of the original SVG, the result will be padded with the background color.

Arguments


Filename

A scalar string specifying the full pathname of the SVG file to render.

Keywords


BACKGROUND_COLOR

Set this keyword to a scalar integer or three-element byte array containing the background color for the resulting image. This color should either be a hexadecimal number in the format 0xRRGGBB (where RR is the red value, GG is green , and BB is blue), or a three-element byte array [Red, Green, Blue].

IF BACKGROUND_COLOR is specified, then the result is an RGB image with dimensions M x N x 3. The background color will be blended into the image using the transparency value (the alpha channel) at each pixel. The equations are:

redResult = ( backgroundRed * (255 - alpha) + redScaled * alpha ) / 255
greenResult = ( backgroundGreen * (255 - alpha) + greenScaled * alpha ) / 255
blueResult = ( backgroundBlue * (255 - alpha) + blueScaled * alpha ) / 255

where redScaled, greenScaled, and blueScaled are derived from the FOREGROUND_COLOR.

IF BACKGROUND_COLOR is not specified, then the result is an RGBA image with dimensions M x N x 4. In this case the alpha channel in the SVG image will be returned unchanged.

FOREGROUND_COLOR

Set this keyword to a scalar integer or three-element byte array containing the foreground color for the resulting image. This color should either be a hexadecimal number in the format 0xRRGGBB (where RR is the red value, GG is green , and BB is blue), or a three-element byte array [Red, Green, Blue]. The default foreground color is black, 0x000000, which has no effect.

A foreground color of 0 on a particular channel will have no effect on that channel, while a foreground color of 0xFF (255) on that channel will make the entire channel equal to 255. This has the effect of "colorizing" the image in that color. In particular, each pixel will have an output value of:

redScaled = pixelRed + (255 - pixelRed) * foregroundRed / 255
greenScaled = pixelGreen + (255 - pixelGreen) * foregroundGreen / 255
blueScaled = pixelBlue + (255 - pixelBlue) * foregroundBlue / 255

Tip: FOREGROUND_COLOR is especially useful for black-and-white "icons" where you want the icon to appear in a certain color instead of just black. For SVG images that already have color, the foreground color will be linearly blended in with the existing colors which may produce unexpected results.

HEIGHT

Set this keyword to an integer giving the desired height of the result. If this keyword is not specified then the height will be automatically computed from either the bounding box (if WIDTH is not specified) or aspect ratio (if WIDTH is specified) of the original SVG file.

WIDTH

Set this keyword to an integer giving the desired width of the result. If this keyword is not specified then the width will be automatically computed from either the bounding box (if HEIGHT is not specified) or aspect ratio (if HEIGHT is specified) of the original SVG file.

Version History


9.1

Introduced

See Also


IMAGE, RENDER_ICON, SVG_ICON_BROWSER