fyre / color
A color library.
Installs: 21
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/fyre/color
Requires
- fyre/macro: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^12
README
FyreColor is a free, open-source immutable color manipulation library for PHP.
It is a modern library, and features full support for all CSS color-spaces.
Table Of Contents
Installation
Using Composer
composer require fyre/color
In PHP:
use Fyre\Color\Color;
Basic Usage
$stringis a string representing the CSS color.
$color = Color::createFromString($string);
Color Creation
Create From A98 RGB
$redis a number between 0 and 1, and will default to 0.$greenis a number between 0 and 1, and will default to 0.$blueis a number between 0 and 1, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromA98Rgb($red, $green, $blue, $alpha);
Create From Display P3
$redis a number between 0 and 1, and will default to 0.$greenis a number between 0 and 1, and will default to 0.$blueis a number between 0 and 1, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromDisplayP3($red, $green, $blue, $alpha);
Create From Display P3 Linear
$redis a number between 0 and 1, and will default to 0.$greenis a number between 0 and 1, and will default to 0.$blueis a number between 0 and 1, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromDisplayP3Linear($red, $green, $blue, $alpha);
Create From HSL
$hueis a number between 0 and 360, and will default to 0.$saturationis a number between 0 and 100, and will default to 0.$lightnessis a number between 0 and 100, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromHsl($hue, $saturation, $lightness, $alpha);
Create From HWB
$hueis a number between 0 and 360, and will default to 0.$whitenessis a number between 0 and 100, and will default to 0.$blacknessis a number between 0 and 100, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromHwb($hue, $whiteness, $blackness, $alpha);
Create From Lab
$lightnessis a number between 0 and 100, and will default to 0.$ais a number between -128 and 127, and will default to 0.$bis a number between -128 and 127, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromLab($lightness, $a, $b, $alpha);
Create From Lch
$lightnessis a number between 0 and 100, and will default to 0.$chromais a number between 0 and 230, and will default to 0.$hueis a number between 0 and 360, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromLch($lightness, $chroma, $hue, $alpha);
Create From OkLab
$lightnessis a number between 0 and 1, and will default to 0.$ais a number between -0.4 and 0.4, and will default to 0.$bis a number between -0.4 and 0.4, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromOkLab($lightness, $a, $b, $alpha);
Create From OkLch
$lightnessis a number between 0 and 1, and will default to 0.$chromais a number between 0 and 0.4, and will default to 0.$hueis a number between 0 and 360, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromOkLch($lightness, $chroma, $hue, $alpha);
Create From ProPhoto Rgb
$redis a number between 0 and 1, and will default to 0.$greenis a number between 0 and 1, and will default to 0.$blueis a number between 0 and 1, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromProPhotoRgb($red, $green, $blue, $alpha);
Create From Rec 2020
$redis a number between 0 and 1, and will default to 0.$greenis a number between 0 and 1, and will default to 0.$blueis a number between 0 and 1, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromRec2020($red, $green, $blue, $alpha);
Create From Rgb
$redis a number between 0 and 255, and will default to 0.$greenis a number between 0 and 255, and will default to 0.$blueis a number between 0 and 255, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromRgb($red, $green, $blue, $alpha);
Create From Srgb
$redis a number between 0 and 1, and will default to 0.$greenis a number between 0 and 1, and will default to 0.$blueis a number between 0 and 1, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromSrgb($red, $green, $blue, $alpha);
Create From Srgb Linear
$redis a number between 0 and 1, and will default to 0.$greenis a number between 0 and 1, and will default to 0.$blueis a number between 0 and 1, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromSrgbLinear($red, $green, $blue, $alpha);
Create From Xyz D50
$xis a number between 0 and 1, and will default to 0.$yis a number between 0 and 1, and will default to 0.$zis a number between 0 and 1, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromXyzD50($x, $y, $z, $alpha);
Create From Xyz D65
$xis a number between 0 and 1, and will default to 0.$yis a number between 0 and 1, and will default to 0.$zis a number between 0 and 1, and will default to 0.$alphais a number between 0 and 1, and will default to 1.
$color = Color::createFromXyzD65($x, $y, $z, $alpha);
Colors
Contrast
Calculate the contrast between this and another Color.
$otheris a Color.
$contrast = $color->contrast($other);
Get Alpha
Get the alpha value.
$alpha = $color->getAlpha();
Label
Find the closest HTML color name for this color (in current color space).
$label = $color->label();
Luma
Calculate the relative luminance value.
$luma = $color->luma();
Space
Get the current color space.
$space = $color->space();
To
Convert the Color to a named color space.
$spaceis a string representing the color space, and must be one of either "a98-rgb", "display-p3", "display-p3-linear", "hex", "hsl", "hwb", "lab", "lch", "oklab", "oklch", "prophoto-rgb", "rec2020", "rgb", "srgb", "srgb-linear", "xyz-d50" or "xyz-d65".
$newColor = $color->to($space);
To A98 RGB
Convert the Color to A98Rgb.
$newColor = $color->toA98Rgb();
To Array
Get the color components as an array.
$data = $color->toArray();
To Display P3
Convert the Color to DisplayP3.
$newColor = $color->toDisplayP3();
To Display P3 Linear
Convert the Color to DisplayP3Linear.
$newColor = $color->toDisplayP3Linear();
To Hex
Convert the Color to Hex.
$newColor = $color->toHex();
To HSL
Convert the Color to Hsl.
$newColor = $color->toHsl();
To HWB
Convert the Color to Hwb.
$newColor = $color->toHwb();
To Lab
Convert the Color to Lab.
$newColor = $color->toLab();
To Lch
Convert the Color to Lch.
$newColor = $color->toLch();
To OkLab
Convert the Color to OkLab.
$newColor = $color->toOkLab();
To OkLch
Convert the Color to OkLch.
$newColor = $color->toOkLch();
To ProPhoto Rgb
Convert the Color to ProPhotoRgb.
$newColor = $color->toProPhotoRgb();
To Rec 2020
Convert the Color to Rec2020.
$newColor = $color->toRec2020();
To Rgb
Convert the Color to Rgb.
$newColor = $color->toRgb();
To Srgb
Convert the Color to Srgb.
$newColor = $color->toSrgb();
To Srgb Linear
Convert the Color to SrgbLinear.
$newColor = $color->toSrgbLinear();
To String
Get the CSS color string.
$alphais a boolean indicating whether to include the alpha component in the string, and will default to null.$precisionis a number representing the decimal precision, and will default to 2.
$colorString = $color->toString($alpha, $precision);
To Xyz D50
Convert the Color to XyzD50.
$newColor = $color->toXyzD50();
To Xyz D65
Convert the Color to XyzD65.
$newColor = $color->toXyzD65();
With Alpha
Clone the Color with a new alpha value.
$alphais a number between 0 and 1.
$newColor = $color->withAlpha($alpha);
A98 RGB
use Fyre\Color\Colors\A98Rgb;
Get Blue
Get the blue value.
$blue = $color->getBlue();
Get Green
Get the green value.
$green = $color->getGreen();
Get Red
Get the red value.
$red = $color->getRed();
With Blue
Clone the Color with a new blue value.
$blueis a number between 0 and 1.
$newColor = $color->withBlue($blue);
With Green
Clone the Color with a new green value.
$greenis a number between 0 and 1.
$newColor = $color->withGreen($green);
With Red
Clone the Color with a new red value.
$redis a number between 0 and 1.
$newColor = $color->withRed($red);
Display P3
use Fyre\Color\Colors\DisplayP3;
Get Blue
Get the blue value.
$blue = $color->getBlue();
Get Green
Get the green value.
$green = $color->getGreen();
Get Red
Get the red value.
$red = $color->getRed();
With Blue
Clone the Color with a new blue value.
$blueis a number between 0 and 1.
$newColor = $color->withBlue($blue);
With Green
Clone the Color with a new green value.
$greenis a number between 0 and 1.
$newColor = $color->withGreen($green);
With Red
Clone the Color with a new red value.
$redis a number between 0 and 1.
$newColor = $color->withRed($red);
Display P3 Linear
use Fyre\Color\Colors\DisplayP3Linear;
Get Blue
Get the blue value.
$blue = $color->getBlue();
Get Green
Get the green value.
$green = $color->getGreen();
Get Red
Get the red value.
$red = $color->getRed();
With Blue
Clone the Color with a new blue value.
$blueis a number between 0 and 1.
$newColor = $color->withBlue($blue);
With Green
Clone the Color with a new green value.
$greenis a number between 0 and 1.
$newColor = $color->withGreen($green);
With Red
Clone the Color with a new red value.
$redis a number between 0 and 1.
$newColor = $color->withRed($red);
Hex
use Fyre\Color\Colors\Hex;
Get Blue
Get the blue value.
$blue = $color->getBlue();
Get Green
Get the green value.
$green = $color->getGreen();
Get Red
Get the red value.
$red = $color->getRed();
To String
Get the CSS color string.
$alphais a boolean indicating whether to include the alpha component in the string, and will default to null.$precisionis a number representing the decimal precision, and will default to 2 (unused).$shortenHexis a boolean indicating whether shorten hex output, and will default to true.$nameis a boolean indicating whether to use CSS color names, and will default to false.
$colorString = $color->toString($alpha, $precision, $shortenHex, $name);
With Blue
Clone the Color with a new blue value.
$blueis a number between 0 and 255.
$newColor = $color->withBlue($blue);
With Green
Clone the Color with a new green value.
$greenis a number between 0 and 255.
$newColor = $color->withGreen($green);
With Red
Clone the Color with a new red value.
$redis a number between 0 and 255.
$newColor = $color->withRed($red);
Hsl
use Fyre\Color\Colors\Hsl;
Get Hue
Get the hue value.
$hue = $color->getHue();
Get Lightness
Get the lightness value.
$lightness = $color->getLightness();
Get Saturation
Get the saturation value.
$saturation = $color->getSaturation();
With Hue
Clone the Color with a new hue value.
$hueis a number between 0 and 360.
$newColor = $color->withHue($hue);
With Lightness
Clone the Color with a new lightness value.
$lightnessis a number between 0 and 100.
$newColor = $color->withLightness($lightness);
With Saturation
Clone the Color with a new saturation value.
$saturationis a number between 0 and 100.
$newColor = $color->withSaturation($saturation);
Hwb
use Fyre\Color\Colors\Hwb;
Get Blackness
Get the blackness value.
$blackness = $color->getBlackness();
Get Hue
Get the hue value.
$hue = $color->getHue();
Get Whiteness
Get the whiteness value.
$whiteness = $color->getWhiteness();
With Blackness
Clone the Color with a new blackness value.
$blacknessis a number between 0 and 100.
$newColor = $color->withBlackness($blackness);
With Hue
Clone the Color with a new hue value.
$hueis a number between 0 and 360.
$newColor = $color->withHue($hue);
With Whiteness
Clone the Color with a new whiteness value.
$whitenessis a number between 0 and 100.
$newColor = $color->withWhiteness($whiteness);
Lab
use Fyre\Color\Colors\Lab;
Get A
Get the a value.
$a = $color->getA();
Get B
Get the b value.
$b = $color->getB();
Get Lightness
Get the lightness value.
$lightness = $color->getLightness();
With A
Clone the Color with a new a value.
$ais a number between -128 and 127.
$newColor = $color->withA($a);
With B
$bis a number between -128 and 127.
$newColor = $color->withB($b);
With Lightness
Clone the Color with a new lightness value.
$lightnessis a number between 0 and 100.
$newColor = $color->withLightness($lightness);
Lch
use Fyre\Color\Colors\Lch;
Get Chroma
Get the chroma value.
$chroma = $color->getChroma();
Get Hue
Get the hue value.
$hue = $color->getHue();
Get Lightness
Get the lightness value.
$lightness = $color->getLightness();
With Chroma
Clone the Color with a new chroma value.
$chromais a number between 0 and 230.
$newColor = $color->withChroma($chroma);
With Hue
Clone the Color with a new hue value.
$hueis a number between 0 and 360.
$newColor = $color->withHue($hue);
With Lightness
Clone the Color with a new lightness value.
$lightnessis a number between 0 and 100.
$newColor = $color->withLightness($lightness);
OkLab
use Fyre\Color\Colors\OkLab;
Get A
Get the a value.
$a = $color->getA();
Get B
Get the b value.
$b = $color->getB();
Get Lightness
Get the lightness value.
$lightness = $color->getLightness();
With A
Clone the Color with a new a value.
$ais a number between -0.4 and 0.4.
$newColor = $color->withA($a);
With B
$bis a number between -0.4 and 0.4.
$newColor = $color->withB($b);
With Lightness
Clone the Color with a new lightness value.
$lightnessis a number between 0 and 1.
$newColor = $color->withLightness($lightness);
OkLch
use Fyre\Color\Colors\OkLch;
Get Chroma
Get the chroma value.
$chroma = $color->getChroma();
Get Hue
Get the hue value.
$hue = $color->getHue();
Get Lightness
Get the lightness value.
$lightness = $color->getLightness();
With Chroma
Clone the Color with a new chroma value.
$chromais a number between 0 and 0.4.
$newColor = $color->withChroma($chroma);
With Hue
Clone the Color with a new hue value.
$hueis a number between 0 and 360.
$newColor = $color->withHue($hue);
With Lightness
Clone the Color with a new lightness value.
$lightnessis a number between 0 and 1.
$newColor = $color->withLightness($lightness);
ProPhoto Rgb
use Fyre\Color\Colors\ProPhotoRgb;
Get Blue
Get the blue value.
$blue = $color->getBlue();
Get Green
Get the green value.
$green = $color->getGreen();
Get Red
Get the red value.
$red = $color->getRed();
With Blue
Clone the Color with a new blue value.
$blueis a number between 0 and 1.
$newColor = $color->withBlue($blue);
With Green
Clone the Color with a new green value.
$greenis a number between 0 and 1.
$newColor = $color->withGreen($green);
With Red
Clone the Color with a new red value.
$redis a number between 0 and 1.
$newColor = $color->withRed($red);
Rec2020
use Fyre\Color\Colors\Rec2020;
Get Blue
Get the blue value.
$blue = $color->getBlue();
Get Green
Get the green value.
$green = $color->getGreen();
Get Red
Get the red value.
$red = $color->getRed();
With Blue
Clone the Color with a new blue value.
$blueis a number between 0 and 1.
$newColor = $color->withBlue($blue);
With Green
Clone the Color with a new green value.
$greenis a number between 0 and 1.
$newColor = $color->withGreen($green);
With Red
Clone the Color with a new red value.
$redis a number between 0 and 1.
$newColor = $color->withRed($red);
Rgb
use Fyre\Color\Colors\Rgb;
Get Blue
Get the blue value.
$blue = $color->getBlue();
Get Green
Get the green value.
$green = $color->getGreen();
Get Red
Get the red value.
$red = $color->getRed();
To String
Get the CSS color string.
$alphais a boolean indicating whether to include the alpha component in the string, and will default to null.$precisionis a number representing the decimal precision, and will default to 2.$nameis a boolean indicating whether to use CSS color names, and will default to false.
$colorString = $color->toString($alpha, $precision, $name);
With Blue
Clone the Color with a new blue value.
$blueis a number between 0 and 255.
$newColor = $color->withBlue($blue);
With Green
Clone the Color with a new green value.
$greenis a number between 0 and 255.
$newColor = $color->withGreen($green);
With Red
Clone the Color with a new red value.
$redis a number between 0 and 255.
$newColor = $color->withRed($red);
Srgb
use Fyre\Color\Colors\Srgb;
Get Blue
Get the blue value.
$blue = $color->getBlue();
Get Green
Get the green value.
$green = $color->getGreen();
Get Red
Get the red value.
$red = $color->getRed();
With Blue
Clone the Color with a new blue value.
$blueis a number between 0 and 1.
$newColor = $color->withBlue($blue);
With Green
Clone the Color with a new green value.
$greenis a number between 0 and 1.
$newColor = $color->withGreen($green);
With Red
Clone the Color with a new red value.
$redis a number between 0 and 1.
$newColor = $color->withRed($red);
Srgb Linear
use Fyre\Color\Colors\SrgbLinear;
Get Blue
Get the blue value.
$blue = $color->getBlue();
Get Green
Get the green value.
$green = $color->getGreen();
Get Red
Get the red value.
$red = $color->getRed();
With Blue
Clone the Color with a new blue value.
$blueis a number between 0 and 1.
$newColor = $color->withBlue($blue);
With Green
Clone the Color with a new green value.
$greenis a number between 0 and 1.
$newColor = $color->withGreen($green);
With Red
Clone the Color with a new red value.
$redis a number between 0 and 1.
$newColor = $color->withRed($red);
Xyz D50
use Fyre\Color\Colors\XyzD50;
Get X
Get the x value.
$x = $color->getX();
Get Y
Get the y value.
$y = $color->getY();
Get Z
Get the z value.
$z = $color->getZ();
With X
Clone the Color with a new x value.
$xis a number between 0 and 1.
$newColor = $color->withX($x);
With Y
Clone the Color with a new y value.
$yis a number between 0 and 1.
$newColor = $color->withY($y);
With Z
Clone the Color with a new z value.
$zis a number between 0 and 1.
$newColor = $color->withZ($z);
Xyz D65
use Fyre\Color\Colors\XyzD65;
Get X
Get the x value.
$x = $color->getX();
Get Y
Get the y value.
$y = $color->getY();
Get Z
Get the z value.
$z = $color->getZ();
With X
Clone the Color with a new x value.
$xis a number between 0 and 1.
$newColor = $color->withX($x);
With Y
Clone the Color with a new y value.
$yis a number between 0 and 1.
$newColor = $color->withY($y);
With Z
Clone the Color with a new z value.
$zis a number between 0 and 1.
$newColor = $color->withZ($z);