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 representation

The following snippet defines an array that holds the RGB definition of the green coloring that is used in the official PEAR logo.

<?php
$color 
= array(0x330x990x00);
?>

or

<?php
$color 
= array(511530);
?>

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 fourth value for alpha-channel-presentation. An example of the "PEAR-green" with approximately 80% intensity would be:

<?php
$color 
= array(0x330x990x000xCC);
?>

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 chosen for consistency with the other RGB values and because it is commonly accepted practice 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.

<?php
$color 
= array(0x330x990x000xCC"type" => "RGB");
?>

Currently the following types are defined, with array-indices from 0..n following the color names listed beside them:

  • RGB - red, green, blue
  • CYMK - cyan, yellow, magenta, black (key)

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 functions for CYMK-conversion as well.

    Поддержать сайт на родительском проекте КГБ