#Introduction
The color picker component allows you to pick a color in a range of formats.
By default, the component uses HEX format:
use Filament\Forms\Components\ColorPicker;
ColorPicker::make('color')
Clicking the color swatch opens a color picker panel where users can visually select a color:
#Setting the color format
While HEX format is used by default, you can choose which color format to use:
use Filament\Forms\Components\ColorPicker;
ColorPicker::make('hsl_color')
->hsl()
ColorPicker::make('rgb_color')
->rgb()
ColorPicker::make('rgba_color')
->rgba()
#Customizing the loading placeholder
The color picker displays a loading placeholder while its JavaScript is loaded and initialized. You may customize it using loadingPlaceholder(), passing a Blade view or an Htmlable instance, such as an HtmlString containing trusted HTML:
use Filament\Forms\Components\ColorPicker;
ColorPicker::make('color')
->loadingPlaceholder(fn () => view('forms.color-picker-loading-placeholder'))
As well as allowing a static value, the loadingPlaceholder() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.
loadingPlaceholder() method also accepts a function to dynamically calculate it. You can inject various utilities into the function as parameters.-
Field
$componentFilament\Forms\ Components\ Field The current field component instance.
-
Get function
$getFilament\Schemas\ Components\ Utilities\ Get A function for retrieving values from the current form data. Validation is not run.
-
Livewire
$livewireLivewire\Component The Livewire component instance.
-
Eloquent model FQN
$model?string<Illuminate\Database\ Eloquent\ Model> The Eloquent model FQN for the current schema.
-
Operation
$operationstring The current operation being performed by the schema. Usually
create,edit, orview.-
Parent repeater item index
$parentRepeaterItemIndexint The zero-based index of the nearest containing repeater item or builder block, or
0when neither exists.-
Raw state
$rawStatemixed The current value of the field, before state casts were applied. Validation is not run.
-
Record
$recordIlluminate\Database\ Eloquent\ Model|array<string, mixed>|null The Eloquent record or custom data array for the current schema.
-
State
$statemixed The current value of the field. Validation is not run.
Keep your placeholder HTML decorative and noninteractive. Filament hides it from assistive technologies and supplies an accessible loading status in the surrounding wrapper. If the JavaScript fails to load or initialize, the placeholder remains visible.
To customize the placeholder for all color pickers, use ColorPicker::configureUsing() in a service provider's boot() method:
use Filament\Forms\Components\ColorPicker;
ColorPicker::configureUsing(function (ColorPicker $colorPicker): void {
$colorPicker->loadingPlaceholder(fn () => view('forms.color-picker-loading-placeholder'));
});
You may call loadingPlaceholder(null) on an individual color picker to restore the default loading placeholder.
#Color picker validation
You may use Laravel's validation rules to validate the values of the color picker:
use Filament\Forms\Components\ColorPicker;
ColorPicker::make('hex_color')
->regex('/^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})\b$/')
ColorPicker::make('hsl_color')
->hsl()
->regex('/^hsl\(\s*(\d+)\s*,\s*(\d*(?:\.\d+)?%)\s*,\s*(\d*(?:\.\d+)?%)\)$/')
ColorPicker::make('rgb_color')
->rgb()
->regex('/^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/')
ColorPicker::make('rgba_color')
->rgba()
->regex('/^rgba\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d*(?:\.\d+)?)\)$/')