Adding Drop Shadows with PHP - Allocating the Color Pallette
(Page 4 of 7 )
Color in the world of computers and televisions is based on the additive RGB color scale. The idea is that each color can be obtained by mixing various intensities of red (R), green (G) and blue (B) light together.
A minimum value is 0 and a maximum value is 255 for a combined total of 16.7 million available colors. The color white is represented by a full intensity of all colors: RGB 255,255,255. The color black is represented by an absence of all colors: RGB 0,0,0.
Since a set of three values work together to form one color, we can handle them in terms of an array.
An area completely covered by the shadow will be black. An area completely free of the shadow will be the canvas' background color, which we've set to white.
The drop shadow will be drawn as a series of overlapping rectangles, one on top of the other. The first rectangle will be the background color then each successive rectangle will be slightly smaller and one shade closer to black. This will create the illusion of black fading to the background color.
To "fade" from black to the background color, the value of each color component will need to change a certain amount per step.
The $step_offset will hold the values each component will change between one color and the next to create a smooth gradient effect.
Depending on the original background color and the defined value of the DS_STEPS constant, the division to determine the offset for a particular color component could result in a floating value. Don't round the value at this point or you may end up with a compounding round off error and the colors won't work out properly.
Each color needed can now be calculated and allocated in the image's color palette. This is where the value may be rounded.
A for loop is used to assign each needed color to the palette. The imagecolorallocate function accepts the image and the color's components to add and returns an identifier that we'll use to reference the color.