├── phpstan.neon.dist ├── .php-cs-fixer.dist.php ├── .github ├── dependabot.yml └── workflows │ └── test-suite.yaml ├── .editorconfig ├── composer.json ├── LICENSE ├── src ├── Colorize.php └── Color.php └── README.md /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | parameters: 2 | level: max 3 | 4 | paths: 5 | - src 6 | - tests 7 | -------------------------------------------------------------------------------- /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | in([ 6 | __DIR__ . DIRECTORY_SEPARATOR . 'src', 7 | __DIR__ . DIRECTORY_SEPARATOR . 'tests', 8 | ]); 9 | 10 | return PHLAK\CodingStandards\ConfigFactory::make($finder); 11 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | updates: 4 | - package-ecosystem: composer 5 | directory: "/" 6 | schedule: 7 | interval: monthly 8 | assignees: [PHLAK] 9 | 10 | - package-ecosystem: github-actions 11 | directory: "/" 12 | schedule: 13 | interval: monthly 14 | assignees: [PHLAK] 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 4 7 | indent_style = space 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | indent_size = 4 14 | 15 | [*.{yml,yaml}] 16 | indent_style = space 17 | indent_size = 2 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "phlak/colorizer", 3 | "description": "Generate persistantly unique colors from a string.", 4 | "keywords": ["colorize", "color hash", "color"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Chris Kankiewicz", 9 | "email": "Chris@ChrisKankiewicz.com", 10 | "homepage": "https://www.chriskankiewicz.com" 11 | } 12 | ], 13 | "support": { 14 | "issues": "https://github.com/PHLAK/Colorizer/issues" 15 | }, 16 | "require": { 17 | "php": "^8.2 || ^8.3 || ^8.4", 18 | "webmozart/assert": "^1.11" 19 | }, 20 | "require-dev": { 21 | "phlak/coding-standards": "^3.0", 22 | "phpunit/phpunit": "^11", 23 | "phpstan/phpstan": "^2.0" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "PHLAK\\Colorizer\\": "src/" 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Chris Kankiewicz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Colorize.php: -------------------------------------------------------------------------------- 1 | normalize($this->normalizeMin, $this->normalizeMax); 38 | } 39 | 40 | /** 41 | * Return a new static Colorize class with specified normalization values. 42 | * 43 | * @return self This colorize object 44 | */ 45 | public function normalize(int $min = 0, int $max = 255): self 46 | { 47 | return new self($min, $max); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /.github/workflows/test-suite.yaml: -------------------------------------------------------------------------------- 1 | name: Colorizer Test Suite 2 | on: [push] 3 | 4 | jobs: 5 | coding-standards: 6 | name: Coding Standards 7 | runs-on: 'ubuntu-latest' 8 | 9 | env: 10 | PHP_CS_FIXER_IGNORE_ENV: 1 11 | 12 | steps: 13 | - name: Checkout Repository 14 | uses: actions/checkout@v2 15 | 16 | - name: Setup PHP 17 | uses: shivammathur/setup-php@v2 18 | with: 19 | php-version: 8.4 20 | 21 | - name: Install PHP Dependencies 22 | run: composer install --no-interaction --no-progress --no-scripts --prefer-dist 23 | 24 | - name: Verify Coding Standards 25 | run: vendor/bin/php-cs-fixer fix --diff --dry-run 26 | 27 | static-analysis: 28 | name: Static Analysis 29 | runs-on: 'ubuntu-latest' 30 | 31 | steps: 32 | - name: Checkout Repository 33 | uses: actions/checkout@v2 34 | 35 | - name: Setup PHP 36 | uses: shivammathur/setup-php@v2 37 | with: 38 | php-version: 8.4 39 | 40 | - name: Install PHP Dependencies 41 | run: composer install --no-interaction --no-progress --no-scripts --prefer-dist 42 | 43 | - name: Run Static Analysis 44 | run: vendor/bin/phpstan analyze 45 | 46 | tests: 47 | name: Tests 48 | runs-on: 'ubuntu-latest' 49 | 50 | strategy: 51 | matrix: 52 | php-versions: ['8.2', '8.3', '8.4'] 53 | 54 | steps: 55 | - name: Checkout Repository 56 | uses: actions/checkout@v2 57 | 58 | - name: Setup PHP 59 | uses: shivammathur/setup-php@v2 60 | with: 61 | php-version: ${{ matrix.php-versions }} 62 | coverage: xdebug 63 | 64 | - name: Install PHP Dependencies 65 | run: composer install --no-interaction --no-progress --no-scripts --prefer-dist 66 | 67 | - name: Run Tests 68 | run: vendor/bin/phpunit --coverage-text 69 | -------------------------------------------------------------------------------- /src/Color.php: -------------------------------------------------------------------------------- 1 | red, $min), $max); 40 | $green = min(max($this->green, $min), $max); 41 | $blue = min(max($this->blue, $min), $max); 42 | 43 | return new self($red, $green, $blue); 44 | } 45 | 46 | /** 47 | * Returns the a hex string representation of the color object. 48 | * 49 | * @return string Hex string representation 50 | */ 51 | public function hex(): string 52 | { 53 | return sprintf('#%s%s%s', $this->decToHex($this->red), $this->decToHex($this->green), $this->decToHex($this->blue)); 54 | } 55 | 56 | /** 57 | * Returns a rgb() string representation of the color object. 58 | * 59 | * @return string rgb() string representation 60 | */ 61 | public function rgb(): string 62 | { 63 | return sprintf('rgb(%d, %d, %d)', $this->red, $this->green, $this->blue); 64 | } 65 | 66 | /** 67 | * Returns a rgba() string representation of the color object. 68 | * 69 | * @return string rgba() string representation 70 | */ 71 | public function rgba(): string 72 | { 73 | return sprintf('rgba(%d, %d, %d, %g)', $this->red, $this->green, $this->blue, $this->alpha); 74 | } 75 | 76 | /** 77 | * Converts an integer decimal value to a padded hex value. 78 | * 79 | * @param $value Decimal integer value 80 | * 81 | * @return string String hex value 82 | */ 83 | private function decToHex(int $value): string 84 | { 85 | return str_pad(dechex($value), 2, '0', STR_PAD_LEFT); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Colorizer 2 | ========= 3 | 4 |

5 | Colorizer 6 |

7 | 8 |

9 | Join our Community 10 | Become a Sponsor 11 | One-time Donation 12 |
13 | Latest Stable Version 14 | Total Downloads 15 | License 16 | Build Status 17 |

18 | 19 | 20 |

21 | Generate persistently unique colors from a string. 22 |
23 | Created by Chris Kankiewicz (@PHLAK) 24 |

25 | 26 | --- 27 | 28 | Requirements 29 | ------------ 30 | 31 | - [PHP](https://php.net) >= 8.1 32 | 33 | Install with Composer 34 | --------------------- 35 | 36 | ```bash 37 | composer require phlak/colorizer 38 | ``` 39 | 40 | Usage 41 | ----- 42 | 43 | ```php 44 | // Import Colorizer 45 | use PHLAK\Colorizer; 46 | 47 | // Initialize Colorizer 48 | $colorize = new Colorizer\Colorize(); 49 | 50 | // Generate a Color object from 'foo' 51 | $color = $colorize->text('foo'); // Returns a new Color object 52 | 53 | // Get the red, green and blue values 54 | $color->red; // 165 55 | $color->green; // 196 56 | $color->blue; // 254 57 | 58 | // Generate a hex color code 59 | $color->hex(); // Returns '#a5c4fe' 60 | 61 | // Generate a RGB color code 62 | $color->rgb(); // Returns 'rgb(165, 196, 254)' 63 | ``` 64 | 65 | #### Normalizing Colors 66 | 67 | You can enforce RGB values to fall within a certain range to prevent colors 68 | from being too bright or dark. This is possible by passing minimum and maximum 69 | normalization values (0-255) to the Colorize class on initialization: 70 | 71 | ```php 72 | $colorize = new Colorizer\Colorize(64, 224); 73 | ``` 74 | 75 | or fluently: 76 | 77 | ```php 78 | $colorize->text('foo')->normalize(64, 224)->rgb(); // Returns 'rgb(165, 196, 224)' 79 | ``` 80 | 81 | Changelog 82 | --------- 83 | 84 | A list of changes can be found on the [GitHub Releases](https://github.com/PHLAK/Colorizer/releases) page. 85 | 86 | Troubleshooting 87 | --------------- 88 | 89 | For general help and support join our [GitHub Discussion](https://github.com/PHLAK/Colorizer/discussions) 90 | or reach out on [Bluesky](https://bsky.app/profile/phlak.dev). 91 | 92 | Please report bugs to the [GitHub Issue Tracker](https://github.com/PHLAK/Colorizer/issues). 93 | 94 | Copyright 95 | --------- 96 | 97 | This project is licensed under the [MIT License](https://github.com/PHLAK/Colorizer/blob/master/LICENSE). 98 | --------------------------------------------------------------------------------