webkernel/platform-forms
17-color-picker
On this page
title: Color picker
Introduction
The color picker component allows you to pick a color in a range of formats.
By default, the component uses HEX format:
use Webkernel\Platform\Forms\Components\ColorPicker;
ColorPicker::make('color')
<AutoScreenshot name="forms/fields/color-picker/simple" alt="Color picker" />
Clicking the color swatch opens a color picker panel where users can visually select a color:
<AutoScreenshot name="forms/fields/color-picker/panel" alt="Color picker with panel open" />
Setting the color format
While HEX format is used by default, you can choose which color format to use:
use Webkernel\Platform\Forms\Components\ColorPicker;
ColorPicker::make('hsl_color')
->hsl()
ColorPicker::make('rgb_color')
->rgb()
ColorPicker::make('rgba_color')
->rgba()
<AutoScreenshot name="forms/fields/color-picker/formats" alt="Color pickers with different format options" />
Color picker validation
You may use Laravel's validation rules to validate the values of the color picker:
use Webkernel\Platform\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+)?)\)$/')