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

v3.0.3 2025-11-02 10:10 UTC

This package is auto-updated.

Last update: 2025-11-02 10:10:34 UTC


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

  • $string is a string representing the CSS color.
$color = Color::createFromString($string);

Color Creation

Create From A98 RGB

  • $red is a number between 0 and 1, and will default to 0.
  • $green is a number between 0 and 1, and will default to 0.
  • $blue is a number between 0 and 1, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromA98Rgb($red, $green, $blue, $alpha);

Create From Display P3

  • $red is a number between 0 and 1, and will default to 0.
  • $green is a number between 0 and 1, and will default to 0.
  • $blue is a number between 0 and 1, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromDisplayP3($red, $green, $blue, $alpha);

Create From Display P3 Linear

  • $red is a number between 0 and 1, and will default to 0.
  • $green is a number between 0 and 1, and will default to 0.
  • $blue is a number between 0 and 1, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromDisplayP3Linear($red, $green, $blue, $alpha);

Create From HSL

  • $hue is a number between 0 and 360, and will default to 0.
  • $saturation is a number between 0 and 100, and will default to 0.
  • $lightness is a number between 0 and 100, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromHsl($hue, $saturation, $lightness, $alpha);

Create From HWB

  • $hue is a number between 0 and 360, and will default to 0.
  • $whiteness is a number between 0 and 100, and will default to 0.
  • $blackness is a number between 0 and 100, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromHwb($hue, $whiteness, $blackness, $alpha);

Create From Lab

  • $lightness is a number between 0 and 100, and will default to 0.
  • $a is a number between -128 and 127, and will default to 0.
  • $b is a number between -128 and 127, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromLab($lightness, $a, $b, $alpha);

Create From Lch

  • $lightness is a number between 0 and 100, and will default to 0.
  • $chroma is a number between 0 and 230, and will default to 0.
  • $hue is a number between 0 and 360, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromLch($lightness, $chroma, $hue, $alpha);

Create From OkLab

  • $lightness is a number between 0 and 1, and will default to 0.
  • $a is a number between -0.4 and 0.4, and will default to 0.
  • $b is a number between -0.4 and 0.4, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromOkLab($lightness, $a, $b, $alpha);

Create From OkLch

  • $lightness is a number between 0 and 1, and will default to 0.
  • $chroma is a number between 0 and 0.4, and will default to 0.
  • $hue is a number between 0 and 360, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromOkLch($lightness, $chroma, $hue, $alpha);

Create From ProPhoto Rgb

  • $red is a number between 0 and 1, and will default to 0.
  • $green is a number between 0 and 1, and will default to 0.
  • $blue is a number between 0 and 1, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromProPhotoRgb($red, $green, $blue, $alpha);

Create From Rec 2020

  • $red is a number between 0 and 1, and will default to 0.
  • $green is a number between 0 and 1, and will default to 0.
  • $blue is a number between 0 and 1, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromRec2020($red, $green, $blue, $alpha);

Create From Rgb

  • $red is a number between 0 and 255, and will default to 0.
  • $green is a number between 0 and 255, and will default to 0.
  • $blue is a number between 0 and 255, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromRgb($red, $green, $blue, $alpha);

Create From Srgb

  • $red is a number between 0 and 1, and will default to 0.
  • $green is a number between 0 and 1, and will default to 0.
  • $blue is a number between 0 and 1, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromSrgb($red, $green, $blue, $alpha);

Create From Srgb Linear

  • $red is a number between 0 and 1, and will default to 0.
  • $green is a number between 0 and 1, and will default to 0.
  • $blue is a number between 0 and 1, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromSrgbLinear($red, $green, $blue, $alpha);

Create From Xyz D50

  • $x is a number between 0 and 1, and will default to 0.
  • $y is a number between 0 and 1, and will default to 0.
  • $z is a number between 0 and 1, and will default to 0.
  • $alpha is a number between 0 and 1, and will default to 1.
$color = Color::createFromXyzD50($x, $y, $z, $alpha);

Create From Xyz D65

  • $x is a number between 0 and 1, and will default to 0.
  • $y is a number between 0 and 1, and will default to 0.
  • $z is a number between 0 and 1, and will default to 0.
  • $alpha is 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.

  • $other is 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.

  • $space is 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.

  • $alpha is a boolean indicating whether to include the alpha component in the string, and will default to null.
  • $precision is 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.

  • $alpha is 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.

  • $blue is a number between 0 and 1.
$newColor = $color->withBlue($blue);

With Green

Clone the Color with a new green value.

  • $green is a number between 0 and 1.
$newColor = $color->withGreen($green);

With Red

Clone the Color with a new red value.

  • $red is 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.

  • $blue is a number between 0 and 1.
$newColor = $color->withBlue($blue);

With Green

