Color Representation inside PEAR Packages

In case a package has to deal with colors, it is recommended that developers store and use an array representation of the color values in their PEAR packages. All values are in the range of 0..255.

RGB represesentation

The following snippet defines an array that holds the RGB definition of the green coloring that is used in the official PEAR logo.
$color = array(0x33, 0x99, 0x00);
or
$color = array(51, 153, 0);
Please note that the internal, numerical representation of both forms of the example are equal. The values represent the RGB (red, green, blue) values. It was decided not to use "r", "g" and "b" as indices for the array, because numerical indices allow easier creation of color arrays in the sourcecode, make handling less complicated and are an already commonly used array representation for colors.

RGB representation with alpha-values

Since this an extension of the RGB representation above, it is recommended to use a forth value for alpha-channel-prepresentation. An example of the "PEAR-green" with aprox. 80% intensity would be:
$color = array(0x33, 0x99, 0x00, 0xCC);
For consistency the alpha-value is also from the range 0..255. A value of 255 means fully opaque (full color-intensity, same as in RGB- notation), 0 means fully transparent.

Please note this is in contrast to the alpha-value used by imagecolorallocate(). The alpha-representation used in PEAR was choosen for consistency with the other RGB values and because it is commonly accepted practise in many other applications (image-processing tools, etc.).

Other color representations than RGB(A)

Since RGB/RGBA will used for graphic generation on a computer we decided to leave out an optional type-identifier classifying colors as RGB/RGBA. However for different color-representations an identifier is needed. The optional identifier "type" can be added to the color array.
$color = array(0x33, 0x99, 0x00, 0xCC, "type" => "RGB");
Currently the following types are definied, with array-indices from 0..n follwing the colornames listed behind them:

Please note that also the RGBA representation has the type "RGB" since it's closely related.

Conversion to/between the color-representations

The PEAR-package Image_Color will (hopefully soon) contain all needed functions to convert arbitrary color formats to the internal RGBA representation and convert different color representations.

Status: Currently Image_Color already offers functions to convert color-names (e.g. "green") and hex-representations-strings (e.g. "#339900") to the PEAR-RGBA-format. We are working to get alpha- channel support added hopefully soon and will later add funtions for CYMK-conversion as well.