invert hex color code
composer require villfa/invert-colorHere a basic example:
<?php
require_once 'vendor/autoload.php';
use InvertColor\Color;
use InvertColor\Exceptions\InvalidColorFormatException;
try {
echo Color::fromHex('#fff')->invert(); // #000000
} catch (InvalidColorFormatException $e) {
echo 'Invalid color: ', $e->getValue();
}$color:stringColor in HEX string. Accepted formats: #000000, #000, 000000, 000
InvertColor\Color::fromHex('#fff')->invert(); // #000000$rgb:arrayColor as an array of RGB values.
InvertColor\Color::fromRGB([0, 255, 0])->invert(); // #ff00ff$bw:boolOptional. A boolean value indicating whether the output should be amplified to black (#000000) or white (#ffffff), according to the luminance of the original color.
InvertColor\Color::fromHex('#000')->invert(); // #ffffff
InvertColor\Color::fromHex('#282b35')->invert(); // #d7d4ca
// amplify to black or white
InvertColor\Color::fromHex('282b35')->invert(true); // #ffffff$bw:boolOptional. A boolean value indicating whether the output should be amplified to black or white, according to the luminance of the original color.
InvertColor\Color::fromHex('#000')->invertAsRGB(); // [255, 255, 255]
InvertColor\Color::fromHex('#282b35')->invertAsRGB(); // [215, 212, 202]
// amplify to black or white
InvertColor\Color::fromHex('282b35')->invertAsRGB(true); // [255, 255, 255]$bw:boolOptional. A boolean value indicating whether the output should be amplified to black or white, according to the luminance of the original color.
InvertColor\Color::fromHex('#000')->invertAsObj()->getHex(); // #ffffff
InvertColor\Color::fromHex('#282b35')->invertAsObj()->getRGB(); // [215, 212, 202]
// amplify to black or white
InvertColor\Color::fromHex('282b35')->invertAsObj(true)->getRGB(); // [255, 255, 255]InvertColor\Color::fromHex('#FFF')->getHex(); // #ffffffInvertColor\Color::fromHex('#fff')->getRGB(); // [255, 255, 255]InvertColor\Color::fromHex('#fff')->getLuminance(); // 1
InvertColor\Color::fromHex('#000')->getLuminance(); // 0InvertColor\Color::fromHex('#fff')->isBright(); // true
InvertColor\Color::fromHex('#000')->isBright(); // falseInvertColor\Color::fromHex('#fff')->isDark(); // false
InvertColor\Color::fromHex('#000')->isDark(); // trueTo validate and test the library:
composer run-script testThis library is a port of the JS package invert-color.
More resources: