import { Component } from '@angular/core';
import { WrColorPicker, WrColorPickerTrigger } from 'ngwr/color-picker';
@Component({ imports: [WrColorPicker, WrColorPickerTrigger] })
export class MyComponent {}Color Picker
Pick a colour inline, or from a button that opens the picker.
Installation
Import the class, and @use the entry point in your global styles: without it the component renders unstyled.
Basic
Bind the colour string with [(value)] or any Angular form.
<wr-color-picker [(value)]="color" />No alpha
[alpha]="false" hides the alpha slider and drops alpha from the value.
<wr-color-picker [(value)]="color" [alpha]="false" />Swatches
[swatches] adds a row of preset colours.
<wr-color-picker
[(value)]="color"
[swatches]="['#3969e2', '#f51c6a', '#00a400', '#ffba00', '#fa383e', '#cbd5e1', '#8594a4', '#0f172a']"
/>Popover trigger
[wrColorPickerTrigger] on any button opens the picker in an overlay.
<button wrColorPickerTrigger
[(value)]="color"
[swatches]="palette">
<span class="swatch" [style.background]="color()"></span>
{{ color() }}
</button>Color utilities
Parse and convert colours without a picker.
import { parseHex, toHex, rgbToHsl } from 'ngwr/color-picker';
const rgb = parseHex('#3969e2'); // { r: 57, g: 105, b: 226, a: 1 }
const hsl = rgbToHsl(rgb!); // { h: 222.96, s: 0.744, l: 0.555, a: 1 }
const back = toHex(rgb!, true); // '#3969e2ff'Template reference
Export the trigger as wrColorPickerTrigger to open it or read its value from elsewhere.
<button type="button" wrColorPickerTrigger [(value)]="brand" #swatch="wrColorPickerTrigger">
Brand colour
</button>
<!-- Anywhere else in the same template -->
<wr-btn (click)="swatch.open()">Pick a colour</wr-btn>
<span [style.color]="swatch.value()">{{ swatch.value() }}</span><wr-color-picker>
| Name | Description | Type | Default |
|---|---|---|---|
value | The colour, written in the format notation. Any notation is read on the way in. | string | '' |
alpha | Show the alpha slider and keep alpha in the value. | boolean | true |
format | The notation value is written in: hex, rgba or hsla. | WrColorFormat | 'hex' |
disabled | Block interaction. | boolean | false |
readonly | Refuse changes. Every part stays focusable and the colour still submits. | boolean | false |
swatches | Preset hex colours shown below the inputs. | readonly string[] | [] |
(touch) | Emitted on blur, so a bound signal-forms field can mark itself touched. | void | — |
[wrColorPickerTrigger]
| Name | Description | Type | Default |
|---|---|---|---|
value | Two-way bindable colour string, in whatever format names. | string | '' |
alpha | Forwarded to the inner picker. | boolean | true |
format | Forwarded to the inner picker. | WrColorFormat | 'hex' |
swatches | Forwarded to the inner picker. | readonly string[] | [] |
disabled | Disable the trigger itself. | boolean | false |
panelClass | Class(es) on the overlay pane. | WrClassInput | null |
[wrColorPickerTrigger] events
| Name | Description | Type | Default |
|---|---|---|---|
(opened) | Fires after the picker overlay opens. | void | — |
(closed) | Fires after the picker overlay closes. | void | — |
Exported utilities
| Name | Description | Type | Default |
|---|---|---|---|
parseHex(s) | Parse a 3/4/6/8-digit hex string into WrRgb. Returns null on invalid input. | (s: string) => WrRgb | null | — |
toHex(rgb, withAlpha?) | Format WrRgb as a hex string with leading #. | (rgb: WrRgb, withAlpha?: boolean) => string | — |
rgbToHsv / hsvToRgb | Convert between RGB and HSV, keeping alpha. | function | — |
rgbToHsl / hslToRgb | Convert between RGB and HSL, keeping alpha. | function | — |
CSS variables
Set these on the component's selector or a wrapper, not on :root: the defaults are declared on ngwr/color-picker's own element.
| Variable | Default | Declared on |
|---|---|---|
--wr-color-picker-bg | var(--wr-color-surface) | .wr-color-picker |
--wr-color-picker-border | var(--wr-border-base) | .wr-color-picker |
--wr-color-picker-chrome-gap | var(--wr-space-sm) | .wr-color-picker__chrome |
--wr-color-picker-color | rgb(0 0 0) | .wr-color-picker |
--wr-color-picker-field-border | var(--wr-border-base) | .wr-color-picker__field |
--wr-color-picker-field-font-family | var(--wr-font-family-mono, monospace) | .wr-color-picker__field |
--wr-color-picker-field-font-size | var(--wr-text-xs) | .wr-color-picker__field +1 variant override |
--wr-color-picker-field-gap | var(--wr-space-xs) | .wr-color-picker__field |
--wr-color-picker-field-label-font-weight | var(--wr-font-weight-semibold) | .wr-color-picker__field-label |
--wr-color-picker-field-line-height | var(--wr-text-xs-leading) | .wr-color-picker__field |
--wr-color-picker-field-padding-x | var(--wr-space-sm) | .wr-color-picker__field |
--wr-color-picker-field-padding-y | var(--wr-space-xs) | .wr-color-picker__field |
--wr-color-picker-field-radius | var(--wr-border-radius-sm) | .wr-color-picker__field |
--wr-color-picker-font-family | var(--wr-font-family-base) | .wr-color-picker |
--wr-color-picker-gap | var(--wr-space-sm) | .wr-color-picker |
--wr-color-picker-hue | hsl(0, 100%, 50%) | .wr-color-picker |
--wr-color-picker-inputs-gap | var(--wr-space-xs) | .wr-color-picker__inputs |
--wr-color-picker-padding | var(--wr-space-md) | .wr-color-picker |
--wr-color-picker-radius | var(--wr-border-radius-base) | .wr-color-picker |
--wr-color-picker-slider-height | 0.75rem | .wr-color-picker |
--wr-color-picker-sv-height | 10rem | .wr-color-picker |
--wr-color-picker-swatch-radius | var(--wr-border-radius-sm) | .wr-color-picker__swatch |
--wr-color-picker-swatches-gap | var(--wr-space-xs) | .wr-color-picker__swatches |
--wr-color-picker-tabs-font-size | var(--wr-text-xs) | .wr-color-picker__tabs |
--wr-color-picker-thumb-size | 0.875rem | .wr-color-picker |
--wr-color-picker-width | 17rem | .wr-color-picker |
--wr-preview-size | 1.75rem | .wr-color-picker__preview |
Dependencies
See also
Validator hexColorCheck a hex colour such as #abc or #1a2b3c.Guide FormsBind ngwr controls to any Angular form and show their errors.Component Form FieldWraps a form control with a label, a hint and its error messages.Util resolveCssColorResolves a CSS colour, such as a var() or light-dark(), to a value a canvas can paint with.Component FormA plain layout wrapper for a label, a control and its error.Guide OverlayStyle ngwr's floating panels and control where focus goes.Component SegmentedPick one option from a row of buttons.