Download the PHP package aristath/ari-color without Composer
On this page you can find all versions of the php package aristath/ari-color. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aristath/ari-color
More information about aristath/ari-color
Files in aristath/ari-color
Package ari-color
Short Description A PHP library for color manipulation in themes and plugins
License GPL-2.0-or-later
Homepage http://aristath.github.io/ariColor
Informations about the package ari-color
ariColor
A PHP library for color manipulation in themes and plugins
ariColor
is a PHP library that will hopefully help WordPress theme developers do their job easier and more effectively.
Installation
It does not provide you with methods like lighten()
, darken()
etc. Instead, what it does is give you the ability to create these yourself with extreme ease by giving you all the properties of a color at hand, and allowing you to manipulate them however you see fit.
Example:
First, let's create our color object:
If you don't like using that method you can write your own proxy function:
Notice that we used auto
as the mode. If you use auto
or completely omit the 2nd argument, ariColor
will auto-detect it for you. You can use rgb
, rgba
, hsl
, hsla
, or even arrays as colors.
Then you can use it like this:
Say you want to get the values for red, green, blue:
Or you want to get the hue, saturation, lightness or even luminance of your color:
Scenario 1:
You have an option where users can define the background color for their <body>
. In order to make sure the text is always readable, you can either give them a 2nd option to set the text color, or auto-calculate it for readability.
Example function that given a background color decides if we're going to use white/black text color:
Usage:
Easy, right? What we did above is simply check the luminance of the background color, and then if the luminance is greater than 127 we return black, otherwise we return white.
Scenario 2:
We have a HEX color, and we want to get the same color as rgba, with an opacity of 0.7
:
or you could write it shorter like this:
or the same thing like this:
The choice is yours and you can manipulate colors in any way you want.
Properies list:
mode
(string: hex/rgb/rgba/hsl/hsla)red
(red value,integer
, range: 0-255)green
(green value,integer
, range: 0-255)blue
(blue value,integer
, range: 0-255)alpha
(alpha/opacity value,float
, range 0-1)hue
(color hue,integer
, range 0-360)saturation
(color saturation,integer
, range 0-100)lightness
(color lightness,integer
, range 0-100)luminance
(color luminance,integer
, range 0-255)hex
(the hex value of the current color)
Methods:
newColor
getNew
toCSS
newColor
Used to create a new object. Example:
The newColor
method has 2 arguments:
$color
: can accept any color value (see below for examples)$mode
: the color mode. If undefined will be auto-detected.
Some example of acceptable formats for the color used in the 1st argument on the method:
And more! This way you can use the saved values from all known frameworks.
getNew
Used if we want to create a new object identical to the one we already have, but changing one of its properties.
The getNew
method has 2 arguments:
$property
: can accept any of the properties listed above$value
: the new value of the property.
Example 1: Darken a color by 10%
Or you could write the above simpler like this by combining 2 steps:
Example 2: Remove any traces of green from an HSL color
toCSS
Returns a CSS-formatted color value.
The toCSS
has a single argument:
$mode
: can accept any of the values listed below (defaults tohex
if undefined)
hex
rgb
rgba
hsl
hsla
Example:
Color sanitization:
All colors are sanitized inside the class so you could easily write a proxy function that will always return a sanitized color like this:
You can even use a function like this one as a sanitize_callback
in a customizer control :)