### Installation via Composer Source: https://github.com/spatie/color/blob/main/README.md Instructions for installing the Spatie Color library using Composer, the dependency manager for PHP. ```bash composer require spatie/color ``` -------------------------------- ### Running Tests Source: https://github.com/spatie/color/blob/main/README.md Command to execute the test suite for the Spatie Color package. ```bash $ composer test ``` -------------------------------- ### Spatie Color Interface Methods Source: https://github.com/spatie/color/blob/main/README.md Details the methods available in the `SpatieColorColor` interface, including parsing color strings and retrieving color channel values. ```APIDOC interface Spatie\Color\Color // Parses a color string and returns a Color implementation. // Supports CSS names, hex, rgb, rgba, hsl, hsla formats. // Throws InvalidColorValue if the string cannot be parsed. // Example: // Named::fromString('blue'); // Hex::fromString('#000000'); // Rgba::fromString('rgba(255, 255, 255, 1)'); // Hsla::fromString('hsla(360, 100%, 100%, 1)'); fromString(): Color // Returns the value of the red color channel. // For Hex, returns a string ('ff'); for Rgb, returns an integer (255). // Example: // Hex::fromString('#ff0000')->red(); // 'ff' // Rgb::fromString('rgb(255, 0, 0)')->red(); // 255 red(): int|string // Returns the value of the green color channel. // For Hex, returns a string ('ff'); for Rgb, returns an integer (255). // Example: // Hex::fromString('#00ff00')->green(); // 'ff' // Rgb::fromString('rgb(0, 255, 0)')->green(); // 255 green(): int|string // Returns the value of the blue color channel. // For Hex, returns a string ('ff'); for Rgb, returns an integer (255). // Example: // Hex::fromString('#0000ff')->blue(); // 'ff' // Rgb::fromString('rgb(0, 0, 255)')->blue(); // 255 blue(): int|string // Converts the current color to a Cmyk color representation. // Returns a Cmyk instance. // Example: // Rgb::fromString('rgb(0, 0, 255)')->toCmyk(); // Cmyk instance; 'cmyk(100,100,0,0)' toCmyk(): Cmyk // Converts the current color to a Hex color representation. // Returns a Hex instance. // Example: // Rgb::fromString('rgb(255, 0, 0)')->toHex(); // Hex instance; '#ff0000' toHex(): Hex // Converts the current color to an Rgb color representation. // Returns an Rgb instance. // Example: // Hex::fromString('#00ff00')->toRgb(); // Rgb instance; 'rgb(0,255,0)' toRgb(): Rgb // Converts the current color to an Rgba color representation. // Returns an Rgba instance. // Example: // Rgb::fromString('rgb(0,0,255)')->toRgba(); // Rgba instance; 'rgba(0,0,255,1)' toRgba(): Rgba // Converts the current color to an Hsl color representation. // Returns an Hsl instance. // Example: // Rgb::fromString('rgb(255,165,0)')->toHsl(); // Hsl instance; 'hsl(33,100%,50%)' toHsl(): Hsl // Converts the current color to an Hsb color representation. // Returns an Hsb instance. // Example: // Rgb::fromString('rgb(255,165,0)')->toHsb(); // Hsb instance; 'hsb(33,100%,100%)' toHsb(): Hsb // Converts the current color to a CIELab color representation. // Returns a CIELab instance. // Example: // Rgb::fromString('rgb(255,0,0)')->toCIELab(); // CIELab instance; 'CIELab(53.24,80.11,-67.22)' toCIELab(): CIELab // Converts the current color to an Xyz color representation. // Returns an Xyz instance. // Example: // Rgb::fromString('rgb(0,0,255)')->toXyz(); // Xyz instance; 'xyz(9.5547,10.087,81.996)' toXyz(): Xyz ``` -------------------------------- ### Color Conversions and Comparisons Source: https://github.com/spatie/color/blob/main/README.md Demonstrates various color conversions (to Hex, Rgb, Rgba, Cmyk, Hsl, Hsb, CIELab, Xyz) and color difference calculations (CIE76, CIE94, CIEDE2000) using the Spatie Color library. ```php $named = Named::fromString('peru'); // case-insensitive echo $named->red(); // 205 echo $named->green(); // 133 echo $named->blue(); // 63 echo $named->toHex(); // #cd853f $rgb = Rgb::fromString('rgb(55,155,255)'); echo $rgb->red(); // 55 echo $rgb->green(); // 155 echo $rgb->blue(); // 255 echo $rgb; // rgb(55,155,255) $rgba = $rgb->toRgba(); // `Spatie\Color\Rgba` $rgba->alpha(); // 1 echo $rgba; // rgba(55,155,255,1) $hex = $rgb->toHex(); // `Spatie\Color\Hex` $rgba->alpha(); // ff echo $hex; // #379bff $cmyk = $rgb->toCmyk(); // `Spatie\Color\Cmyk` echo $cmyk; // cmyk(78,39,0,0) $hsl = $rgb->toHsl(); // `Spatie\Color\Hsl` echo $hsl; // hsl(210,100%,100%) $hsb = $rgb->toHsb(); // `Spatie\Color\Hsb` echo $hsb; // hsl(210,78.4%,100%) $lab = $rgb->toCIELab(); echo $lab; // CIELab(62.91,5.34,-57.73) $xyz = $rgb->toXyz(); echo $xyz; // xyz(31.3469,31.4749,99.0308) $hex2 = Hex::fromString('#2d78c8'); $ratio = Contrast::ratio(Hex::fromString('#f0fff0'), Hex::fromString('#191970')); echo $ratio; // 15.0 $cie76_distance = Distance::CIE76($rgb, $hex2); $cie76_distance = Distance::CIE76('rgba(55,155,255,1)', '#2d78c8'); // Outputs the same thing, Factory is built-in to all comparison functions echo $cie76_distance; // 55.89468042667388 $cie94_distance = Distance::CIE94($rgb, $hex2); echo $cie94_distance; // 13.49091942790753 $cie94_textiles_distance = Distance::CIE94($rgb, $hex2, 1); // Third parameter optionally sets the application type (0 = Graphic Arts [Default], 1 = Textiles) echo $cie94_textiles_distance; // 7.0926538068477 $ciede2000_distance = Distance::CIEDE2000($rgb, $hex2); echo $ciede2000_distance; // 12.711957696300898 ``` -------------------------------- ### Factory::fromString() Usage Source: https://github.com/spatie/color/blob/main/README.md Creates color instances from various string formats using the Factory class. Throws InvalidColorValue for invalid strings. ```php Factory::fromString('rgb(0, 0, 255)'); // `Rgb` instance Factory::fromString('blue'); // `Named` instance Factory::fromString('#0000ff'); // `Hex` instance Factory::fromString('hsl(240, 100%, 50%)'); // `Hsl` instance Factory::fromString('Hello world!'); // `InvalidColorValue` exception ``` -------------------------------- ### Convert Color to HSL Source: https://github.com/spatie/color/blob/main/README.md Converts a color instance to its HSL (Hue, Saturation, Lightness) representation. Opacity is omitted if present in the source. ```php Rgb::fromString('rgb(0, 0, 255)')->toHsl(); // `Hsl` instance; 'hsl(240, 100%, 50%)' Rgba::fromString('rgba(0, 0, 255, .5)')->toHsl(); // `Hsl` instance; 'hsl(240, 100%, 50%)' ``` -------------------------------- ### Convert Color to HSB Source: https://github.com/spatie/color/blob/main/README.md Converts a color instance to its HSB (Hue, Saturation, Brightness) representation. Opacity is omitted if present in the source. ```php Rgb::fromString('rgb(0, 0, 255)')->toHsb(); // `Hsl` instance; 'hsb(240, 100%, 100%)' ``` -------------------------------- ### String Casting of Colors Source: https://github.com/spatie/color/blob/main/README.md Casts various color instances to their string representations. ```php (string) Rgb::fromString('rgb(0, 0, 255)'); // 'rgb(0,0,255)' (string) Rgba::fromString('rgb(0, 0, 255, .5)'); // 'rgb(0,0,255,0.5)' (string) Hex::fromString('#0000ff'); // '#0000ff' (string) Hsl::fromString('hsl(240, 100%, 50%)'); // 'hsl(240, 100%, 50%)' (string) Hsla::fromString('hsla(240, 100%, 50%, 1.0)'); // 'hsla(240, 100%, 50%, 1.0)' ``` -------------------------------- ### Color Difference Calculation Methods Source: https://github.com/spatie/color/blob/main/README.md Provides methods for calculating the color difference between two colors using CIE76, CIE94, and CIEDE2000 algorithms. ```APIDOC class Distance // Calculates the color difference using the CIE76 formula. // Accepts Color instances or color strings. // Example: // Distance::CIE76(Rgb::fromString('rgba(55,155,255,1)'), '#2d78c8'); // 55.89468042667388 CIE76(color1: Color|string, color2: Color|string): float // Calculates the color difference using the CIE94 formula. // Accepts Color instances or color strings. // The optional third parameter specifies the application type (0 for Graphic Arts [default], 1 for Textiles). // Example: // Distance::CIE94(Rgb::fromString('rgba(55,155,255,1)'), '#2d78c8'); // 13.49091942790753 // Distance::CIE94(Rgb::fromString('rgba(55,155,255,1)'), '#2d78c8', 1); // 7.0926538068477 CIE94(color1: Color|string, color2: Color|string, applicationType: int = 0): float // Calculates the color difference using the CIEDE2000 formula. // Accepts Color instances or color strings. // Example: // Distance::CIEDE2000(Rgb::fromString('rgba(55,155,255,1)'), '#2d78c8'); // 12.711957696300898 CIEDE2000(color1: Color|string, color2: Color|string): float class Contrast // Calculates the contrast ratio between two colors. // Accepts Hex color strings. // Example: // Contrast::ratio(Hex::fromString('#f0fff0'), Hex::fromString('#191970')); // 15.0 ratio(hex1: string, hex2: string): float ``` -------------------------------- ### Convert Color to RGB Source: https://github.com/spatie/color/blob/main/README.md Converts a color instance to its RGB representation. Opacity is omitted if present in the source. ```php Hex::fromString('#0000ff')->toRgb(); // `Rgb` instance; 'rgb(0, 0, 255)' Rgba::fromString('rgb(0, 0, 255, .5)')->toRgb(); // `Rgb` instance; 'rgb(0, 0, 255)' ``` -------------------------------- ### Convert Color to HSLA Source: https://github.com/spatie/color/blob/main/README.md Converts a color instance to its HSLA (Hue, Saturation, Lightness, Alpha) representation. Supports adding alpha if the original format doesn't have it. ```php Rgb::fromString('rgb(0, 0, 255)')->toHsla(); // `Hsla` instance; 'hsla(240, 100%, 50%, 1.0)' Rgb::fromString('rgb(0, 0, 255)')->toHsla(.5); // `Hsla` instance; 'hsla(240, 100%, 50%, 0.5)' ``` -------------------------------- ### Convert Color to Hex Source: https://github.com/spatie/color/blob/main/README.md Converts a color instance to its Hex representation. Supports adding alpha if the original format doesn't have it. ```php Rgb::fromString('rgb(0, 0, 255)')->toHex(); // `Hex` instance; '#0000ff' ``` -------------------------------- ### Convert Color to RGBA Source: https://github.com/spatie/color/blob/main/README.md Converts a color instance to its RGBA (Red, Green, Blue, Alpha) representation. Supports adding alpha if the original format doesn't have it. ```php Rgb::fromString('rgb(0, 0, 255)')->toRgba(); // `Rgba` instance; 'rgba(0, 0, 255, 1)' Rgba::fromString('rgb(0, 0, 255)')->toRgba(.5); // `Rgba` instance; 'rgba(0, 0, 255, .5)' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.