mistic100/randomcolor
Generate attractive random colors in PHP (port of David Merfield’s randomColor). Create single or multiple colors with options for hue, luminosity, alpha, and output formats like hex, rgb(a), hsl(a), or hsv(a). Supports custom PRNG.
composer require mistic100/randomcolor
use Colors\RandomColor;
// Generate a single hex color
$color = RandomColor::one();
echo $color; // e.g., "#e74c3c"
// Generate a light blue color in RGB format
$color = RandomColor::one([
'luminosity' => 'light',
'hue' => 'blue',
'format' => 'rgbCss'
]);
echo $color; // e.g., "rgb(135, 206, 235)"
src/RandomColor.php for advanced customization (e.g., overriding prng).Dynamic Chart Colors in Laravel:
// In a controller or service
$chartColors = RandomColor::many(5, [
'hue' => ['blue', 'green', 'purple'],
'luminosity' => 'light',
'format' => 'hex'
]);
// Pass to Blade view
return view('dashboard', ['colors' => $chartColors]);
<!-- In Blade template -->
@foreach ($colors as $color)
<div style="background-color: {{ $color }}; width: 50px; height: 50px;"></div>
@endforeach
Single Color Generation:
// Hex format (default)
$hexColor = RandomColor::one();
// RGB format for CSS
$rgbColor = RandomColor::one(['format' => 'rgbCss']);
// HSL format for design tools
$hslColor = RandomColor::one(['format' => 'hsl']);
Color Palettes:
// Generate 10 green shades
$palette = RandomColor::many(10, [
'hue' => 'green',
'format' => 'hex'
]);
// Generate colors from multiple hues
$mixedPalette = RandomColor::many(8, [
'hue' => ['red', 'blue', 'yellow'],
'luminosity' => 'random'
]);
Accessibility-Focused Colors:
// High-contrast colors for dark mode
$darkColors = RandomColor::many(3, [
'luminosity' => 'dark',
'format' => 'hex',
'hue' => 'random'
]);
// Ensure sufficient contrast (pair with a validation library)
Transparent Colors:
// RGBA for overlays
$rgbaColor = RandomColor::one([
'format' => 'rgbaCss',
'alpha' => 0.5 // 50% transparency
]);
// Hex with alpha
$hexaColor = RandomColor::one([
'format' => 'hexa',
'alpha' => 0.7
]);
Laravel Service Integration:
// app/Services/ColorService.php
class ColorService {
public function generatePalette(int $count, array $options = []): array {
return RandomColor::many($count, $options);
}
public function generateSingle(array $options = []): string {
return RandomColor::one($options);
}
}
Register in AppServiceProvider:
public function register() {
$this->app->singleton(ColorService::class, function ($app) {
return new ColorService();
});
}
Blade Directives:
// app/Providers/BladeServiceProvider.php
Blade::directive('color', function ($expression) {
return "<?php echo \\Colors\\RandomColor::one({$expression}); ?>";
});
Usage in Blade:
<div style="background-color: @color(['hue' => 'blue'])"></div>
Eloquent Model Accessors:
// app/Models/User.php
public function getColorAttribute() {
return RandomColor::one([
'hue' => $this->preferred_color ?? 'random',
'format' => 'hex'
]);
}
Usage:
$user->color; // e.g., "#3498db"
API Responses:
// In a controller
return response()->json([
'data' => [
'color' => RandomColor::one(['format' => 'hex']),
'rgba' => RandomColor::one(['format' => 'rgbaCss'])
]
]);
Consistent Formatting:
hex) for API responses to avoid client-side parsing.format option to match frontend requirements (e.g., rgbCss for Tailwind).Caching:
// Cache colors for 1 hour to reduce generation overhead
$colors = Cache::remember("color_palette_{$userId}", now()->addHours(1), function () use ($userId) {
return RandomColor::many(5, [
'hue' => 'blue',
'format' => 'hex'
]);
});
Seeded Randomness:
// For reproducible colors (e.g., testing)
$color = RandomColor::one([
'prng' => function() {
return 42; // Fixed seed
},
'format' => 'hex'
]);
Validation:
spatie/color to validate generated colors for accessibility (e.g., WCAG contrast ratios).Frontend Sync:
<div data-color="{{ json_encode(RandomColor::one(['format' => 'rgbaCss'])) }}"></div>
PHP Version Warnings:
Alpha Channel Quirks:
hex).format when using alpha (e.g., rgbaCss or hexa).Hue Array Behavior:
hue is an array, one hue is selected randomly. Empty array = random hue.Luminosity "Random":
luminosity: 'random' may produce unexpected results (e.g., very dark colors).luminosity: 'light' or luminosity: 'bright' for predictable outputs.Color Repetition:
RandomColor::many() may generate duplicates, especially with limited hues.$uniqueColors = array_unique(RandomColor::many(10, ['hue' => 'blue']));
Unexpected Colors:
prng option or ensure no custom logic is overriding defaults.Format Mismatches:
format option matches frontend expectations (e.g., rgbCss vs. rgb).var_dump() to inspect the output format:
var_dump(RandomColor::one(['format' => 'rgbCss'])); // string(12) "rgb(123,45,67)"
Performance Issues:
microtime() if generating colors in loops:
$start = microtime(true);
$colors = RandomColor::many(1000);
echo microtime(true) - $start; // Should be < 0.1s
random (0–360).random (0–How can I help you explore Laravel packages today?