Clone the Color with a new green value.

  • $green is a number between 0 and 1.
$newColor = $color->withGreen($green);

With Red

Clone the Color with a new red value.

  • $red is 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.

  • $blue is a number between 0 and 1.
$newColor = $color->withBlue($blue);

With Green

Clone the Color with a new green value.

  • $green is a number between 0 and 1.
$newColor = $color->withGreen($green);

With Red

Clone the Color with a new red value.

  • $red is 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.

  • $alpha is a boolean indicating whether to include the alpha component in the string, and will default to null.
  • $precision is a number representing the decimal precision, and will default to 2 (unused).
  • $shortenHex is a boolean indicating whether shorten hex output, and will default to true.
  • $name is 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.

  • $blue is a number between 0 and 255.
$newColor = $color->withBlue($blue);

With Green

Clone the Color with a new green value.

  • $green is a number between 0 and 255.
$newColor = $color->withGreen($green);

With Red

Clone the Color with a new red value.

  • $red is 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.

  • $hue is a number between 0 and 360.
$newColor = $color->withHue($hue);

With Lightness

Clone the Color with a new lightness value.

  • $lightness is a number between 0 and 100.
$newColor = $color->withLightness($lightness);

With Saturation

Clone the Color with a new saturation value.

  • $saturation is 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.

  • $blackness is a number between 0 and 100.
$newColor = $color->withBlackness($blackness);

With Hue

Clone the Color with a new hue value.

  • $hue is a number between 0 and 360.
$newColor = $color->withHue($hue);

With Whiteness

Clone the Color with a new whiteness value.

  • $whiteness is 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.

  • $a is a number between -128 and 127.
$newColor = $color->withA($a);

With B

  • $b is a number between -128 and 127.
$newColor = $color->withB($b);

With Lightness

Clone the Color with a new lightness value.

  • $lightness is 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.

  • $chroma is a number between 0 and 230.
$newColor = $color->withChroma($chroma);

With Hue

Clone the Color with a new hue value.

  • $hue is a number between 0 and 360.
$newColor = $color->withHue($hue);

With Lightness

Clone the Color with a new lightness value.

  • $lightness is 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.

  • $a is a number between -0.4 and 0.4.
$newColor = $color->withA($a);

With B

  • $b is a number between -0.4 and 0.4.
$newColor = $color->withB($b);

With Lightness

Clone the Color with a new lightness value.

  • $lightness is 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.

  • $chroma is a number between 0 and 0.4.
$newColor = $color->withChroma($chroma);

With Hue

Clone the Color with a new hue value.

  • $hue is a number between 0 and 360.
$newColor = $color->withHue($hue);

With Lightness

Clone the Color with a new lightness value.

  • $lightness is 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.

  • $blue is a number between 0 and 1.
$newColor = $color->withBlue($blue);

With Green

Clone the Color with a new green value.

  • $green is a number between 0 and 1.
$newColor = $color->withGreen($green);

With Red

Clone the Color with a new red value.

  • $red is 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.

  • $blue is a number between 0 and 1.
$newColor = $color->withBlue($blue);

With Green

Clone the Color with a new green value.

  • $green is a number between 0 and 1.
$newColor = $color->withGreen($green);

With Red

Clone the Color with a new red value.

  • $red is 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.

  • $alpha is a boolean indicating whether to include the alpha component in the string, and will default to null.
  • $precision is a number representing the decimal precision, and will default to 2.
  • $name is 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.

  • $blue is a number between 0 and 255.
$newColor = $color->withBlue($blue);

With Green

Clone the Color with a new green value.

  • $green is a number between 0 and 255.
$newColor = $color->withGreen($green);

With Red

Clone the Color with a new red value.

  • $red is 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.

  • $blue is a number between 0 and 1.
$newColor = $color->withBlue($blue);

With Green

Clone the Color with a new green value.

  • $green is a number between 0 and 1.
$newColor = $color->withGreen($green);

With Red

Clone the Color with a new red value.

  • $red is 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.

  • $blue is a number between 0 and 1.
$newColor = $color->withBlue($blue);

With Green

Clone the Color with a new green value.

  • $green is a number between 0 and 1.
$newColor = $color->withGreen($green);

With Red

Clone the Color with a new red value.

  • $red is 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.

  • $x is a number between 0 and 1.
$newColor = $color->withX($x);

With Y

Clone the Color with a new y value.

  • $y is a number between 0 and 1.
$newColor = $color->withY($y);

With Z

Clone the Color with a new z value.

  • $z is 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.

  • $x is a number between 0 and 1.
$newColor = $color->withX($x);

With Y

Clone the Color with a new y value.

  • $y is a number between 0 and 1.
$newColor = $color->withY($y);

With Z

Clone the Color with a new z value.

  • $z is a number between 0 and 1.
$newColor = $color->withZ($z);