ozdemirburak/iris
Iris is a PHP 8.1+ color library for parsing, manipulating, and converting colors across Hex/Hexa, RGB/RGBA, HSL/HSLA, HSV, CMYK, and OKLCH. Provides format classes with channel accessors and easy toX() conversions.
Hex, Hsl, Rgb, etc.), which aligns well with Laravel’s single-responsibility principle and type safety (PHP 8.1+).toHex(), saturate(), and mix() return new instances rather than modifying state, which is functional and thread-safe—ideal for Laravel’s request/response lifecycle.Factory::init() method abstracts color parsing, reducing boilerplate when dealing with dynamic color inputs (e.g., user uploads or database fields).hex, hsl).hex, rgb) and converting them on-the-fly.hsl(120,100%,50%) in a theme_color column and convert to hex for frontend use.@media (prefers-color-scheme: dark) { --primary: {{ $color->darken(20)->toHex() }} }).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Performance | Color conversions are O(1) but may introduce overhead in loops. | Benchmark critical paths (e.g., gradient generation for 100+ colors). Cache results if reused. |
| Precision Loss | CMYK/RGB conversions may round values (fixed in v4.1.1). | Test edge cases (e.g., cmyk(0,0,0,100) → #000000). Use Oklch for perceptual uniformity. |
| Alpha Handling | Alpha precision varies across formats (e.g., Hexa vs. Hsla). |
Standardize on float for internal alpha calculations; document expected inputs. |
| Backward Compatibility | Breaking changes in v4.x (PHP 8.1+, PHPUnit 10/11). | Pin to ^4.0 in composer.json if migrating from older Laravel/PHP versions. |
| OKLCH Support | Experimental/less common than RGB/HSL. | Use only for advanced use cases (e.g., accessibility tools). Fall back to HSL for broad compatibility. |
Color class) or augment it (e.g., for OKLCH/gradient features)?hex) or user-friendly formats (e.g., hsl(120,100%,50%))?Illuminate\Validation\Rule).isLight()/isDark() methods can help.oklch() alpha support, new color spaces like lch()).<div style="background: {{ $themeColor->toHex() }}">{{ $themeColor->lighten(10)->toHex() }}</div>
public function getHexAttribute(): string
{
return $this->attributes['hsl'] ? (new Hsl($this->hsl))->toHex() : '#000';
}
return response()->json(['gradient' => (new Hex('#ff0000'))->gradient(new Hex('#00ff00'), 5)]);
color.js may suffice).hex ↔ rgb).| Component | Compatibility Notes |
|---|---|
| Laravel 9/10 | Full support (PHP 8.1+). |
| Laravel 8 | Possible with PHP 8.0 polyfills, but test Oklch/gradient methods. |
| PHP 8.0 | Works but lacks type safety (e.g., no return type hints). |
| Databases | Store colors as string (e.g., hex, hsl) or json (for complex objects). |
| Caching | Cache converted colors (e.g., Redis) if generated repeatedly (e.g., UI themes). |
| Testing | Use PHPUnit to validate conversions (Iris includes tests as a reference). |
composer require ozdemirburak/iris.// app/Providers/AppServiceProvider.php
public function boot()
{
app()->singleton('color', fn() => new Factory());
}
use Illuminate\Validation\Rule;
Rule::define('hex', fn ($attribute, $value, $fail) => (new Hex($value)) instanceof Hex || $fail('Invalid hex color.'));
// app/Helpers/ColorHelper.php
if (!function_exists('color')) {
function color(string $input): OzdemirBurak\Iris\Color\BaseColor
{
return Factory::init($input);
}
}
How can I help you explore Laravel packages today?