FyreColor is a free, open-source immutable color manipulation library for PHP.
It is a modern library, and features full support for RGB, HSL, HSV, CMY and CMYK color-spaces.
- Installation
- Basic Usage
- Color Creation
- Color Formatting
- Color Attributes
- Color Manipulation
- Color Schemes
- Color Palettes
- Static Methods
Using Composer
composer require fyre/color
In PHP:
use Fyre\Color\Color;From RGB
$redis a number between 0 and 255.$greenis a number between 0 and 255.$blueis a number between 0 and 255.$alphais a number between 0 and 1, and will default to 1.
$color = new Color($red, $green, $blue, $alpha);From Brightness
$brightnessis a number between 0 and 100.$alphais a number between 0 and 1, and will default to 1.
$color = new Color($brightness, $alpha);From String
Create a new Color from a HTML color string.
$colorStringis a string containing a color value in either hexadecimal, RGB, RGBA, HSL, HSLA or a standard HTML color name.
$color = Color::fromString($colorString);From CMY
Create a new Color from CMY values.
$cyanis a number between 0 and 100.$magentais a number between 0 and 100.$yellowis a number between 0 and 100.$alphais a number between 0 and 1, and will default to 1.
$color = Color::fromCMY($cyan, $magenta, $yellow, $alpha);From CMYK
Create a new Color from CMYK values.
$cyanis a number between 0 and 100.$magentais a number between 0 and 100.$yellowis a number between 0 and 100.$keyis a number between 0 and 100.$alphais a number between 0 and 1, and will default to 1.
$color = Color::fromCMYK($cyan, $magenta, $yellow, $key, $alpha);From HSL
Create a new Color from HSL values.
$hueis a number between 0 and 360.$saturationis a number between 0 and 100.$lightnessis a number between 0 and 100.$alphais a number between 0 and 1, and will default to 1.
$color = Color::fromHSL($hue, $saturation, $lightness, $alpha);From HSV
Create a new Color from HSV values.
$hueis a number between 0 and 360.$saturationis a number between 0 and 100.$valueis a number between 0 and 100.$alphais a number between 0 and 1, and will default to 1.
$color = Color::fromHSV($hue, $saturation, $value, $alpha);To String
Return a HTML string representation of the color.
The $colorString returned will be a string containing either a HTML color name (if one exists), a hexadecimal string (if alpha is 1) or an RGBA string.
$colorString = $color->toString();To Hex String
Return a hexadecimal string representation of the color.
$hexString = $color->toHexString();To RGB String
Return a RGB/RGBA string representation of the color.
$rgbString = $color->toRGBString();To HSL String
Return a HSL/HSLA string representation of the color.
$hslString = $color->toHSLString();Label
Get the closest color name for the color.
$label = $color->label();Get Alpha
Get the alpha value of the color (between 0 and 1).
$alpha = $color->getAlpha();Get Brightness
Get the brightness value of the color (between 0 and 100).
$brightness = $color->getBrightness();Get Hue
Get the hue value of the color (between 0 and 360).
$hue = $color->getHue();Get Saturation
Get the saturation value of the color (between 0 and 100).
$saturation = $color->getSaturation();Luma
Get the relative luminance value of the color (between 0 and 1).
$luma = $color->luma();Set Alpha
Set the alpha value of the color.
$alphais a number between 0 and 1.
$newColor = $color->setAlpha($alpha);Set Brightness
Set the brightness value of the color.
$brightnessis a number between 0 and 100.
$newColor = $color->setBrightness($brightness);Set Hue
Set the hue value of the color.
$hueis a number between 0 and 360.
$newColor = $color->setHue($hue);Set Saturation
Set the saturation value of the color.
$saturationis a number between 0 and 100.
$newColor = $color->setSaturation($saturation);Darken
Darken the color by a specified amount.
$amountis a number between 0 and 1.
$newColor = $color->darken($amount);Invert
Invert the color.
$newColor = $color->invert();Lighten
Lighten the color by a specified amount.
$amountis a number between 0 and 1.
$newColor = $color->lighten($amount);Shade
Shade the color by a specified amount.
$amountis a number between 0 and 1.
$newColor = $color->shade($amount);Tint
Tint the color by a specified amount.
$amountis a number between 0 and 1.
$newColor = $color->tint($amount);Tone
Tone the color by a specified amount.
$amountis a number between 0 and 1.
$newColor = $color->tone($amount);Complementary
Create a complementary color variation.
$complementary = $color->complementary();Split
Create an array with 2 split color variations.
[$secondary, $accent] = $color->split();Analogous
Create an array with 2 analogous color variations.
[$secondary, $accent] = $color->analogous();Triadic
Create an array with 2 triadic color variations.
[$secondary, $accent] = $color->triadic();Tetradic
Create an array with 3 tetradic color variations.
[$secondary, $alternate, $accent] = $color->tetradic();Create a palette of colors from a Color object you have created using the following methods.
Shades
Create an array with a specified number of shade variations.
$shadesis a number indicating how many shades you wish to generate, and will default to 10.
$colorShades = $color->shades($shades);Tints
Create an array with a specified number of tint variations.
$tintsis a number indicating how many tints you wish to generate, and will default to 10.
$colorTints = $color->tints($tints);Tones
Create an array with a specified number of tone variations.
$tonesis a number indicating how many tones you wish to generate, and will default to 10.
$colorTones = $color->tones($tones);Palette
Create a palette object with a specified number of shades, tints and tone variations.
$shadesis a number indicating how many shades you wish to generate, and will default to 10.$tintsis a number indicating how many tints you wish to generate, and will default to 10.$tonesis a number indicating how many tones you wish to generate, and will default to 10.
$colorPalette = $color->palette($shades, $tints, $tones);Contrast
Calculate the contrast between two colors (between 1 and 21).
$color1is a Color object.$color2is a Color object.
$contrast = Color::contrast($color1, $color2);Distance
Calculate the distance between two colors.
$color1is a Color object.$color2is a Color object.
$distance = Color::dist($color1, $color2);Find Contrast
Find an optimally contrasting color for another color.
$color1is a Color object.$color2is a Color object, and will default to null.$minContrastis a number between 1 and 21 indicating the minimum valid contrast, and will default to 4.5.$stepSizeis a number between 0 and 1 indicating the amount to darken/lighten the color on each iteration, and will default to 0.01.
$contrastColor = Color::findContrast($color1, $color2, $minContrast, $stepSize);If $color2 value is null, $color1 will be used instead.
This method will tint/shade $color2 until it meets a minimum contrast threshold with $color1, then the new color will be returned. If no valid contrast value can be found, this method will return null instead.
Mix
Create a new Color by mixing two colors together by a specified amount.
$color1is a Color object.$color2is a Color object.$amountis a number between 0 and 1.
$mixed = Color::mix($color1, $color2, $amount);Multiply
Create a new Color by multiplying two colors together by a specified amount.
$color1is a Color object.$color2is a Color object.$amountis a number between 0 and 1.
$multiplied = Color::multiply($color1, $color2, $amount);