-
Notifications
You must be signed in to change notification settings - Fork 8
04 – Importing A Color
Importing a color is fairly straight forward, and can be done several ways.
Strings and integers are always assumed to be Hexadecimal values. Strings can be either be 3 or 6 characters long, and can be optionally prefixed with a octothorpe (#). Integers are interpreted as PHP hexadecimals but can be represented as any integer that PHP can interpret.
use \projectcleverweb\color\main as color;
// The below all result in the same object
$color = new color('#333333');
$color = new color('#333');
$color = new color('333333');
$color = new color('333');
// NOTE: 0x333333 IS NOT the same as 0x333
$color = new color(0x333333);
$color = new color(3355443);
RGB colors can only be imported as arrays with the keys set to 'r', 'g', and 'b' respectively.
use \projectcleverweb\color\main as color;
$color = new color(['r' => 255, 'g' => 0, 'b' => 0]);
HSL colors can only be imported as arrays with the keys set to 'h', 's', and 'l' respectively.
use \projectcleverweb\color\main as color;
$color = new color(['h' => 0, 's' => 100, 'l' => 50]);
HSB (aka HSV) colors can only be imported as arrays with the keys set to 'h', 's', and 'b' respectively.
use \projectcleverweb\color\main as color;
$color = new color(['h' => 0, 's' => 100, 'b' => 100]);
CMYK colors can only be imported as arrays with the keys set to 'c', 'm', 'y', and 'k' respectively.
use \projectcleverweb\color\main as color;
$color = new color(['c' => 0, 'm' => 100, 'y' => 100, 'k' => 0]);
If a color has transparency, this is represented in the alpha channel. To import the alpha channel you can either use the 'a' key OR set it through the main::alpha()
method.
use \projectcleverweb\color\main as color;
$color = new color(['r' => 255, 'g' => 0, 'b' => 0, 'a' => 50]);
// OR
$color = new color(['r' => 255, 'g' => 0, 'b' => 0]);
$color->alpha(50);
When error occurs with an import, an error will be triggered and the color will be set to black (#000000);
use \projectcleverweb\color\main as color;
try {
$color = new color(['invalid input']);
} catch (\projectcleverweb\color\exception $e) {
// Handle Error
var_dump($e);
}
// Output: 000000
echo $color->hex();
Copyright © 2016 Nicholas Jordon
This documentation is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.