1. Go to this page and download the library: Download tomloprod/colority 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/ */
/** @var HexColor $hexColor */
$hexColor = colority()->fromHex('#51B389');
/** @var HexColor $bestForegroundHexColor (black or white) */
$bestForegroundHexColor = $hexColor->getBestForegroundColor();
/** @var HexColor $bestForegroundHexColor (#A63F3F, #3FA684 or #6E3FA6) */
$bestForegroundHexColor = $hexColor->getBestForegroundColor([
new HexColor('#A63F3F'),
new HexColor('#3FA684'),
new HexColor('#6E3FA6'),
]);
/** @var HexColor $hexColor */
$hexColor = colority()->fromHex('#51B389');
/** @var float $contrastRatio Contrast ratio with black as the foreground color. */
$contrastRatio = $hexColor->getContrastRatio();
/** @var float $contrastRatio Contrast ratio with #3FA684 as the foreground color. */
$contrastRatio = $hexColor->getContrastRatio(new HexColor('#3FA684'));
/** @var HexColor $hexColor */
$hexColor = colority()->fromHex('#51B389');
/**
* The `getContrastRatio` method can take a `Color` object as the foreground
* to calculate the contrast ratio against that color. Black is used by default.
*
* @var float $contrastRatio
*/
$contrastRatio = $hexColor->getContrastRatio();
/**
* AA Level for texts
*/
$passsesAALevelForLargeText = ContrastRatioScore::passesTextAALevel(
contrastRatio: $contrastRatio,
largeText: true
);
$passsesAALevelForNormalText = ContrastRatioScore::passesTextAALevel(
contrastRatio: $contrastRatio,
largeText: false
);
/**
* AAA Level for texts
*/
$passsesAAALevelForLargeText = ContrastRatioScore::passesTextAAALevel(
contrastRatio: $contrastRatio,
largeText: true
);
$passsesAAALevelForNormalText = ContrastRatioScore::passesTextAAALevel(
contrastRatio: $contrastRatio,
largeText: false
);
/**
* AA Level for Graphical Objects and User Interface Components
*/
$passsesAALevelForUI = ContrastRatioScore::passesUIAALevel(
$contrastRatio
);
/** @var ValueColorParser $hexParser */
$hexParser = HexColor::getParser();
// will throw InvalidArgumentException
$valueColor = $hexParser->parse('Not a valid value color');
// will return #FFFFFF
$valueColor = $hexParser->parse('#FFF');