Download the PHP package tomloprod/colority without Composer
On this page you can find all versions of the php package tomloprod/colority. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tomloprod/colority
More information about tomloprod/colority
Files in tomloprod/colority
Package colority
Short Description Colority is a lightweight PHP library designed to handle color transformations, validations and manipulations with ease.
License MIT
Informations about the package colority
π¨ About Colority
Colority is a lightweight PHP library designed to handle color transformations, validations and manipulations with ease.
It allows you to instantiate concrete objects according to the color format (RGB, HSL, Hexadecimal) and convert from one format to another.
Additionally, it lets you check if a background color meets the WCAG 2.0 accessibility standard regarding the color contrast ratio in text and UI.
Furthermore, it includes multiple functions such as the following:
- It allows you to obtain the colors found in an image
- Generate the best foreground color (white, black, or from a user-provided list) for a background color, ensuring the best possible contrast ratio. (important for improving text visibility on, for example, colored badges).
- Generate a fixed color based on a string. Useful for generating a color associated with, for example, a username.
- Allows you to obtain a random color similar to a given color.
β¨ Getting Started
Instantiating Color objects
You can convert value colors (strings or, additionally, depending on the color type, arrays) to specific Color
objects.
If you cannot specify the original format of the value color, you can use the parse
method. This will detect what type of color it is and instantiate a new object or, if the received string does not match any type of color, it will return NULL
:
Contrast ratio (WCAG 2.0 standard)
When you have the Color
object, you will be able to use all its methods. Below, we describe two of them related to the contrast ratio.
getBestForegroundColor
Returns a Color
object with the most suitable foreground color (using the Luminosity Contrast Ratio algorithm).
You can pass an array with Color
objects as a parameter, so it chooses the foreground color with the best contrast ratio. If no parameter is specified, it will default to white or black.
getContrastRatio
Returns the contrast ratio (higher is better contrast, lower is worse) between the color invoking this method and the color passed as a parameter. If no color is passed as a parameter, the contrast ratio against black as foreground will be determined.
AA & AAA WCAG levels
Below we show you how to check if a contrast ratio meets WCAG AA and AAA levels using Tomloprod\Colority\Support\Algorithms\ContrastRatioScore
.
Color validation
The concrete Color
classes have a static method called getParser()
which returns an instance of ValueColorParser
.
The parse
method returns a string with the value color adapted to work correctly with Colority or throws an InvalidArgumentException
when it's not valid.
You can use the specific parser for any type of color:
Color conversion
Colority allows you to convert a Color object to any other Color
object of the desired format.
Color utilities
getImageColors
It allows you to obtain the colors detected in an image. As the first argument, the path to the image must be provided.
It has an optional second parameter (default is 5) that specifies the number of colors to be returned.
textToColor
Generate a fixed color based on a string.
π§ Advise Useful for generating a color associated with, for example, a username, mail address, etc, since a string will always return the same color.
getSimilarColor
Allows you to obtain a random color similar (in the same color palette) to a given color.
Ways of using Colority
You can use Colority either with the aliases colority()
or by directly invoking the static methods of the Colority
facade:
You decide how to use it π
𧱠Architecture
Colority is composed of several types of elements. Below are some features of each of these elements.
Colority
Tomloprod\Colority\Support\Facades\Colority
is a facade that acts as a simplified interface for using the rest of the Colority elements.
Methods
Color
All concrete color classes extend the abstract class Color
. Concrete color classes:
Tomloprod\Colority\Colors\HexColor
Tomloprod\Colority\Colors\HslColor
Tomloprod\Colority\Colors\RgbColor
Methods
For the HslColor
and RgbColor
objects, you also have a method getArrayValueColor
that will return the value color in array format:
On the other hand, the HslColor
object has an additional method called getValueColorWithMeasureUnits
, which returns the value color, but with units of measurement (useful for, for example, using it in a CSS style):
π Installation & Requirements
Requires PHP 8.2+
You may use Composer to install Colority into your PHP project:
π§βπ€βπ§ Contributing
Contributions are welcome, and are accepted via pull requests. Please review these guidelines before submitting any pull requests.
Colority was created by TomΓ‘s LΓ³pez and open-sourced under the MIT license.
All versions of colority with dependencies
ext-mbstring Version *