10quality/php-css-color-parser

Package used to parse CSS colors in order to normalize them into different formats.

v1.0.5 2018-03-29 17:15 UTC

This package is auto-updated.

Last update: 2024-11-09 05:21:01 UTC


README

Latest Stable Version Total Downloads License

A little package used to parse CSS colors in order to normalize them into different formats (supported: hex, argb and rgba).

Requirements

  • PHP >= 5.4

Install

composer require 10quality/php-css-color-parser

Usage

Use statement:

use TenQuality\Utility\Color\CssParser;

For a normalized HEX code:

// This will echo "#44FCCD"
echo CssParser::hex('#44fCCd');

// This will echo "#44FFFF"
echo CssParser::hex('#4ff');

// This will echo "#FFFFFF"
echo CssParser::hex('white');

// This will echo "#89CC7F"
echo CssParser::hex('89cc7F');

For a normalized HEX code (with transparency):

// This will echo "#44FCCD44"
echo CssParser::hexTransparent('#44fCCd44');

// This will echo "#44FFFFFF"
echo CssParser::hexTransparent('#4ff');

// This will echo "#FFFFFFFF"
echo CssParser::hexTransparent('white');

For ARGB:

// This will echo "0x4444FCCD"
echo CssParser::argb('#44fCCd44');

// This will echo "0xFF44FFFF"
echo CssParser::argb('#4ff');

// This will echo "0xFFFFFFFF"
echo CssParser::argb('white');

For RGBA:

// This will echo "rgba(57,115,157,0.53)"
echo CssParser::rgba('#39739d88');

// This will echo "rgba(255,255,255,1)"
echo CssParser::rgba('white');

Casting

To return the color's rgba codes as an array:

// This will dump the following array "[57,115,157,255]"
var_dump(CssParser::array('#39739d'));

To return the color's rgba codes as a JSON string:

// This will echo "{"red":57,"green":115,"blue":157,"alpha":255}"
echo CssParser::string('#39739d');

Alpha

Default alpha can be changed from FF to 00 by calling to the following static method:

CssParser::setAlpha('0');
// Or
CssParser::setAlpha(CssParser::ALPHA_TRANSPARENT);

Resulting in:

// This will echo "#44FFFF00"
echo CssParser::hexTransparent('#4ff');

// This will echo "rgba(255,255,255,0)"
echo CssParser::rgba('white');

// This will echo "0x0044FFFF"
echo CssParser::argb('#4ff');

To restore the default alpha call:

CssParser::setAlpha('F');
// Or
CssParser::setAlpha(CssParser::ALPHA_OPAQUE);

Extending named colors

To add more CSS named colors:

// This will exho "#00008B"
echo CssParser::hex('darkblue', ['/darkblue/','/darkgreen/'], ['00008B','006400']);

NOTE: Second parameter passed by containes the list of additional css labels (names) to parse and the third paramener contains its HEX code in caps and without the hashtag character.

Copyright & License

MIT License.

(c) 2018 10 Quality