VGColor¶
VGColor is a small, immutable RGBA color value used everywhere in the vector graphics API. Instead
of juggling raw float components you get a friendly object with named colors, hex and HSL
constructors, and handy tweaks like darken, lighten and
withAlpha. You hand a VGColor to
VGContext::fillColor or
strokeColor to paint your shapes.
Looking for a walkthrough?
For a gentle, example-first tour of colors (named colors, hex, HSL and adjusting lightness), see the Colors user guide.
Usage¶
Reach for a named color, build one from a hex string, or construct it directly from components in the
[0.0, 1.0] range.
use GL\VectorGraphics\VGColor;
$red = VGColor::red(); // a named color
$brand = VGColor::hex('#2ecc71'); // from a hex string
$warm = VGColor::irgb(255, 165, 0); // from 0-255 integer components
$raw = new VGColor(0.1, 0.2, 0.3, 1.0); // components in [0.0, 1.0]
// derive new colors without mutating the originals
$hover = $brand->lighten(0.1);
$ghost = $brand->withAlpha(0.5);
Properties¶
A VGColor exposes its four components as read/write float properties, each in the [0.0, 1.0]
range:
| Property | Meaning |
|---|---|
$r |
Red component. |
$g |
Green component. |
$b |
Blue component. |
$a |
Alpha component (0.0 fully transparent, 1.0 fully opaque). |
Constructors¶
rgb¶
RGB color constructor.
All values are in the range [0.0, 1.0], alpha defaults to fully opaque.
- arguments
-
float$rRedfloat$gGreenfloat$bBlue
- returns
-
\VGColorThe resulting color.
rgba¶
RGBA color constructor.
All values are in the range [0.0, 1.0].
- arguments
-
float$rRedfloat$gGreenfloat$bBluefloat$aAlpha
- returns
-
\VGColorThe resulting color.
irgb¶
RGB color constructor from integer values All values are in the range [0, 255]
- arguments
-
int$rRedint$gGreenint$bBlue
irgba¶
RGBA color constructor from integer values All values are in the range [0, 255]
- arguments
-
int$rRedint$gGreenint$bBlueint$aAlpha
hsl¶
HSL color constructor All values are in the range [0.0, 1.0]
- arguments
-
float$hHuefloat$sSaturationfloat$lLightness
hsla¶
HSL with alpha color constructor All values are in the range [0.0, 1.0]
- arguments
-
float$hHuefloat$sSaturationfloat$lLightness
hex¶
Hex color constructor
You can pass a hex string with or without the # prefix. (e.g. #FF0000 or FF0000)
Example: $color = VGColor::hex('#FF0000'); $color = VGColor::hex('FF0000'); $color = VGColor::hex('#000');
- arguments
-
string$hexThe hex string to convert to a color.
Named Colors¶
Convenient presets for the common cases. random and randomGray are handy for quick debugging and
visual tests.
red¶
The named color red.
@return VGColor
green¶
The named color green.
@return VGColor
blue¶
The named color blue.
@return VGColor
white¶
The named color white.
@return VGColor
black¶
The named color black.
@return VGColor
transparent¶
A fully transparent color.
@return VGColor
yellow¶
The named color yellow.
@return VGColor
cyan¶
The named color cyan.
@return VGColor
magenta¶
The named color magenta.
@return VGColor
orange¶
The named color orange.
@return VGColor
pink¶
The named color pink.
@return VGColor
purple¶
The named color purple.
@return VGColor
brown¶
The named color brown.
@return VGColor
gray¶
The named color gray.
@return VGColor
darkGray¶
A darker shade of gray.
@return VGColor
lightGray¶
A lighter shade of gray.
@return VGColor
random¶
A random opaque color, handy for quick debugging.
@return VGColor
randomGray¶
A random shade of gray, handy for quick debugging.
@return VGColor
Conversions & Adjustments¶
These return new VGColor (or vector) values and leave the original untouched.
getHSLA¶
Returns the color as a Vec4 where each component represents Hue, Saturation, Lightness and Alpha respectively.
getHSL¶
Returns the color as a Vec3 where each component represents Hue, Saturation and Lightness respectively.
getVec4¶
Returns the color as a Vec4 where each component represents Red, Green, Blue and Alpha respectively.
getVec3¶
Returns the color as a Vec3 where each component represents Red, Green and Blue respectively.
darken¶
Darkens the color by the specified amount.
- arguments
-
float$amountThe amount to darken the color by.
- returns
-
\VGColorThe darkened color.
lighten¶
Lightens the color by the specified amount.
- arguments
-
float$amountThe amount to lighten the color by.
- returns
-
\VGColorThe lightened color.
invert¶
Inverts the color.
- returns
-
\VGColorThe inverted color.
withAlpha¶
Returns a new color with modified alpha.
- arguments
-
float$alphaThe new alpha value (0.0 to 1.0).
- returns
-
\VGColorA new color with the specified alpha.
copy¶
Returns a copy of the color object.
- returns
-
\VGColorA copy of this color.
contrast¶
Returns either black or white depending on what provides better contrast for this color.
Uses perceived luminance to determine which color (black or white) would be more readable when displayed on top of this color.
- returns
-
\VGColorEither black or white color for optimal contrast.