ColorPickerColorPicker is an input component to select a color.

ColorPicker - 图1

Documentation

Import

  1. import {ColorPickerModule} from 'primeng/colorpicker';
  2.  

Getting Started

ColorPicker uses ngModel directive to bind a value.

  1. <p-colorPicker [(ngModel)]="color"></p-colorPicker>
  2.  
  1. export class MyComponent {
  2. color: string;
  3. }
  4.  

Formats

Default color format to use in value binding is "hex" and other possible values are "rgb" and "hsb". Example below has 3 colorpickers having default values with different formats.

  1. <p-colorPicker [(ngModel)]="color1"></p-colorPicker>
  2. <p-colorPicker [(ngModel)]="color2" format="rgb"></p-colorPicker>
  3. <p-colorPicker [(ngModel)]="color3" format="hsb"></p-colorPicker>
  4.  
  1. export class MyComponent {
  2. color1: string;
  3. color2: any = {
  4. r: 100, g: 130, b: 150
  5. };
  6. color3: any = {
  7. h: 100, s: 50, b: 50
  8. };
  9. }
  10.  

Overlay and Inline

ColorPicker can be displayed as inline or as an overlay (default) using the "inline" property.

  1. <p-colorPicker [(ngModel)]="color1" ></p-colorPicker>
  2. <p-colorPicker [(ngModel)]="color2" [inline]="true"></p-colorPicker>
  3.  

Model Driven Forms

Colorpicker can be used in a model driven form as well.

  1. <p-colorPicker formControlName="color"></p-colorPicker>
  2.  

Animation Configuration

Transition of the open and hide animations can be customized using the showTransitionOptions and hideTransitionOptions properties, example below disables the animations altogether.

  1. <p-colorPicker [showTransitionOptions]="'0ms'" [hideTransitionOptions]="'0ms'" formControlName="color"></p-colorPicker>
  2.  

Properties

NameTypeDefaultDescription
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
inlinebooleanfalseWhether to display as an overlay or not.
formatstringhexFormat to use in value binding, supported formats are "hex", "rgb" and "hsb".
appendToanynullTarget element to attach the overlay, valid values are "body" or a local ng-template variable of another element.
tabindexnumbernullIndex of the element in tabbing order.
disabledbooleanfalseWhen present, it specifies that the component should be disabled.
inputIdstringnullIdentifier of the focus input to match a label defined for the dropdown.
baseZIndexnumber0Base zIndex value to use in layering.
autoZIndexbooleantrueWhether to automatically manage layering.
showTransitionOptionsstring225ms ease-outTransition options of the show animation.
hideTransitionOptionsstring195ms ease-inTransition options of the hide animation.

Events

NameParametersDescription
onChangeevent.originalEvent: Browser event event.value: Selected color Callback to invoke when a color is selected.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-colorpickerContainer element.
ui-colorpicker-overlayContainer element in overlay mode.
ui-colorpicker-preview Preview input in overlay mode.
ui-colorpicker-panelPanel element of the colorpicker.
ui-colorpicker-contentWrapper that contains color and hue sections.
ui-colorpicker-color-selectorColor selector container.
ui-colorpicker-colorColor element.
ui-colorpicker-color-handleHandle of the color element.
ui-colorpicker-hueHue slider.
ui-colorpicker-hue-handleHandle of the hue slider.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Inline</h3>
  2. <p-colorPicker [(ngModel)]="color1" [inline]="true"></p-colorPicker>
  3. <p style="margin-top:.5em">Selected Color: <span style="display:inline-block;width:32px;height:32px;vertical-align:middle" [style.backgroundColor]="color1"></span> {{color1}} </p>
  4. <h3>Overlay</h3>
  5. <p-colorPicker [(ngModel)]="color2"></p-colorPicker>
  6. <p style="margin-top:.5em">Selected Color: <span [ngStyle]="{'color':color2}">{{color2}}</span></p>
  7.  
  1. export class ColorPickerDemo {
  2. color1: string;
  3. color2: string = '#1976D2';
  4. }
  5.