# Color

The `color` field supports multiple color notations, a color picker, pre-defined colors, custom color names, and transparency.

****

- Read more: <https://getkirby.com/docs/guide/blueprints/fields>
- Source: <https://github.com/getkirby/kirby/tree/5.5.3/config/fields/color.php>

****

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/323b926b1a-1717846665/color.png?width=1416&amp;height=328&amp;enlarge=0"><img alt="" class="rounded" height="214" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/323b926b1a-1717846665/color.png?width=924&amp;height=214&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/323b926b1a-1717846665/color.png?width=462&amp;height=107&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/323b926b1a-1717846665/color.png?width=690&amp;height=160&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/323b926b1a-1717846665/color.png?width=924&amp;height=214&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/323b926b1a-1717846665/color.png?width=1380&amp;height=320&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/323b926b1a-1717846665/color.png?width=1416&amp;height=328&amp;enlarge=0 1848w" width="924"></a></figure>

## Field properties



<div class="table">
	<table class="parameters">
		<thead>
			<tr>
				<th>Name</th>
				<th>Type</th>
				<th>Default</th>

								<th>Description</th>
							</tr>
		</thead>
		<tbody>
						<tr>
				<td>
					alpha				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>false</code>
									</td>

								<td>Whether to allow alpha transparency in the color</td>
							</tr>
						<tr>
				<td>
					autofocus				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Sets the focus on this field when the form loads. Only the first field with this label gets</td>
							</tr>
						<tr>
				<td>
					default				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Default value for the field, which will be used when a page/file/user is created</td>
							</tr>
						<tr>
				<td>
					disabled				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>If <code class="type type-bool">true</code>, the field is no longer editable and will not be saved</td>
							</tr>
						<tr>
				<td>
					format				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>'hex'</code>
									</td>

								<td>The CSS format (hex, rgb, hsl) to display and store the value</td>
							</tr>
						<tr>
				<td>
					help				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Optional help text below the field</td>
							</tr>
						<tr>
				<td>
					icon				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Optional icon that will be shown at the end of the field</td>
							</tr>
						<tr>
				<td>
					label				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>The field label can be set as string or associative array with translations</td>
							</tr>
						<tr>
				<td>
					mode				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>'picker'</code>
									</td>

								<td>Change mode to disable the color picker (<code class="type">input</code>) or to only show the <code class="type">options</code> as toggles</td>
							</tr>
						<tr>
				<td>
					options				</td>
				<td><code class="type type-array">array</code></td>

				<td data-label="Default:">
															<code>[ ]</code>
									</td>

								<td>List of colors that will be shown as buttons to directly select them</td>
							</tr>
						<tr>
				<td>
					placeholder				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Optional placeholder value that will be shown when the field is empty</td>
							</tr>
						<tr>
				<td>
					required				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>If <code class="type type-bool">true</code>, the field has to be filled in correctly to be saved.</td>
							</tr>
						<tr>
				<td>
					translate				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>true</code>
									</td>

								<td>If <code class="type type-bool">false</code>, the field will be disabled in non-default languages and cannot be translated. This is only relevant in multi-language setups.</td>
							</tr>
						<tr>
				<td>
					when				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Conditions when the field will be shown (since 3.1.0)</td>
							</tr>
						<tr>
				<td>
					width				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>'1/1'</code>
									</td>

								<td>The width of the field in the field grid, e.g. <code class="type">1/1</code>, <code class="type">1/2</code>, <code class="type">1/3</code>, <code class="type">1/4</code>, <code class="type">2/3</code>, <code class="type">3/4</code></td>
							</tr>
					</tbody>
	</table>
</div>

## Usage

```yaml
color:
  type: color
  format: hsl # supports hex (default), rgb, hsl
```

### In templates/snippets

```php
<?= $page->color()->escape('css') ?>
```

## Alpha

Use the `alpha` option (default: false) to activate alpha transparency support:

```yaml
color:
  type: color
  alpha: true
```

## Modes

With the `mode` option you control which elements of the color field are available. Possible values:

