Download the PHP package savvywombat/color without Composer
On this page you can find all versions of the php package savvywombat/color. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download savvywombat/color
More information about savvywombat/color
Files in savvywombat/color
Package color
Short Description Package for manipulating colors in multiple color spaces.
License MIT
Homepage https://github.com/SavvyWombat/color-php
Informations about the package color
SavvyWombat Color
A PHP package to help convert between and manipulate HSL and RGB colorspaces/types.
Main features:
- support for CSS Color Module Level 3 color specifications ( #323C46, rgb(50,60,70), hsl(210,16.7%,23.5%) );
- support for alpha transparency ( #323C4680, rgba(50,60,70,0.5), hsla(210,16.7%,23.5%,0.5) );
- modifiers to change red/green/blue or hue/saturation/lightness and alpha on any color type;
- able to extend with custom colorspaces/types which can then be converted and/or modified;
- able to extend with custom modifiers;
- immutable behaviour.
Installation
Usage
Creating a color
The following patterns are available by default to create a color:
Converting a color
Colors can be converted to any other registered color type:
Modifying a color
The following methods are available on any registered color type by default:
red($value)
green($value)
blue($value)
alpha($value)
hue($value)
saturation($value)
lightness($value)
All methods return a modified copy of the original object, leaving the original unmodified, and will accept the following argument formats:
Red, green, blue values are clamped in the range of 0..255. This means that adding 100 to a color which already has a red value of 200 will return a color with red set to 255.
Similarly, saturation and lightness are clamped in the range 0..100, while alpha is clamped between 0 and 1.
Hue is not clamped, as it is possible to rotate more than 360 in either direction (and 420° is equivalent to 60°). Because of this, the fractional pattern ('+1/2') should not be used as there is no minimum or maximum.
Extending
Custom color types
The package allows you to create your own color types. These must extend Color
, and implement the following methods from the ColorInterface
:
Registering color types
You will need to register the class to allow conversion to and from your new color:
You can replace existing color types with new ones.
You can get a list of registered color types via Color::registeredColors()
.
public static function fromString(string $colorSpec): ColorInterface
The fromString
accepts a color specification and tests it against the registered patterns and extracts the information.
Registering color specification patterns
To register a color specification pattern you must first register the color type it will apply to:
You register new specifications for any registered color type, including the default types. You can also override registered patterns so that they apply to different color types.
You can get a list of registered color specification via Color::registeredColorSpecs()
.
public function __toString(): string;
public static function fromRGB(Rgb $rgb): ColorInterface
This method is used to allow conversion from one color type to any another that has been registered. This means that you only have to specify how to convert to your new type from an RGB color.
public function toRGB(): Rgb
The abstract \SavvyWombat\Color\Color
already implements this method:
The red, green, blue and alpha properties are also defined on the abstract. You can either reimplement the method, or set these values in your color type constructor to make use of the default implementation:
It is recommended that you use the default implementation, as the red, green and blue properties are exposed in the public interface.
Custom color modifiers
Color modifiers are methods on color types that are registered and made available to all other color types.
Calling a modifier on a color type will convert it to the type that implements the registered method, and then convert the result back to the original type. To help with this implicit conversion, custom modifiers must return an object which extends Color
- ideally, it should be a copy of the same type the method is implemented on.
Color::adjustValue($originalValue, $newValue, $max = 0, $min = 0)
handles the various relative arguments (±50 or ±50%) to produce the new calculated value for the property being modified.
The modifier is also responsible for ensuring the new value is valid for the property being modified.
Support for color keywords
and will accept any of the CSS color keywords to create a Hex color.
If you wish to generate a color of a different type from a keyword, you can convert from the Hex color after creation:
License
The MIT License (MIT). Please see License File for more information.