Skip to content

04 – Importing A Color

Nicholas Jordon edited this page Sep 25, 2016 · 5 revisions

Importing a color is fairly straight forward, and can be done several ways.

Importing Hexadecimal Colors

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.

Example:

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);

Importing RGB Colors

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]);

Importing HSL Colors

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]);

Importing HSB/HSV Colors

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]);

Importing CMYK Colors

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]);

Importing The Alpha Channel

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);

Handling Import Errors

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();


Previous Page - Optimization & Caching     Next Page - The Alpha Channel

Clone this wiki locally