{"id":16789655,"url":"https://github.com/ryangjchandler/color","last_synced_at":"2025-03-17T03:30:20.985Z","repository":{"id":54607434,"uuid":"331680679","full_name":"ryangjchandler/color","owner":"ryangjchandler","description":"A simple Color object for PHP packages and applications. 🎨","archived":true,"fork":false,"pushed_at":"2021-03-19T10:06:44.000Z","size":63,"stargazers_count":38,"open_issues_count":0,"forks_count":7,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-15T20:48:44.557Z","etag":null,"topics":["color","color-difference","color-distance","color-object","composer","hex-to-rgb","php","php-color","random-color","rgb-to-hex"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"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/ryangjchandler.png","metadata":{"files":{"readme":"README.md","changelog":null,"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},"funding":{"github":"ryangjchandler"}},"created_at":"2021-01-21T16:07:03.000Z","updated_at":"2024-10-29T16:34:03.000Z","dependencies_parsed_at":"2022-08-13T21:20:24.064Z","dependency_job_id":null,"html_url":"https://github.com/ryangjchandler/color","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryangjchandler%2Fcolor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryangjchandler%2Fcolor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryangjchandler%2Fcolor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryangjchandler%2Fcolor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryangjchandler","download_url":"https://codeload.github.com/ryangjchandler/color/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243968379,"owners_count":20376388,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","color-difference","color-distance","color-object","composer","hex-to-rgb","php","php-color","random-color","rgb-to-hex"],"created_at":"2024-10-13T08:27:57.210Z","updated_at":"2025-03-17T03:30:20.696Z","avatar_url":"https://github.com/ryangjchandler.png","language":"PHP","funding_links":["https://github.com/sponsors/ryangjchandler"],"categories":[],"sub_categories":[],"readme":"# Color\n\nA simple Color object for PHP packages and applications. 🎨\n\n## Installation\n\nThis package can be installed via Composer:\n\n```bash\ncomposer require ryangjchandler/color\n```\n\n## Usage\n\nThis package provides a single `RyanChandler\\Color\\Color` object.\n\n### Creating a color\n\nTo create a color, instantiate a new `RyanChandler\\Color\\Color` object:\n\n```php\nuse RyanChandler\\Color\\Color;\n\n$color = new Color(255, 255, 255);\n```\n\nThe constructor accepts three _optional_ arguments. The red, green and blue decimal representations of your color.\n\nIf you prefer using static constructors you can use the `Color::new()` method, à la Rust.\n\n```php\n$color = Color::new(255, 255, 255);\n```\n\n### Creating a color from a hex\n\nIf you wish to create a color using the hex representation, you can use the `Color::hex()` method.\n\n```php\n$color = Color::hex('#ffffff');\n```\n\nThis will convert your hex representation into the RGB equivalent.\n\nThe `#` is not compulsory. It will only be removed _if_ the string provided starts with it.\n\n\u003e It's worth noting that any alpha values specified on the hex value will be stripped since the string is clamped to a length of 6. This is something that might be supported in a future version.\n\n\n### Creating a color from HSL values\n\nYou can also use hue, saturation and lightness values to create a color using the `Color::hsl()` method.\n\n```php\n$color = Color::hsl(0, 0, 100);\n```\n\nThis will convert your HSL values into the RGB equivalent.\n\n\u003e You can also define the alpha as an optional fourth argument `Color::hsl(0, 0, 100, 0.5)`\n\n\n### Generating a random color\n\nYou can generate a random color using the `Color::random()` method.\n\n```php\n$random = Color::random();\n```\n\n### Accessing the red, green and blue values\n\nEach color value can be accessed using a public property on the `Color` object.\n\n```php\n$color = Color::new(255, 255, 255);\n\n$color-\u003ered; // 255\n$color-\u003egreen; // 255\n$color-\u003eblue; // 255\n```\n\n### Getting the hex representation\n\nIf you wish to get the hex equivalent of your color, you can use the `Color::toHex()` method.\n\n```php\nColor::new(255, 255, 255)-\u003etoHex(); // #ffffff\n```\n\n### Getting the HSL representation\n\nIf you wish to get the HSL equivalent of your color as an array, you can use the `Color::toHsl()` method.\n\n```php\n[$h, $s, $l] = Color::new(255, 255, 255)-\u003etoHsl(); // [0, 0, 100]\n```\n\n### Getting the string representation\n\nBy default, the `Color::toString()` method returns a tuple-like string.\n\n```php\nColor::new(255, 255, 255)-\u003etoString(); // \"(255, 255, 255)\"\n```\n\nYou can also use the `Color::toString()` method to retrieve the hex representation.\n\n```php\nColor::new(255, 255, 255)-\u003etoString(true); // #ffffff\n```\n\nOr use PHP's typecasting to get a string instead.\n\n```php\n(string) Color::new(255, 255, 255); // \"(255, 255, 255)\"\n```\n\n### Getting an array\n\nYou can use the `Color::toArray()` method to get all three color values in an ordered list.\n\n```php\nColor::new(255, 255, 255)-\u003etoArray(); // [255, 255, 255]\n```\n\nThe array does not use string-keys, so you can unpack the array into separate variables too.\n\n```php\n[$r, $g, $b] = Color::new(255, 255, 255)-\u003etoArray();\n```\n\n### Finding the distance between 2 colors\n\nIf you need to calculate the distance between 2 colors, you can use the `Color::distanceBetween()` method.\n\n```php\n$one = Color::new(0, 0, 220);\n$two = Color::new(255, 0, 220);\n\nColor::distanceBetween($one, $two); // 65_025\n```\n\nThe return value is the distance between the 2 colors, squared. Generally speaking, this number will be more\nreadable and recognisable than the radical (result of the square root).\n\n#### Using an existing `Color`\n\nIf you already have a `Color` object, you can use the `Color::distanceTo()` method as well.\n\n```php\n$one = Color::new(0, 0, 220);\n$two = Color::new(255, 0, 220);\n\n$one-\u003edistanceTo($two); // 65_025\n```\n\n\u003e It is worth noting that the distance calculations and `Color` objects **do not** support alpha-based colors. This is potentially something that will be added in the future.\n\n### Comparing colors\n\nYou can compare two colors using the `Color::bothEqual()` method.\n\n```php\n$one = Color::rgb('#aaa');\n$two = Color::rgb('#aaa');\n$three = Color::rgb('#ccc');\n\nColor::bothEqual($one, $two); // true\nColor::bothEqual($one, $three); // false\n```\n\nYou can also compare one color to another using the `equals` method on one of the colors.\n\n```php\n$one = Color::rgb('#aaa');\n$two = Color::rgb('#aaa');\n$three = Color::rgb('#ccc');\n\n$one-\u003eequals($two); // true\n$one-\u003eequals($three); // false\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryangjchandler%2Fcolor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryangjchandler%2Fcolor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryangjchandler%2Fcolor/lists"}