PHP code example of devlop / colours

1. Go to this page and download the library: Download devlop/colours library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devlop / colours example snippets


use Devlop\Colours\HexColor;

// check validity
HexColor::isValid('#fff'); // bool(true)
HexColor::isValid('#0B33B1'); // bool(true)
HexColor::isValid('#19b9cd'); // bool(true)
HexColor::isValid('#b9cd'); // bool(false)
HexColor::isValid('red'); // bool(false)
HexColor::isValid('19b9cd'); // bool(true)

// check validity (strict mode)
HexColor::isValid('19b9cd', true); // bool(false)

// creating from hex string
$hexColor = new HexColor('#f0f');

// creating from other formats
$hexColor = HexColor::fromHslColor($hslColorInstance);
$hexColor = HexColor::fromRgbColor($rgbColorInstance);
$hexColor = HexColor::fromCmykColor($cmykColorInstance);

// get the hex string
$hexColor->getHexString(); // string('#ff00ff')

// converting to other formats
$hslColor = $hexColor->toHsl();
$rgbColor = $hexColor->toRgb();
$cmykColor = $hexColor->toCmyk();

use Devlop\Colours\HslColor;

// creating from HSL values
$hslColor = new HslColor(180, 96, 77);

// creating from other formats
$hslColor = HslColor::fromHexString('#b187F5');
$hslColor = HslColor::fromHexColor($hexColorInstance);
$hslColor = HslColor::fromRgbColor($rgbColorInstance);

// get HSL properties
$hslColor->getHue(); // int(180)
$hslColor->getSaturation(); // int(96)
$hslColor->getLightness(); // int(77)

// converting to other formats
$hexColor = $hslColor->toHex();
$rgbColor = $hslColor->toRgb();

use Devlop\Colours\RgbColor;

// creating from RGB values
$rgbColor = new RgbColor(221, 186, 146);

// creating from other formats
$rgbColor = RgbColor::fromHexString('#f3840c');
$rgbColor = RgbColor::fromHexColor($hexColorInstance);
$rgbColor = RgbColor::fromHslColor($hslColorInstance);
$rgbColor = RgbColor::fromCmykColor($cmykColorInstance);

// get RGB properties
$rgbColor->getRed(); // int(221)
$rgbColor->getGreen(); // int(186)
$rgbColor->getBlue(); // int(146)

// converting to other formats
$hexColor = $rgbColor->toHex();
$hslColor = $rgbColor->toHsl();
$cmykColor = $rgbColor->toCmyk();

use Devlop\Colours\CmykColor;

// creating from CMYK values
$cmykColor = new CmykColor(20, 80, 65, 34);

// creating from other formats
$cmykColor = CmykColor::fromHexString('#f3840c');
$cmykColor = CmykColor::fromHexColor($hexColorInstance);
$cmykColor = CmykColor::fromRgbColor($rgbColorInstance);

// get CMYK properties
$cmykColor->getCyan(); // int(20)
$cmykColor->getMagenta(); // int(80)
$cmykColor->getYellow(); // int(65)
$cmykColor->getKey(); // int(34)

// converting to other formats
$hexColor = $cmykColor->toHex();
$rgbColor = $cmykColor->toRgb();