Download the PHP package matthieumastadenis/couleur without Composer
On this page you can find all versions of the php package matthieumastadenis/couleur. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download matthieumastadenis/couleur
More information about matthieumastadenis/couleur
Files in matthieumastadenis/couleur
Package couleur
Short Description A modern PHP 8.1+ color library, compatible with CSS Color Module Level 4
License MIT
Informations about the package couleur
🎨 Couleur: A modern PHP 8.1+ color library
- 🎨 Couleur: A modern PHP 8.1+ color library
- 👋 Presentation
- ⚙️ Installation
- 🏁 Quick Start
- 📚 Usage
- 🏭 Immutable Objects and the
ColorFactory
- Direct instanciation
- Using the
ColorFactory
- Using immutable color objects
- 🧰 Pure Functions
- Color Space Functions
- Conversion Functions
- Generic Functions
- 🛠️ Enums and Constants
- The
Constant
Enum - The
ColorSpace
Enum - The
CssColor
Enum
- The
- 🌈 Color Spaces
- CSS
- Hexadecimal RGB
- HSL
- HSV
- HWB
- Lab
- Lch
- Linear RGB
- Linear P3
- Linear ProPhoto
- OkLab
- OkLch
- P3
- ProPhoto
- RGB
- XYZ-D50
- XYZ-D65
- 🤝 Contributing
- 📜 License
- ❤️ Thanks
👋 Presentation
Couleur is a modern PHP 8.1+ color library, intended to be compatible with CSS Color Module Level 4, and inspired by Color.js from Lea Verou and Chris Lilley.
The main goal of this package is to allow color conversions between multiple, old and new many advantages for design purpose.
Couleur is made to be usable with an OOP approach as well as with a FP approach:
- If you prefer OOP, you can use 🏭 Immutable Objects and the
ColorFactory
; - If you prefer FP, you can directly use the multiple 🧰 Pure Functions ;
Warning: This package is currently under development.
The current version may include bugs, untested code, undocumented code, unfinished code, or simply code that will change. More specifically, for the moment there is a lack of unit tests, and a few color spaces as well as distance calculation functions and gammut correction remain to be implemented. All of these will come soon.
In the meantime, it is recommended to avoid using this package in production.
↑ Back to Top
⚙️ Installation
Use the following command to add Couleur to your project with Composer:
Don't forget to include the autoloader provided by Composer:
↑ Back to Top
🏁 Quick Start
The following is a quick tl;dr example of the simplest way to use Couleur, based on an OOP approach. For more detailed instructions, please read the 📚 Usage section.
↑ Back to Top
📚 Usage
🏭 Immutable Objects and the ColorFactory
Direct instanciation
Couleur provides one immutable class for each supported 🌈 Color Space. You can of course instantiate these classes manually:
Note : You may have noticed from the previous example that it implies passing correctly formatted values to each constructor.
For example, the
Rgb
class expects to receive opacity expressed in the same magnitude than red, green and blue values, meaning as a number between 0 and 255. Same thing for theHexRgb
class which expects only hexadecimal strings for each of the four parameters it takes (opacity included).Because of this, you may prefer to avoid instanciating these classes yourself. A simpler solution is to use the ColorFactory like in the following examples. It will automatically handle values conversion for you.
↑ Back to Top
Using the ColorFactory
The best and simplest way to create color objects is by using the ColorFactory
abstract class which provides a specific static method for each supported 🌈 Color Space:
Note that by default these methods are automatically guessing the input syntax. This means it's possible to provide an input value in a different format than the expected output, and the conversion will happen automatically:
If you use an incorrectly formated value, a UnknownColorSpace
Exception will be thrown:
Also if you use an incomplete value, a MissingColorValue
Exception will be thrown:
By using the $from
parameter, you can specify the input color space with a string alias or with the ColorSpace enum. Like before, the value will automatically be converted from it to the targetted space.
Specifying this is particularly helpful when you're using an array as input, to be sure it will not be treated as RGB (which is the default for an array of numbers):
You can alternatively use the new()
static method, which adds a $to
parameter just after the input value. If this parameter is not specified, the targetted color space will automatically be determined according to the format of the value:
↑ Back to Top
Using immutable color objects
Once you have a color instance, you can easily convert it to another color space using one of its dedicated to...()
methods, which will return a new object:
Note that any color can be converted to CSS with the toCss()
method. It will automatically pick the closest CSS color:
All color objects are directly stringable. They also provide a stringify()
method which offers more possibilities:
All color objects also have a coordinates()
method which returns an array:
You can also directly access readonly properties from each color object:
All color objects have a change()
method which always return a new object corresponding to a variant of the current color.
Note: The
change()
method of theHexRgb
class behave differently depending on the operation you ask for :
- For replacing a coordinate you have to provide an hexadecimal value ;
- For additions and substractions you have to provide an hexadecimal value ;
- For all other operactions you have to provide a decimal value ;
Please observe the detailed demonstration in the next example:
Note: The
change()
method of theCss
class also behave differently: it only accepts a stringable color name or an instances of the CssColor Enum, which replace the color without variations.Please observe the next example:
↑ Back to Top
🧰 Pure Functions
Objects in Couleur are all based on a collection of pure functions under the hood. These functions can be used directly if you don't want to use objects.
Note: Choosing this functional programming approach is better in terms of performance, but can be a bit more tedious because you have to manipulate arrays of values instead of objects.
There are three main types of functions provided by Couleur : dedicated Generic Functions:
Color Space Functions
Each supported 🌈 Color Space has its own dedicated functions, accessible under the namespace matthieumastadenis\couleur\utils\[space]
. Those are the same for each color space: clean()
, from()
, stringify()
and verify()
.
clean()
functions are made to transform an input value in a correctly formated set of values, according to the corresponding color space. They all return an array, except for css\clean()
which directly returns an instance of the the CssColor
Enum:
from()
functions convert and clean an input value from the specified color space (with the $from
parameter) to the color space corresponding to the used namespace. If no input color space is specified with the $from
parameter, it will be automatically guessed from the format of the $value
:
stringify()
functions return a color string fully compatible with CSS syntax. Depending of each color space, these functions can have the following parameters:
$sharp
: only forHexRgb
colors, this can be used to include or not the hexadecimal sharp character (#) ;$short
: only forHexRgb
colors, this can be used to force or prevent the shortening of the value (#f00 instead of #ff0000) ;$uppercase
: only forHexRgb
colors, this can be used to force the conversion to uppercase or lowercase (by default the case is preserved) ;$alpha
: can be used to force or prevent the inclusion of opacity (by default opacity is included only if it has a value other than 100%) ;$precision
: number of decimals used to round values (by defaut theCOULEUR_PRECISION
constant is used) ;$legacy
: if true the color string will be formatted according to the traditional CSS syntax rather than the modern one (rgba(255,0,0,1) instead of rgb(100% 0% 0% / 100%)) ;
verify()
functions simply return a boolean indicating if the input value matches the corresponding color space:
↑ Back to Top
Conversion Functions
Each supported 🌈 Color Space has a complete set of dedicated functions to convert into other color spaces. These are also accessible under the namespace matthieumastadenis\couleur\utils\[space]
:
↑ Back to Top
Generic Functions
Couleur also offers an ensemble of generic utilitary functions, all located under the namespace matthieumastadenis\couleur\utils
.
If the majority of these functions are mostly made for interal usages, a few can be useful to you if you prefer to use Couleur with a functional programming approcach. These are described below.
The constant()
function can be used to access and declare configuration constants direclty, without the need to use the Constant
Enum:
The findColorSpace()
function helps you to guess a 🌈 Color Space by interpreting a provided $value
.
If the function succeeds, it returns an instance of the ColorSpace
Enum.
In case of failure, the function will throw a UnknownColorSpace
by default, except if you set the $throw
parameter to false
or if you provide a $fallback
value.
The isColorString()
function returns a boolean indicating if the provided $value
is a valid CSS color string.
By default it is very tolerant and will return true
for any string corresponding to a valid CSS syntax, regardless of how you wrote the function name (meaning something like 'myCustomRgb(255,0,0)' will be considered as valid).
If you want a more precise check, you can use the $spaces
parameter to provide either:
- an unique stringable value, like
'rgb'
; - an array of accepted values, like
[ 'rgb', 'rgba' ]
; - an instance of the
ColorSpace
Enum (all of its aliases will be accepted) ;
The parseColorValue()
function transforms a CSS color string into an array of values. If the provided $value
is not stringable, it will simply be returned as an array.
The $opacityFactor
parameter is useful to convert opacity into the correct range (for example converting 1 to 100 or 255).
Note : This function does not clean values inside of the array. For a typical usage, you may want to pass its result into the corresponding
clean()
function (see the Color Space Functions section for more details).
The to()
function is the highest-level function used to convert any color value to any color space.
In case of success, its result will always be an array.
Its $to
and $from
parameters correspond respectively to the output and input color spaces, and accept either an instance of 🌈 Color Spaces section).
If these parameters are null, they will be guessed by interpreting the format of $value
(using the findColorSpace()
function).
↑ Back to Top
🛠️ Enums and Constants
The Constant
Enum
Couleur can be preconfigured with dedicated constants. These act as default values used by multiple functions when the corresponding parameter is missing or set to null. All constants are written in uppercase and prefixed with COULEUR_
.
Currently, the following constants are used:
COULEUR_LEGACY
(default0
): if set to1
, stringified colors will use the legacy CSS syntax by default ;COULEUR_PRECISION
(default9
): the default rounding precision for color values when stringified ;
You can use the Constant
enum to easily access and manage these constants and their values:
↑ Back to Top
The ColorSpace
Enum
This enum is the simplest way to access all color spaces supported by Couleur:
You can access a ColorSpace
instance by the corresponding color class
:
Each ColorSpace
is accessible from multiple aliases with the fromAlias()
method. All aliases are case insensitive:
You can easily access dedicated functions from each ColorSpace
with the cleanCallback()
, fromCallback()
, stringifyCallback()
and verifyCallback()
methods:
↑ Back to Top
The CssColor
Enum
This enum helps managing CSS
named colors. With its multiple methods, you can easily know if a CssColor
exists and get the corresponding RGB
or HexRGB
coordinates from it:
The fromCss()
method allows you to get a specific CssColor
by its name. If no supported color matches the $name
parameter, a UnsupportedCssColor
Exception will be thrown by default, unless you provide a $fallback
or you set the $throw
parameter to false.
You can also find the CssColor
corresponding to precise RGB
or HexRGB
coordinates with fromRgb()
and fromHexRgb()
.
By default these functions will return the supported CSS color which is the closest to the provided coordinates, unless you set the $closest
parameter to false. In that case and if no supported color matches the exact coordinates you provided, a UnsupportedCssColor
Exception will be thrown by default, unless you provide a $fallback
or you set the $throw
parameter to false.
You can stringify a CssColor
with the toHexRgbString()
and toRgbString
methods:
You can also get a new Color
object from any CssColor
by using the toCss()
, toHexRgb()
or toRgb()
method:
↑ Back to Top
🌈 Color Spaces
Couleur currently supports the following color spaces and formats:
CSS
In Couleur, the Css
color space refers to the named colors according to the CSS specification. Because they are a predefined and standardized list of exact colors, they all can be accessed easily with the CssColor Enum.
Note :
Css
colors cannot have an opacity value. If you want to apply transparency to aCss
color, you first have to convert it into another color space. In the same way, if you convert a color with transparency into itsCss
equivalent, it will lose the transparency.
- ColorSpace enum case :
ColorSpace::Css
; - Color class :
matthieumastadenis\couleur\colors\Css
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\css
; - Accepted aliases :
css
,html
,web
;
Hexadecimal RGB
- ColorSpace case:
ColorSpace::HexRgb
; - Color class :
matthieumastadenis\couleur\colors\HexRgb
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\hexRgb
; - Accepted aliases :
hex
,hexrgb
,hex-rgb
,hex_rgb
,hexadecimal
; - Coordinates :
red
,green
,blue
;
HSL
- ColorSpace case:
ColorSpace::Hsl
; - Color class :
matthieumastadenis\couleur\colors\Hsl
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\hsl
; - Accepted aliases :
hsl
,hsla
; - Coordinates :
hue
,saturation
,lightness
;
HSV
- ColorSpace case:
ColorSpace::Hsv
; - Color class :
matthieumastadenis\couleur\colors\Hsv
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\hsv
; - Accepted aliases :
hsv
,hsb
; - Coordinates :
hue
,saturation
,value
;
HWB
- ColorSpace case:
ColorSpace::Hwb
; - Color class :
matthieumastadenis\couleur\colors\Hwb
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\hwb
; - Accepted aliases :
hwb
; - Coordinates :
hue
,whiteness
,blackness
;
Lab
- ColorSpace case:
ColorSpace::Lab
; - Color class :
matthieumastadenis\couleur\colors\Lab
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\lab
; - Accepted aliases :
lab
,cielab
,cie-lab
,cie_lab
; - Coordinates :
lightness
,b
,a
;
Lch
- ColorSpace case:
ColorSpace::Lch
; - Color class :
matthieumastadenis\couleur\colors\Lch
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\lch
; - Accepted aliases :
lch
,cielch
,cie-lch
,cie_lch
; - Coordinates :
lightness
,chroma
,hue
;
Linear RGB
- ColorSpace case:
ColorSpace::LinRgb
; - Color class :
matthieumastadenis\couleur\colors\LinRgb
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\linRgb
; - Accepted aliases :
srgb-linear
,linrgb
,linsrgb
,lin-rgb
,lin_rgb
,lin-srgb
,lin_srgb
; - Coordinates :
red
,green
,blue
;
Linear P3
- ColorSpace case:
ColorSpace::LinP3
; - Color class :
matthieumastadenis\couleur\colors\LinP3
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\linP3
; - Accepted aliases :
p3-linear
,p3_linear
,linp3
,lin-p3
,lin_p3
; - Coordinates :
red
,green
,blue
;
Linear ProPhoto
- ColorSpace case:
ColorSpace::LinProPhoto
; - Color class :
matthieumastadenis\couleur\colors\LinProPhoto
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\linProPhoto
; - Accepted aliases :
prophoto-linear
,prophoto_linear
,linprophoto
,lin-prophoto
,lin_prophoto
; - Coordinates :
red
,green
,blue
;
OkLab
- ColorSpace case:
ColorSpace::OkLab
; - Color class :
matthieumastadenis\couleur\colors\OkLab
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\okLab
; - Accepted aliases :
oklab
,ok-lab
,ok_lab
; - Coordinates :
lightness
,a
,b
;
OkLch
- ColorSpace case:
ColorSpace::OkLch
; - Color class :
matthieumastadenis\couleur\colors\OkLch
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\okLch
; - Accepted aliases :
oklch
,ok-lch
,ok_lch
; - Coordinates :
lightness
,chroma
,hue
;
P3
- ColorSpace case:
ColorSpace::LinP3
; - Color class :
matthieumastadenis\couleur\colors\LinP3
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\linP3
; - Accepted aliases :
display-p3
,display_p3
,p3
; - Coordinates :
red
,green
,blue
;
ProPhoto
- ColorSpace case:
ColorSpace::ProPhoto
; - Color class :
matthieumastadenis\couleur\colors\ProPhoto
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\proPhoto
; - Accepted aliases :
prophoto
,prophoto-rgb
,prophoto_rgb
; - Coordinates :
red
,green
,blue
;
RGB
- ColorSpace case:
ColorSpace::Rgb
; - Color class :
matthieumastadenis\couleur\colors\Rgb
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\rgb
; - Accepted aliases :
rgb
,rgba
,srgb
,s-rgb
,s_rgb
; - Coordinates :
red
,green
,blue
;
XYZ-D50
- ColorSpace case:
ColorSpace::XyzD50
; - Color class :
matthieumastadenis\couleur\colors\XyzD50
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\xyzD50
; - Accepted aliases :
xyz-d50
,xyz_d50
,xyzd50
; - Coordinates :
x
,y
,z
;
XYZ-D65
- ColorSpace case:
ColorSpace::XyzD65
; - Color class :
matthieumastadenis\couleur\colors\XyzD65
; - Dedicated functions namespace :
matthieumastadenis\couleur\utils\xyzD65
; - Accepted aliases :
xyz-d65
,xyz_d65
,xyzd65
,xyz
; - Coordinates :
x
,y
,z
;
↑ Back to Top
🤝 Contributing
You're welcome to contribute to this package by using issues and pull requests.
Before submitting any breaking change, please consider contacting me (either by directly submitting an issue, or by sending me a DM on Twitter if you really feel it's more appropriate).
↑ Back to Top
📜 License
Couleur is publicly shared under the MIT License. You can freely use it in any project. For more information, please read the included License File.
↑ Back to Top
❤️ Thanks
A huge thanks to Lea Verou and Chris Lilley for their incredible work and their precise articles on the subject. With a special thanks to Chris for the time and the answers he gave me during the implementation of this library.
↑ Back to Top