Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Colors

svg_color_constant Interface
Example of using svg_color_constant
svg_color interface
Example of using svg_color
Internals and Rationale

The project supports any RGB color, as well as a number of constants that are named by the SVG standard.

svg_color_constant Interface

svg_color_constant is simply an enumerated list. The colors are defined here. The list contains all of your expected colors, such as black and red. The list contains one extra color element, blank, used when you need to pass a color, but would not like it to show up. This comes in handy for defining defaults for functions, for example.

Example of using svg_color_constant

using namespace boost::svg;

svg_2d_plot my_plot;

svg_color_constant my_const = red;

my_plot.background_border_color(my_const);
my_plot.background_color(lightgray);
[Note] Note

svg_color has a constructor for svg_color_constant, so you can use a svg_color_constant in place of a svg_color and it will be implicitly converted. However, there is not currently an svg_color::operator=(svg_color_constant) overload, so

svg_color my_color = red;

does not work.

svg_color interface

You can define a svg_color using two different constructors

// The parameters are red, green, and blue respectively.
svg_color(int, int, int);

// Use a pre-existing color constant.
svg_color(svg_color_constant);
[Important] Important

Any integer value is accepted by the SVG standard, but negative values are rounded to 0, and positive values > 255 are rounded down to 255.

Example of using svg_color

using namespace boost::svg;

svg_color my_white(255, 255, 255);
svg_color const_white(white);

BOOST_ASSERT(my_white == const_white);
[Note] Note

svg_color's constructor takes in three integer values. The SVG 1.1 standard allows any integer to represent an RGB value, with values less than 0 and greater than 255 being rounded to their respective min and max.

Internals and Rationale

Constants are defined in an enum, svg_color_constant, in alphabetical order. This facilitates quick lookup of their RGB values from an array. Anywhere that a svg_color can be used, a svg_color_constant can be used, as the conversion is implicit.

All color information is stored in RGB format in a svg_color struct. The rationale for storing information in RGB format is because it is precise, and is always representable the exact same way. Storing a floating point percentage introduces the possibility of rounding error, which I would like to avoid at all costs.

Copyright © 2007 Jake Voytko

PrevUpHomeNext