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.

import { Component } from '@angular/core';
import { WrColorPicker, WrColorPickerTrigger } from 'ngwr/color-picker';

@Component({ imports: [WrColorPicker, WrColorPickerTrigger] })
export class MyComponent {}

Basic

Bind the colour string with [(value)] or any Angular form.

<wr-color-picker [(value)]="color" />
Value: #3969e2

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>

NameDescriptionTypeDefault
valueThe colour, written in the format notation. Any notation is read on the way in.string''
alphaShow the alpha slider and keep alpha in the value.booleantrue
formatThe notation value is written in: hex, rgba or hsla.WrColorFormat'hex'
disabledBlock interaction.booleanfalse
readonlyRefuse changes. Every part stays focusable and the colour still submits.booleanfalse
swatchesPreset hex colours shown below the inputs.readonly string[][]
(touch)Emitted on blur, so a bound signal-forms field can mark itself touched.void—

[wrColorPickerTrigger]

NameDescriptionTypeDefault
valueTwo-way bindable colour string, in whatever format names.string''
alphaForwarded to the inner picker.booleantrue
formatForwarded to the inner picker.WrColorFormat'hex'
swatchesForwarded to the inner picker.readonly string[][]
disabledDisable the trigger itself.booleanfalse
panelClassClass(es) on the overlay pane.WrClassInputnull

[wrColorPickerTrigger] events

NameDescriptionTypeDefault
(opened)Fires after the picker overlay opens.void—
(closed)Fires after the picker overlay closes.void—

Exported utilities

NameDescriptionTypeDefault
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 / hsvToRgbConvert between RGB and HSV, keeping alpha.function—
rgbToHsl / hslToRgbConvert 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.

VariableDefaultDeclared on
--wr-color-picker-bgvar(--wr-color-surface).wr-color-picker
--wr-color-picker-bordervar(--wr-border-base).wr-color-picker
--wr-color-picker-chrome-gapvar(--wr-space-sm).wr-color-picker__chrome
--wr-color-picker-colorrgb(0 0 0).wr-color-picker
--wr-color-picker-field-bordervar(--wr-border-base).wr-color-picker__field
--wr-color-picker-field-font-familyvar(--wr-font-family-mono, monospace).wr-color-picker__field
--wr-color-picker-field-font-sizevar(--wr-text-xs).wr-color-picker__field +1 variant override
--wr-color-picker-field-gapvar(--wr-space-xs).wr-color-picker__field
--wr-color-picker-field-label-font-weightvar(--wr-font-weight-semibold).wr-color-picker__field-label
--wr-color-picker-field-line-heightvar(--wr-text-xs-leading).wr-color-picker__field
--wr-color-picker-field-padding-xvar(--wr-space-sm).wr-color-picker__field
--wr-color-picker-field-padding-yvar(--wr-space-xs).wr-color-picker__field
--wr-color-picker-field-radiusvar(--wr-border-radius-sm).wr-color-picker__field
--wr-color-picker-font-familyvar(--wr-font-family-base).wr-color-picker
--wr-color-picker-gapvar(--wr-space-sm).wr-color-picker
--wr-color-picker-huehsl(0, 100%, 50%).wr-color-picker
--wr-color-picker-inputs-gapvar(--wr-space-xs).wr-color-picker__inputs
--wr-color-picker-paddingvar(--wr-space-md).wr-color-picker
--wr-color-picker-radiusvar(--wr-border-radius-base).wr-color-picker
--wr-color-picker-slider-height0.75rem.wr-color-picker
--wr-color-picker-sv-height10rem.wr-color-picker
--wr-color-picker-swatch-radiusvar(--wr-border-radius-sm).wr-color-picker__swatch
--wr-color-picker-swatches-gapvar(--wr-space-xs).wr-color-picker__swatches
--wr-color-picker-tabs-font-sizevar(--wr-text-xs).wr-color-picker__tabs
--wr-color-picker-thumb-size0.875rem.wr-color-picker
--wr-color-picker-width17rem.wr-color-picker
--wr-preview-size1.75rem.wr-color-picker__preview

Dependencies

Angular CDKoverlay, portal
Built onForm, Overlay, Segmented

See also