| Option   | Description |
| ------   | ----------- |
|`picker`  | show everything (default)|
|`input`   | show only the input |
|`options` | show only options |

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/dd31d56478-1717846665/color-field-modes.png?width=473&amp;height=152&amp;enlarge=0"><img alt="" class="rounded" height="152" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/dd31d56478-1717846665/color-field-modes.png?width=473&amp;height=152&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/dd31d56478-1717846665/color-field-modes.png?width=462&amp;height=148&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/dd31d56478-1717846665/color-field-modes.png?width=473&amp;height=152&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/dd31d56478-1717846665/color-field-modes.png?width=473&amp;height=152&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/dd31d56478-1717846665/color-field-modes.png?width=473&amp;height=152&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/dd31d56478-1717846665/color-field-modes.png?width=473&amp;height=152&amp;enlarge=0 1848w" width="473"></a></figure>

## Options

### Pre-defined options

Allows adding a list of pre-defined colors that are shown as one-click options in the Panel.

```yaml
color:
  type: color
  options:
    - "#f8b195"
    - "#f67280"
    - "#c06c84"
    - "#6c5b7b"
    - "#355c7d"
```

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/ce818a4937-1717846665/color-field-options.png?width=882&amp;height=732&amp;enlarge=0"><img alt="" class="rounded" height="732" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/ce818a4937-1717846665/color-field-options.png?width=882&amp;height=732&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/ce818a4937-1717846665/color-field-options.png?width=462&amp;height=383&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/ce818a4937-1717846665/color-field-options.png?width=690&amp;height=573&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/ce818a4937-1717846665/color-field-options.png?width=882&amp;height=732&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/ce818a4937-1717846665/color-field-options.png?width=882&amp;height=732&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/ce818a4937-1717846665/color-field-options.png?width=882&amp;height=732&amp;enlarge=0 1848w" width="882"></a></figure>

If you name the colors, their name is shown in the Panel as well.

```yaml
color:
  type: color
  options:
    "#F8B195": "Sunny rays"
    "#F67280": "First-love blush"
    "#C06C84": "Cherry blossom"
    "#6C5B7B": "Morning gloom"
    "#355C7D": "Midnight rain"
```

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/f8a8903955-1717846665/color-field-names.png?width=856&amp;height=714&amp;enlarge=0"><img alt="" class="rounded" height="714" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/f8a8903955-1717846665/color-field-names.png?width=856&amp;height=714&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/f8a8903955-1717846665/color-field-names.png?width=462&amp;height=385&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/f8a8903955-1717846665/color-field-names.png?width=690&amp;height=576&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/f8a8903955-1717846665/color-field-names.png?width=856&amp;height=714&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/f8a8903955-1717846665/color-field-names.png?width=856&amp;height=714&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/color/f8a8903955-1717846665/color-field-names.png?width=856&amp;height=714&amp;enlarge=0 1848w" width="856"></a></figure>

### Dynamic options

Our options <a href="https://getkirby.com/docs/guide/blueprints/query-language">query syntax</a> offers a powerful way of adding automatically generated option lists to a color field.

```yaml
myColorField:
  type: color
  options:
    type: query
    query: kirby.option('my.colors')
```

The example above will get the color options from the `my.colors` config entry, which could look like:

```php "site/config/config.php"
// only values
return [
	'my' => [
		'colors' => [
			'#3e3e3e',
			'#aaa',
			'#ddd',
		]
	]
];

// values -> labels
return [
	'my' => [
		'colors' => [
			'#3e3e3e' => 'Color A',
			'#aaa'    => 'Color B',
			'#ddd'    => 'Color C',
		]
	]
];
```

You can start at the `site`, current `page`, `users` collection, or the `kirby` instance to run your query.

#### Custom value and label

To customize the stored value and displayed label, you can be more specific when defining the query: `text` and `value` can be defined with the help of our string template language to get exactly what you want as the result.

```yaml
myColorField:
  type: color
  options:
    type: query
    query: kirby.option('my.colors')
    text: "{{ item.name }}"
    value: "{{ item.hex }}"
```

```php "site/config/config.php"
return [
	'my' => [
		'colors' => [
			[
				'name' => 'Color A',
				'hex'   => '#3e3e3e',
			],
			[
				'name' => 'Color B',
				'hex'   => '#aaa',
			],
			[
				'name' => 'Color C',
				'hex'   => '#ddd',
			]
		]
	]
];
```

### Options via API

If the option queries are not enough or you need to pluck an external source for option data, you can use the API type.

```yaml
myColorField:
  type: color
  options:
    type: api
    url: https://your-options-api.com/colors.json
```

By default, the API type expects that the JSON endpoint returns an option array like this:

```js
{
	"#3e3e3e": "Color A",
	"#aaa": "Color B",
	"#ddd": "Color C"
}
```