{"id":17623449,"url":"https://github.com/spatie/color","last_synced_at":"2026-02-09T00:07:15.565Z","repository":{"id":39717557,"uuid":"68709937","full_name":"spatie/color","owner":"spatie","description":"A little library to deal with color conversions","archived":false,"fork":false,"pushed_at":"2026-01-31T11:03:21.000Z","size":225,"stargazers_count":380,"open_issues_count":0,"forks_count":40,"subscribers_count":10,"default_branch":"main","last_synced_at":"2026-01-31T22:59:08.305Z","etag":null,"topics":["color","conversion","hex","php","rgb"],"latest_commit_sha":null,"homepage":"https://spatie.be/opensource/php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"spatie"}},"created_at":"2016-09-20T12:32:45.000Z","updated_at":"2026-01-31T11:03:03.000Z","dependencies_parsed_at":"2024-10-23T03:37:37.854Z","dependency_job_id":"63d5e5f4-9839-41f6-9d73-d91041420e87","html_url":"https://github.com/spatie/color","commit_stats":{"total_commits":140,"total_committers":28,"mean_commits":5.0,"dds":0.7785714285714286,"last_synced_commit":"417ab254bbb8e17c1fbb98f6a8f226c5f5d60a2f"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"purl":"pkg:github/spatie/color","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fcolor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fcolor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fcolor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fcolor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/color/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fcolor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29250295,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-08T22:49:53.206Z","status":"ssl_error","status_checked_at":"2026-02-08T22:49:51.384Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["color","conversion","hex","php","rgb"],"created_at":"2024-10-22T21:09:28.965Z","updated_at":"2026-02-09T00:07:15.558Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie"],"categories":[],"sub_categories":[],"readme":"# A little library to handle color conversions and comparisons\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/color.svg?style=flat-square)](https://packagist.org/packages/spatie/color)\n![Tests](https://github.com/spatie/color/workflows/Tests/badge.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/color.svg?style=flat-square)](https://packagist.org/packages/spatie/color)\n\nA little library to handle color conversions and comparisons. Currently, supports CSS names, rgb, rgba, argb, hex, hsl, hsla, CIELab, and xyz color formats as well as CIE76, CIE94, and CIEDE2000 color comparison algorithms.\n\n```php\n$named = Named::fromString('peru'); // case-insensitive\n\necho $named-\u003ered(); // 205\necho $named-\u003egreen(); // 133\necho $named-\u003eblue(); // 63\n\necho $named-\u003etoHex(); // #cd853f\n\n$rgb = Rgb::fromString('rgb(55,155,255)');\n\necho $rgb-\u003ered(); // 55\necho $rgb-\u003egreen(); // 155\necho $rgb-\u003eblue(); // 255\n\necho $rgb; // rgb(55,155,255)\n\n$rgba = $rgb-\u003etoRgba(); // `Spatie\\Color\\Rgba`\n$rgba-\u003ealpha(); // 1\necho $rgba; // rgba(55,155,255,1)\n\n$hex = $rgb-\u003etoHex(); // `Spatie\\Color\\Hex`\n$rgba-\u003ealpha(); // ff\necho $hex; // #379bff\n\n$cmyk = $rgb-\u003etoCmyk(); // `Spatie\\Color\\Cmyk`\necho $cmyk; // cmyk(78,39,0,0)\n\n$hsl = $rgb-\u003etoHsl(); // `Spatie\\Color\\Hsl`\necho $hsl; // hsl(210,100%,100%)\n\n$hsb = $rgb-\u003etoHsb(); // `Spatie\\Color\\Hsb`\necho $hsb; // hsl(210,78.4%,100%)\n\n$lab = $rgb-\u003etoCIELab();\necho $lab; // CIELab(62.91,5.34,-57.73)\n\n$xyz = $rgb-\u003etoXyz();\necho $xyz; // xyz(31.3469,31.4749,99.0308)\n\n$argb = $rgb-\u003etoArgb(0.5); // `Spatie\\Color\\Argb`\necho $argb; // argb(0.50,55,155,255)\n\n$hex2 = Hex::fromString('#2d78c8');\n\n$ratio = Contrast::ratio(Hex::fromString('#f0fff0'), Hex::fromString('#191970')); \necho $ratio; // 15.0\n\n$cie76_distance = Distance::CIE76($rgb, $hex2);\n$cie76_distance = Distance::CIE76('rgba(55,155,255,1)', '#2d78c8'); // Outputs the same thing, Factory is built-in to all comparison functions\necho $cie76_distance; // 55.89468042667388\n\n$cie94_distance = Distance::CIE94($rgb, $hex2);\necho $cie94_distance; // 13.49091942790753\n\n$cie94_textiles_distance = Distance::CIE94($rgb, $hex2, 1); // Third parameter optionally sets the application type (0 = Graphic Arts [Default], 1 = Textiles)\necho $cie94_textiles_distance; // 7.0926538068477\n\n$ciede2000_distance = Distance::CIEDE2000($rgb, $hex2);\necho $ciede2000_distance; // 12.711957696300898\n```\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/color.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/color)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Requirements\n\nPHP 8.2 or higher is required.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/color\n```\n\n## Usage\n\nThe `Color` package contains a separate class per color format, which each implement a `Color` interface.\n\nThere are eleven classes which implement the `Color` interface:\n\n- `Argb`\n- `CIELab`\n- `Cmyk`\n- `Hex`\n- `Hsb`\n- `Hsl`\n- `Hsla`\n- `Named`\n- `Rgb`\n- `Rgba`\n- `Xyz`\n\n### `interface Spatie\\Color\\Color`\n\n#### `fromString(): Color`\n\nParses a color string and returns a `Color` implementation, depending on the format of the input string.\n\n```php\nNamed::fromString('blue');\nHex::fromString('#000000');\nRgba::fromString('rgba(255, 255, 255, 1)');\nArgb::fromString('argb(1, 255, 255, 255)');\nHsla::fromString('hsla(360, 100%, 100%, 1)');\n```\n\nThrows an `InvalidColorValue` exception if the string can't be parsed.\n\n\u003e `Rgb`, `Rgba`, `Hsl` and `Hsla` strings are allowed to have spaces. `rgb(0,0,0)` is just as valid as `rgb(0, 0, 0)`.\n\n#### `red(): int|string`\n\nReturn the value of the `red` color channel.\n\n```php\nHex::fromString('#ff0000')-\u003ered(); // 'ff'\nRgb::fromString('rgb(255, 0, 0)')-\u003ered(); // 255\n```\n\n#### `green(): int|string`\n\nReturn the value of the `green` color channel.\n\n```php\nHex::fromString('#00ff00')-\u003egreen(); // 'ff'\nRgb::fromString('rgb(0, 255, 0)')-\u003egreen(); // 255\n```\n\n#### `blue(): int|string`\n\nReturn the value of the `blue` color channel.\n\n```php\nHex::fromString('#0000ff')-\u003eblue(); // 'ff'\nRgb::fromString('rgb(0, 0, 255)')-\u003eblue(); // 255\n```\n\n#### `toCmyk(): Cmyk`\n\nConvert a color to a `Cmyk` color.\n\n```php\nRgb::fromString('rgb(0, 0, 255)')-\u003etoCmyk();\n// `Cmyk` instance; 'cmyk(100,100,0,0)'\n```\n\n#### `toHex(): Hex`\n\nConvert a color to a `Hex` color.\n\n```php\nRgb::fromString('rgb(0, 0, 255)')-\u003etoHex();\n// `Hex` instance; '#0000ff'\n```\n\nWhen coming from a color format that doesn't support opacity, it can be added by passing it to the `$alpha` parameter.\n\n\n#### `toHsb(): Hsb`\n\nConvert a color to a `Hsb` color.\n\n```php\nRgb::fromString('rgb(0, 0, 255)')-\u003etoHsb();\n// `Hsl` instance; 'hsb(240, 100%, 100%)'\n```\n\n#### `toHsl(): Hsl`\n\nConvert a color to a `Hsl` color.\n\n```php\nRgb::fromString('rgb(0, 0, 255)')-\u003etoHsl();\n// `Hsl` instance; 'hsl(240, 100%, 50%)'\n```\n\nWhen coming from a color format that supports opacity, the opacity will simply be omitted.\n\n```php\nRgba::fromString('rgba(0, 0, 255, .5)')-\u003etoHsl();\n// `Hsl` instance; 'hsl(240, 100%, 50%)'\n```\n\n#### `toHsla(float $alpha = 1): Hsla`\n\nConvert a color to a `Hsla` color.\n\n```php\nRgb::fromString('rgb(0, 0, 255)')-\u003etoHsla();\n// `Hsla` instance; 'hsla(240, 100%, 50%, 1.0)'\n```\n\nWhen coming from a color format that doesn't support opacity, it can be added by passing it to the `$alpha` parameter.\n\n```php\nRgb::fromString('rgb(0, 0, 255)')-\u003etoHsla(.5);\n// `Hsla` instance; 'hsla(240, 100%, 50%, 0.5)'\n```\n\n#### `toArgb(float $alpha = 1): Argb`\n\nConvert a color to an `Argb` color. ARGB places the alpha channel first, commonly used in Android development and Windows APIs.\n\n```php\nRgb::fromString('rgb(0, 0, 255)')-\u003etoArgb();\n// `Argb` instance; 'argb(1.00,0,0,255)'\n```\n\nWhen coming from a color format that doesn't support opacity, it can be added by passing it to the `$alpha` parameter.\n\n```php\nRgb::fromString('rgb(0, 0, 255)')-\u003etoArgb(.5);\n// `Argb` instance; 'argb(0.50,0,0,255)'\n```\n\n#### `toRgb(): Rgb`\n\nConvert a color to an `Rgb` color.\n\n```php\nHex::fromString('#0000ff')-\u003etoRgb();\n// `Rgb` instance; 'rgb(0, 0, 255)'\n```\n\nWhen coming from a color format that supports opacity, the opacity will simply be omitted.\n\n```php\nRgba::fromString('rgb(0, 0, 255, .5)')-\u003etoRgb();\n// `Rgb` instance; 'rgb(0, 0, 255)'\n```\n\n#### `toRgba(float $alpha = 1): Rgba`\n\nConvert a color to a `Rgba` color.\n\n```php\nRgb::fromString('rgb(0, 0, 255)')-\u003etoRgba();\n// `Rgba` instance; 'rgba(0, 0, 255, 1)'\n```\n\nWhen coming from a color format that doesn't support opacity, it can be added by passing it to the `$alpha` parameter.\n\n```php\nRgba::fromString('rgb(0, 0, 255)')-\u003etoRgba(.5);\n// `Rgba` instance; 'rgba(0, 0, 255, .5)'\n```\n\n#### `__toString(): string`\n\nCast the color to a string.\n\n```php\n(string) Rgb::fromString('rgb(0, 0, 255)'); // 'rgb(0,0,255)'\n(string) Rgba::fromString('rgb(0, 0, 255, .5)'); // 'rgb(0,0,255,0.5)'\n(string) Hex::fromString('#0000ff'); // '#0000ff'\n(string) Hsl::fromString('hsl(240, 100%, 50%)'); // 'hsl(240, 100%, 50%)'\n(string) Hsla::fromString('hsla(240, 100%, 50%, 1.0)'); // 'hsla(240, 100%, 50%, 1.0)'\n```\n\n### `Factory::fromString(): Color`\n\nWith the `Factory` class, you can create a color instance from any string (it does an educated guess under the hood). If the string isn't a valid color string in any format, it throws an `InvalidColorValue` exception.\n\n```php\nFactory::fromString('rgb(0, 0, 255)'); // `Rgb` instance\nFactory::fromString('blue'); // `Named` instance\nFactory::fromString('#0000ff'); // `Hex` instance\nFactory::fromString('hsl(240, 100%, 50%)'); // `Hsl` instance\nFactory::fromString('Hello world!'); // `InvalidColorValue` exception\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Credits\n\n- [Sebastian De Deyne](https://github.com/sebastiandedeyne)\n- [All Contributors](../../contributors)\n\n## About Spatie\nSpatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fcolor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fcolor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fcolor/lists"}