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/ */
tomloprod / colority example snippets
/**
* RGB format - Commas, whitespace or any combination accepted
*
* @var RgbColor $rgbColor
*/
$rgbColor = colority()->fromRgb('rgb(255,255,255)');
$rgbColor = colority()->fromRgb('rgb(255, 255, 255)');
$rgbColor = colority()->fromRgb('rgb(255 255 255)');
$rgbColor = colority()->fromRgb('255,255,255');
$rgbColor = colority()->fromRgb('255 255 255');
$rgbColor = colority()->fromRgb([255, 255, 255]);
/**
* Hexadecimal format - With or without hash, shorthand supported,
* and array of three RGB byte values [R, G, B]
*
* @var HexColor $hexColor
*/
$hexColor = colority()->fromHex('#51B389');
$hexColor = colority()->fromHex('51B389');
$hexColor = colority()->fromHex('#ABC');
$hexColor = colority()->fromHex([81, 179, 137]);
/**
* HSL format - Commas, whitespace, with or without deg/% units
*
* @var HslColor $hslColor
*/
$hslColor = colority()->fromHsl('hsl(168.31deg, 49.58%, 46.67%)');
$hslColor = colority()->fromHsl('hsl(168.31 49.58% 46.67%)');
$hslColor = colority()->fromHsl('168.31, 49.58, 46.67');
$hslColor = colority()->fromHsl('168.31 49.58 46.67');
$hslColor = colority()->fromHsl([168.31, 49.58, 46.67]);
/**
* OKLCH format - Commas, whitespace, percentage form for L and C,
* angle units (deg, rad, turn, grad) for H
*
* @var OklchColor $oklchColor
*/
$oklchColor = colority()->fromOklch('oklch(0.70 0.11 163)');
$oklchColor = colority()->fromOklch('oklch(0.70, 0.11, 163)');
$oklchColor = colority()->fromOklch('oklch(70% 27.5% 163deg)');
$oklchColor = colority()->fromOklch('oklch(0.70 0.11 0.45turn)');
$oklchColor = colority()->fromOklch('0.70 0.11 163');
$oklchColor = colority()->fromOklch([0.70, 0.11, 163]);
$color = colority()->parse(
'linear-gradient(135deg, rgb(17, 153, 142) 0%, rgb(56, 239, 125) 100%)'
);
// rgb(17,153,142) first stop wins on a 50/50 split
$color = colority()->parse(
'linear-gradient(rgb(255,0,0) 0%, rgb(0,0,255) 30%, rgb(0,255,0) 100%)'
);
// rgb(0,0,255) blue dominates the 15%-65% range (50 units of width)
$hexColor = colority()->fromHex('#51B389');
/**
* Automatic choice: Returns black or white, whichever has better contrast
*
* @var HexColor $bestForegroundHexColor
*/
$bestForegroundHexColor = $hexColor->getBestForegroundColor();
/**
* Custom palette: Chooses the best option from your brand colors
*
* @var HexColor $bestForegroundHexColor
*/
$bestForegroundHexColor = $hexColor->getBestForegroundColor([
new HexColor('#A63F3F'),
new HexColor('#3FA684'),
new HexColor('#6E3FA6'),
]);
$hexColor = colority()->fromHex('#51B389');
/**
* Contrast ratio with black (default)
*
* @var float $contrastRatio
*/
$contrastRatio = $hexColor->getContrastRatio();
/**
* Contrast ratio with a specific foreground color
*
* @var float $contrastRatio
*/
$contrastRatio = $hexColor->getContrastRatio(new HexColor('#3FA684'));
use Tomloprod\Colority\Support\Algorithms\ContrastRatioScore;
$backgroundColor = colority()->fromHex('#336699');
// AAA level by default
$foregroundColor = $backgroundColor->getMatchingForegroundColor();
// AA level for relaxed contrast atioScore::Good,
lightnessStep: 5 // the default value is 1
);
/** @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
);
use Tomloprod\Colority\Colors\HexColor;
use InvalidArgumentException;
$hexParser = HexColor::getParser();
try {
// This will throw InvalidArgumentException
$valueColor = $hexParser->parse('Not a valid value color');
} catch (InvalidArgumentException $e) {
// Handle invalid color format
}
// Normalize shorthand: Returns "#FFFFFF"
$valueColor = $hexParser->parse('#FFF');
use Tomloprod\Colority\Colors\{HexColor, HslColor, RgbColor, OklchColor};
$hexParser = HexColor::getParser(); // Validates Hex format
$hslParser = HslColor::getParser(); // Validates HSL format
$rgbParser = RgbColor::getParser(); // Validates RGB format
$oklchParser = OklchColor::getParser(); // Validates OKLCH format
// Defaults are available out of the box.
colority()->primary(); // HexColor #5B8DEF
colority()->success(); // HexColor #4BD396
colority()->danger(); // HexColor #F5707A
// More aggressive filtering (groups similar shades)
$dominantColors = colority()->getImageDominantColors(
imagePath: __DIR__.'/photo.png',
desiredNumColors: 5,
similarityThreshold: 80
);
/**
* Basic usage - generates a unique color from text
*
* @var HslColor $hslColor
*/
$hslColor = colority()->textToColor("Hi, I'm Tomás");
/**
* Only grayscale colors will be generated
*
* @var HslColor $hslColor
*/
$hslColor = colority()->textToColor(
"Hi, I'm Tomás",
colority()->fromHex('#CCC')
);
/**
* Color will fall somewhere between light green and dark teal
*
* @var HslColor $hslColor
*/
$hslColor = colority()->textToColor(
"Hi, I'm Tomás",
colority()->fromHex('#85d5a4'), // Start: Light green
colority()->fromHex('#165a59') // End: Dark teal
);
$hexColor = colority()->fromHex('#51B389');
/**
* Get a random similar color (default variation ranges)
*
* @var HexColor $similarHexColor
*/
$similarHexColor = colority()->getSimilarColor($hexColor);
// Fine-tune the similarity with custom ranges
$similarHexColor = colority()->getSimilarColor(
$hexColor,
hueRange: 20, // +- 20 degrees on color wheel
saturationRange: 5, // +- 5% saturation
lightnessRange: 5 // +- 5% lightness
);
/**
* Returns a random HSL color
*
* @var HslColor $randomColor
*/
$randomColor = colority()->random();
// And, if you want, convert to your preferred format
$hexRandom = $randomColor->toHex();
/**
* Create a 10-step gradient from red -> green -> blue
*
* @var array<HexColor> $gradient
*/
$gradient = colority()->gradient(
colors: [
colority()->fromHex('#ff0000'), // Red
colority()->fromHex('#00ff00'), // Green
colority()->fromHex('#0000ff') // Blue
],
steps: 10
);
use Tomloprod\Colority\Colors\HexColor;
/**
* Using the helper function
*
* @var HexColor $hexColor
*/
$hexColor = colority()->fromHex('#CCC');
use Tomloprod\Colority\Support\Facades\Colority;
use Tomloprod\Colority\Colors\HexColor;
/**
* Using the facade directly
*
* @var HexColor $hexColor
*/
$hexColor = Colority::fromHex('#CCC');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.