spatie/color
PHP library for color parsing, conversion, and comparison. Supports CSS named colors plus RGB/RGBA/ARGB, Hex, HSL/HSLA, HSB, CMYK, CIELab, and XYZ. Includes WCAG contrast ratio and Delta E distances (CIE76, CIE94, CIEDE2000).
spatie/color package is a lightweight utility library for color space conversions (e.g., RGB ↔ HEX ↔ CMYK ↔ HSL) and color manipulation. It fits well in applications requiring:
composer require spatie/color). No framework-specific hooks or middleware required.intervention/image with overlapping features).Color::fromHex('#FF5733')->toRgb()). Edge cases (e.g., invalid HEX inputs) should be validated.php-color (lower-level) or league/color-extractor (for palette extraction) if broader functionality is needed.@php echo Color::fromHex('#3498db')->toCssVariable() in Blade templates).return response()->json(['color' => $color->toHex()])).theme() function for dynamic styling.colors.json config file and hydrate them with the package.Color objects in a controlled component.spatie/color methods.// Before
if (preg_match('/^#([0-9a-f]{3}){1,2}$/i', $hex)) { ... }
// After
try {
$color = Color::fromHex($hex);
} catch (InvalidColorException) { ... }
$this->app->singleton(Color::class, function () {
return new Color();
});
varchar(7)) or RGB arrays (e.g., json column) for flexibility.Schema::table('products', function (Blueprint $table) {
$table->string('primary_color_hex')->nullable();
$table->json('primary_color_rgb')->nullable();
});
spatie/color with older color-handling logic (e.g., a LegacyColorAdapter that converts between formats).app/Services/ColorService).Cache::remember()).spatie/color for breaking changes (e.g., renamed methods). Use composer why-not spatie/color:^2.0 to check constraints.composer.json to avoid surprises:
"spatie/color": "^1.0"
ExtendedColor with custom methods like adjustBrightness()).class ExtendedColor extends Color {
public function lighten(float $percentage): self {
$rgb = $this->toRgb();
// Custom logic...
return new static($rgb);
}
}
dd($color->toArray()) to inspect color objects.#GHI).parallel helper or Pipes).$colors = collect($hexStrings)->parallel()->map(fn ($hex) => Color::fromHex($hex));
Cache::forever("color:{$hex}", $color)).WHERE primary_color_hex = '#FF5733').| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Invalid HEX input | Crashes or silent failures | Use try-catch blocks or input validation. |
| Color space unsupported | Partial functionality | Document supported formats; add feature flags. |
| Batch processing timeout | Job queue failures | Split into smaller chunks or use async workers. |
| Package abandonment | Technical debt | Fork or migrate to alternatives (e.g., php-color). |
| CSS/JS incompatibility |
How can I help you explore Laravel packages today?