-
Notifications
You must be signed in to change notification settings - Fork 1
RGBAColor
This utility class is used to hold information on an RGB based color, with optional opacity / alpha channel.
To easily work with the many possible color value ranges, each color
can be specified using a special ColorChannel
class, which allows
specifying the color value the way you prefer:
- 8-Bit integer:
0
-255
(Standard color values) - 7-Bit integer:
0
-127
(PHP GD style alpha channel) - Decimal:
0.00
-1.00
(CSS opacity, for example) - Percent:
0
-100
(General purpose)
- Red
- Green
- Blue
- Opacity
- Transparency (Inverted opacity)
use AppUtils\RGBAColor\ColorFactory;
$red = ColorFactory::createFromHEX('CC0000');
// Shorthand syntax
$gray = ColorFactory::createFromHEX('CCC');
// With opacity
$overlay = ColorFactory::createFromHEX('00000066');
use AppUtils\RGBAColor\ColorFactory;
$white = ColorFactory::create8Bit(255, 255, 255, 255);
use AppUtils\RGBAColor\ColorFactory;
// Indexed array
$white = ColorFactory::createPercent(array(100, 100, 100, 100));
// Associative array
$white = ColorFactory::createPercent(array(
'red' => 100,
'green' => 100,
'blue' => 100,
'alpha' => 100
));
PHP's GD library uses 8-Bit values for the color channels, and 7-Bit for the opacity.
use AppUtils\RGBAColor\ColorFactory;
$white = ColorFactory::createGD(255, 255, 255, 127);
use AppUtils\RGBAColor\ColorFactory;
$white = ColorFactory::createPercent(100, 100, 100, 100);
In style sheets, the color channels are 8-Bit values, and the opacity is a decimal from 0-1.
use AppUtils\RGBAColor\ColorFactory;
$white = ColorFactory::createCSS(255, 255, 255, 1);
Each color is specified using a color channel instance, giving the most flexibility in the type of value per channel.
In the following example, every color uses a different value:
use AppUtils\RGBAColor\ColorFactory;
use AppUtils\RGBAColor\ColorChannel;
$white = ColorFactory::create(
ColorChannel::eightBit(255),
ColorChannel::sevenBit(127),
ColorChannel::percent(100),
ColorChannel::decimal(1)
);
While it is not possible to automatically detect the type from numeric color values, some other formats can be used:
- An empty or NULL value
- A HEX color string
- An 8-Bit color array
- An existing
RGBAColor
instance - A preset name
The match()
method compares two colors, ignoring the opacity. Two colors that have the same RGB values but different opacity values will be considered the same.
use AppUtils;
$colorA = ColorFactory::createFromHEX('CCC');
$colorB = ColorFactory::createFromHEX('CCC');
if($colorA->matches($colorB))
{
echo 'Colors match.';
}
else
{
echo 'Colors do not match.';
}
Output:
Colors match.
The matchAlpha()
method compares two colors, including their opacity. To match, colors must both have the same RGB values and opacity value.
use AppUtils;
$colorA = ColorFactory::createFromHEX('CCCCCCDD');
$colorB = ColorFactory::createFromHEX('CCCCCCEE');
if($colorA->matchesAlpha($colorB))
{
echo 'Colors match.';
}
else
{
echo 'Colors do not match.';
}
Output:
Colors do not match.
New here?
Have a look at the overview for a list of all helper classes available in the package.
Table of contents
Find the current page in the collapsible "Pages" list above, and expand the page, to view a table of contents.