https://github.com/10quality/php-css-color-parser
A little package used that parses CSS colors in order to normalize them into different formats.
https://github.com/10quality/php-css-color-parser
Last synced: about 1 year ago
JSON representation
A little package used that parses CSS colors in order to normalize them into different formats.
- Host: GitHub
- URL: https://github.com/10quality/php-css-color-parser
- Owner: 10quality
- License: mit
- Created: 2018-03-13T00:29:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-07T21:29:49.000Z (over 7 years ago)
- Last Synced: 2025-03-23T20:23:09.164Z (about 1 year ago)
- Language: PHP
- Size: 12.7 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CSS Color Parser
[](https://packagist.org/packages/10quality/php-css-color-parser)
[](https://packagist.org/packages/10quality/php-css-color-parser)
[](https://packagist.org/packages/10quality/php-css-color-parser)
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
```bash
composer require 10quality/php-css-color-parser
```
## Usage
Use statement:
```php
use TenQuality\Utility\Color\CssParser;
```
For a normalized HEX code:
```php
// 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):
```php
// 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:
```php
// 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:
```php
// 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:
```php
// 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:
```php
// 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:
```php
CssParser::setAlpha('0');
// Or
CssParser::setAlpha(CssParser::ALPHA_TRANSPARENT);
```
Resulting in:
```php
// 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:
```php
CssParser::setAlpha('F');
// Or
CssParser::setAlpha(CssParser::ALPHA_OPAQUE);
```
### Extending named colors
To add more CSS named colors:
```php
// 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](https://www.10quality.com/)