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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package couleur

🎨 Couleur: A modern PHP 8.1+ color library

Software License PHP Version Lines of Code Release date Last commit on main branch Latest Version on Packagist

👋 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:

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 the HexRgb 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 the HexRgb 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 the Css 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:

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:

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:

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 a Css color, you first have to convert it into another color space. In the same way, if you convert a color with transparency into its Css equivalent, it will lose the transparency.

Hexadecimal RGB

HSL

HSV

HWB

Lab

Lch

Linear RGB

Linear P3

Linear ProPhoto

OkLab

OkLch

P3

ProPhoto

RGB

XYZ-D50

XYZ-D65

↑ 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


All versions of couleur with dependencies

PHP Build Version
Package Version
Requires php Version ~8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package matthieumastadenis/couleur contains the following files

Loading the files please wait ....