app.module.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { ColorPickerModule } from 'ngx-color-picker'; import { AppComponent } from './app.component'; @NgModule({ bootstrap: [ AppComponent ], declarations: [ AppComponent ], imports: [ BrowserModule, ColorPickerModule ] }) export class AppModule {} |
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import { Component, ViewContainerRef } from '@angular/core'; import { ColorPickerService, Cmyk } from 'ngx-color-picker'; @Component({ selector: 'my-app', moduleId: 'src/app/app.component', templateUrl: 'app.component.html', styleUrls: [ 'app.component.css' ] }) export class AppComponent { public toggle: boolean = false; public rgbaText: string = 'rgba(165, 26, 214, 0.2)'; public arrayColors: any = { color1: '#2883e9', color2: '#e920e9', color3: 'rgb(255,245,0)', color4: 'rgb(236,64,64)', color5: 'rgba(45,208,45,1)' }; public selectedColor: string = 'color1'; public color1: string = '#2889e9'; public color2: string = '#e920e9'; public color3: string = '#fff500'; public color4: string = 'rgb(236,64,64)'; public color5: string = 'rgba(45,208,45,1)'; public color6: string = '#1973c0'; public color7: string = '#f200bd'; public color8: string = '#a8ff00'; public color9: string = '#278ce2'; public color10: string = '#0a6211'; public color11: string = '#f2ff00'; public color12: string = '#f200bd'; public color13: string = 'rgba(0,255,0,0.5)'; public color14: string = 'rgb(0,255,255)'; public color15: string = 'rgb(255,0,0)'; public color16: string = '#a51ad633'; public color17: string = '#666666'; public color18: string = '#ff0000'; public cmykValue: string = ''; public cmykColor: Cmyk = new Cmyk(0, 0, 0, 0); constructor(public vcRef: ViewContainerRef, private cpService: ColorPickerService) {} public onEventLog(event: string, data: any): void { console.log(event, data); } public onChangeColorCmyk(color: string): Cmyk { const hsva = this.cpService.stringToHsva(color); if (hsva) { const rgba = this.cpService.hsvaToRgba(hsva); return this.cpService.rgbaToCmyk(rgba); } return new Cmyk(0, 0, 0, 0); } public onChangeColorHex8(color: string): string { const hsva = this.cpService.stringToHsva(color, true); if (hsva) { return this.cpService.outputFormat(hsva, 'rgba', null); } return ''; } } |
app.component.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
<div class="container"> <h1>Angular Color Picker Directive</h1> <h4>A Color Picker Directive for Angular with no dependencies.</h4> <h4><b>based on angular2-color-picker by Alberto Pujante</b></h4> <hr> <div class="row"> <div class="col-md-5"> <input [style.background]="color1" [(colorPicker)]="color1" (colorPickerOpen)="onEventLog('colorPickerOpen', $event)" (colorPickerClose)="onEventLog('colorPickerClose', $event)" (cpInputChange)="onEventLog('cpInputChange', $event)" (cpSliderDragStart)="onEventLog('cpSliderDragStart', $event)" (cpSliderDragEnd)="onEventLog('cpSliderDragEnd', $event)"/> </div> <div class="col-md-7"> <p>Usage:</p> <pre> <input [(colorPicker)]="color" [style.background]="color"/> </pre> <p>Or:</p> <pre> <input [style.background]="color" [colorPicker]="color" (colorPickerChange)="color=$event"/> </pre> </div> </div> <hr> <div class="row"> <div class="col-md-5"> <input [style.background]="color17" [(colorPicker)]="color17" [cpColorMode]="'grayscale'" (colorPickerOpen)="onEventLog('colorPickerOpen', $event)" (colorPickerClose)="onEventLog('colorPickerClose', $event)" (cpInputChange)="onEventLog('cpInputChange', $event)" (cpSliderDragStart)="onEventLog('cpSliderDragStart', $event)" (cpSliderDragEnd)="onEventLog('cpSliderDragEnd', $event)"/> </div> <div class="col-md-7"> <p>Grayscale color mode:</p> <pre> <input [(colorPicker)]="color" [cpColorMode]="'grayscale'" [style.background]="color"/> </pre> </div> </div> <hr> <div class="row"> <div class="col-md-5"> <input [value]="color2" [style.background]="color2" [(colorPicker)]="color2"/> </div> <div class="col-md-7"> <p>Show the color in the input field:</p> <pre> <input [value]="color" [style.background]="color" [(colorPicker)]="color"/> </pre> </div> </div> <hr> <div class="row"> <div class="col-md-5"> <input [value]="color3" [style.background]="color3" [cpOutputFormat]="'rgba'" [(colorPicker)]="color3"/> <br><br> <input [value]="color4" [style.background]="color4" [cpOutputFormat]="'hsla'" [(colorPicker)]="color4"/> </div> <div class="col-md-7"> <p>Output format:</p> <pre> <input [value]="color" [style.background]="color" [cpOutputFormat]="'rgba'" [(colorPicker)]="color"/> </pre> </div> </div> <hr> <div class="row" > <div class="col-md-5" > <input [value]="color5" [style.background]="color5" [cpPosition]="'bottom'" [(colorPicker)]="color5"/> </div> <div class="col-md-7"> <p>Changing dialog position:</p> <pre> <input [value]="color" [style.background]="color" [cpPosition]="'bottom'" [(colorPicker)]="color"/> </pre> </div> </div> <hr> <div class="row" > <div class="col-md-5" > <span class="change-me" [style.color]="color6" [cpPosition]="'bottom'" [cpPositionOffset]="'50%'" [cpPositionRelativeToArrow]="true" [(colorPicker)]="color6">Change me!</span> </div> <div class="col-md-7"> <p>You can introduce a offset of the color picker relative to the html element:</p> <pre> <span [style.color]="color" [cpPosition]="'bottom'" [cpPositionOffset]="'50%'" [cpPositionRelativeToArrow]="true" [(colorPicker)]="color">Change me!</span> </pre> </div> </div> <hr> <div class="row" > <div class="col-md-5" > <input [value]="color7" [style.background]="color7" [cpCancelButton]="true" [(colorPicker)]="color7"/> </div> <div class="col-md-7"> <p>Show cancel button:</p> <pre> <input [value]="color" [style.background]="color" [cpCancelButton]="true" [(colorPicker)]="color"/> </pre> </div> </div> <hr> <div class="row" > <div class="col-md-5" > <input [value]="color8" [style.background]="color8" [cpCancelButton]="true" [cpCancelButtonClass]= "'btn btn-primary btn-xs'" [(colorPicker)]="color8"/> </div> <div class="col-md-7"> <p>Change cancel button class, in this example we are using a bootstrap button:</p> <pre> <input [value]="color" [style.background]="color" [cpCancelButton]="true" [cpCancelButtonClass]= "'btn btn-primary btn-xs'" [(colorPicker)]="color"/> </pre> </div> </div> <hr> <div class="row" > <div class="col-md-5" > <input [value]="color9" [style.background]="color9" [cpOKButton]="true" [cpSaveClickOutside]="false" [cpOKButtonClass]= "'btn btn-primary btn-xs'"[(colorPicker)]="color9"/> </div> <div class="col-md-7"> <p>Show OK button:</p> <pre> <input [value]="color" [style.background]="color" [cpOKButton]="true" [cpSaveClickOutside]="false" [cpOKButtonClass]= "'btn btn-primary btn-xs'" [(colorPicker)]="color"/> </pre> </div> </div> <hr> <div class="row"> <div class="col-md-5"> <input [style.background]="color10" [colorPicker]="color10" [cpCmykEnabled]="true" (cpCmykColorChange)="cmykValue=$event" (colorPickerChange)="cmykColor=onChangeColorCmyk($event);color10=$event"/> <div class="clearfix"></div> <div>{{cmykValue}}</div> <div> <span class="cmyk-text" style="color: rgb(0,255,255);" [style.font-size.px]="100 * cmykColor.c">C</span> <span class="cmyk-text" style="color: rgb(255,0,255)" [style.font-size.px]="100 * cmykColor.m">M</span> </div> <div class="clearfix"></div> <div> <span class="cmyk-text" style="color: rgb(255,255,0)" [style.font-size.px]="100 * cmykColor.y">Y</span> <span class="cmyk-text" [style.font-size.px]="100 * cmykColor.k">K</span> </div> <div class="clearfix"></div> </div> <div class="col-md-7"> <p>Change event color:</p> <pre> <input [style.background]="color" [colorPicker]="color" [cpCmykEnabled]="true" (cpCmykColorChange)="cmykValue=$event" (colorPickerChange)="cmykColor=onChangeColorCmyk($event);color=$event"/> <span [style.font-size.px]="100 * cmykColor.c"/>C</span/> <span [style.font-size.px]="100 * cmykColor.m"/>M</span/> <span [style.font-size.px]="100 * cmykColor.y"/>Y</span/> <span [style.font-size.px]="100 * cmykColor.k"/>K</span/> </pre> </div> </div> <hr> <div class="row"> <div class="col-md-5"> <input [style.background]="color11" [cpPresetColors]="['#fff', '#000', '#2889e9', '#e920e9', '#fff500', 'rgb(236,64,64)']" [(colorPicker)]="color11"/> </div> <div class="col-md-7"> <p>With preset colors:</p> <pre> <input [style.background]="color" [cpPresetColors]="['#fff', '#000', '#2889e9', '#e920e9', '#fff500', 'rgb(236,64,64)']" [(colorPicker)]="color"/> </pre> </div> </div> <hr> <div class="row"> <div class="col-md-5"> <input [style.background]="color13" [cpAlphaChannel]="'always'" [cpOutputFormat]="'rgba'" [cpPresetColors]="['#fff', '#2889e9']" [cpAddColorButton]="true" [(colorPicker)]="color13"/> </div> <div class="col-md-7"> <p>Add and remove preset colors:</p> <pre> <input [style.background]="color" [cpAlphaChannel]="'always'" [cpOutputFormat]="'rgba'" [cpPresetColors]="['#fff', '#2889e9']" [cpAddColorButton]="true" [(colorPicker)]="color"/> </pre> </div> </div> <hr> <div class="row"> <div class="col-md-5"> <input #ignoredInput [style.background]="color12" [cpFallbackColor]="'#f200bd'" [cpIgnoredElements]="[ignoredButton, ignoredInput]" [(cpToggle)]="toggle" [(colorPicker)]="color12"/> <div class="clearfix"></div> <button #ignoredButton class="btn btn-primary" (click)="toggle=!toggle;">Toggle</button> <br><br> <div><b>Toggle status: {{toggle}}</b></div> </div> <div class="col-md-7"> <p>Use cpToggle with cpIgnoredElements:</p> <pre> <input #ignoredInput [style.background]="color" [cpIgnoredElements]="[ignoredButton, ignoredInput]" [(cpToggle)]="toggle" [(colorPicker)]="color"/> <button #ignoredButton (click)="toggle=!toggle"></button> </pre> </div> </div> <hr> <div class="row"> <div class="col-md-5"> <input [value]="color14" [style.background]="color14" [cpAlphaChannel]="'always'" [cpOutputFormat]="'rgba'" [(colorPicker)]="color14"/> <br> <input [value]="color13" [style.background]="color13" [cpAlphaChannel]="'disabled'" [cpOutputFormat]="'rgba'" [(colorPicker)]="color13"/> <br> <input [value]="color16" [style.background]="rgbaText" [cpAlphaChannel]="'always'" [cpOutputFormat]="'hex'" [colorPicker]="color16" (colorPickerChange)="rgbaText=onChangeColorHex8($event);color16=$event"/> <br> <input [value]="color15" [style.background]="color15" [cpAlphaChannel]="'forced'" [cpOutputFormat]="'hex'" [(colorPicker)]="color15"/> </div> <div class="col-md-7"> <p>Change alpha channel behaviour:</p> <pre> <input [value]="color" [style.background]="color" [cpAlphaChannel]="'always'" [cpOutputFormat]="'rgba'" [(colorPicker)]="color"/> <input [value]="color" [style.background]="color13" [cpAlphaChannel]="'disabled'" [cpOutputFormat]="'rgba'" [(colorPicker)]="color"/> <input [value]="color" [style.background]="rgbaText" [cpAlphaChannel]="'always'" [cpOutputFormat]="'hex'" [colorPicker]="color" (colorPickerChange)="rgbaText=onChangeColorHex8($event);color16=$event"/> <input [value]="color" [style.background]="color" [cpAlphaChannel]="'forced'" [cpOutputFormat]="'hex'" [(colorPicker)]="color"/> </pre> </div> </div> <hr> <div class="row" style="height: 320px"> <div class="col-md-3"> <span [style.background]="arrayColors[selectedColor]" [cpToggle]="true" [cpDialogDisplay]="'inline'" [cpCancelButton]="true" [cpCancelButtonClass]= "'btn btn-primary btn-xs'" [(colorPicker)]="arrayColors[selectedColor]"></span> </div> <div class="col-md-2"> <div class="color-box" [style.background]="arrayColors['color1']" (click)="selectedColor='color1'"></div> <div class="color-box" [style.background]="arrayColors['color2']" (click)="selectedColor='color2'"></div> <div class="color-box" [style.background]="arrayColors['color3']" (click)="selectedColor='color3'"></div> <div class="color-box" [style.background]="arrayColors['color4']" (click)="selectedColor='color4'"></div> <div class="color-box" [style.background]="arrayColors['color5']" (click)="selectedColor='color5'"></div> </div> <div class="col-md-7"> <p>Show the dialog permanently: </p> <pre> <span [style.background]="arrayColors[selectedColor]" [cpToggle]="true" [cpDialogDisplay]="'inline'" [cpCancelButton]="true" [(colorPicker)]="arrayColors[selectedColor]"></span> <div [style.background]="arrayColors['color1']" (click)="selectedColor='color1'"></div> <div [style.background]="arrayColors['color2']" (click)="selectedColor='color2'"></div> </pre> </div> </div> <hr><br> <div class="row"> <div class="col-md-12"> <table class="table"> <thead> <tr> <th>Options</th> <th>Values (default values in bold)</th> </tr> </thead> <tbody> <tr> <td>cpOutputFormat</td> <td> <b>'auto'</b>, 'hex', 'rgba', 'hsla' </td> </tr> <tr> <td>cpPosition</td> <td> <b>'right'</b>, 'left', 'top', 'bottom' </td> </tr> <tr> <td>cpPositionOffset</td> <td> <b>'0%'</b><br> Dialog offset (percent) relative to the element that contains the directive. </td> </tr> <tr> <td>cpPositionRelativeToArrow</td> <td> <b>false</b>, true <br> Dialog position is calculated relative to the dialog (false) or relative to the dialog arrow (true). </td> </tr> <tr> <td>cpWidth</td> <td> <b>'230px'</b><br> Use this option to set color picker dialog width (pixels). </td> </tr> <tr> <td>cpHeight</td> <td> <b>'auto'</b><br> Use this option to force color picker dialog height (pixels). </td> </tr> <tr> <td>cpSaveClickOutside</td> <td> <b>true</b>, false<br> If true the initial color is restored when user clicks outside. </td> </tr> <tr> <td>cpOKButton</td> <td> <b>false</b>, true<br> Shows the Ok button. Saves the selected color. </td> </tr> <tr> <td>cpOKButtonText</td> <td> <b>'OK'</b> </td> </tr> <tr> <td>cpOKButtonClass</td> <td> Class to customize the OK button. </td> </tr> <tr> <td>cpCancelButton</td> <td> <b>false</b>, true<br> Shows the Cancel button. Cancel the selected color. </td> </tr> <tr> <td>cpCancelButtonText</td> <td> <b>'Cancel'</b> </td> </tr> <tr> <td>cpCancelButtonClass</td> <td> Class to customize the Cancel button. </td> </tr> <tr> <td>cpFallbackColor</td> <td> <b>'#fff'</b><br> Is used when the color is not well-formed or not defined. </td> </tr> <tr> <td>cpPresetLabel</td> <td> <b>'Preset colors'</b><br> Label for preset colors if any provided used. </td> </tr> <tr> <td>cpPresetColors</td> <td> <b>[]</b><br> Array of preset colors to show in the color picker dialog. </td> </tr> <tr> <td>cpToggle</td> <td> <b>false</b>, true<br> Input/ouput to open/close the color picker. </td> </tr> <tr> <td>cpIgnoredElements</td> <td> <b>[]</b><br> Array of HTML elements that will be ignored by the color picker when they are clicked. </td> </tr> <tr> <td>cpDialogDisplay</td> <td> <b>'popup'</b>, 'inline'<br> popup: dialog is showed when user clicks in the directive.<br> inline: dialog is showed permanently. You can show/hide the dialog with cpToggle.<br> </td> </tr> <tr> <td>cpDisableInput</td> <td> <b>false</b>, true<br> Disables / hides the color input field from the dialog.<br> </td> </tr> <tr> <td>cpAlphaChannel</td> <td> <b>'enabled'</b>, 'disabled', 'always', 'forced'<br> enabled: alpha channel is not included for hexadecimal (hex6) values or for values without alpha (alpha = 1).<br> disabled: alpha channel is completely disabled.<br> always: alpha channel is included for hexadecimal (hex6) values and values without alpha (alpha = 1).<br> forced: alpha channel field is added for hexadecimal (hex6) values.<br> </td> </tr> <tr> <td>cpCmykEnabled</td> <td> <b>false</b>, true<br> Enables CMYK color input and selected CMYK color event sending on color change.<br> </td> </tr> <tr> <td>cpUseRootViewContainer</td> <td> <b>false</b>, true<br> Create dialog component in the root view container instead the elements view container.<br> </td> </tr> <tr> <td>cpAddColorButton</td> <td> <b>false</b>, true<br> Add or remove colors into your preset panel. The [cpPresetColors] is needed<br> </td> </tr> <tr> <td>cpAddColorButtonText</td> <td> <b>'Add color'</b> </td> </tr> <tr> <td>cpAddColorButtonClass</td> <td> Class to customize the add color button. </td> </tr> <tr> <td>cpRemoveColorButtonClass</td> <td> Class to customize the remove color button. </td> </tr> <tr> <td>cpMaxPresetColorsLength</td> <td> <b>8</b> (number)<br> Use this option to set the max colors allowed into preset panel. </td> </tr> <tr> <td>cpPresetEmptyMessage</td> <td> <b>'No colors added'</b><br> Message for empty colors if any provided used. </td> </tr> <tr> <td>cpPresetEmptyMessageClass</td> <td> Class to customize the empty colors message. </td> </tr> </tbody> </table> </div> </div> <hr><br> <div class="row"> <div class="col-md-12"> <table class="table"> <thead> <tr> <th>Events</th> <th>Description (data format in bold)</th> </tr> </thead> <tbody> <tr> <td>colorPickerChange</td> <td> Changed color value, send when color is changed. <b>(value: string)</b> </td> </tr> <tr> <td>colorPickerSelect</td> <td> Selected color value, send when user presses the OK button. <b>(value: string)</b> </td> </tr> <tr> <td>cpToggleChange</td> <td> Status of the dialog, send when dialog is opened / closed. <b>(open: boolean)</b> </td> </tr> <tr> <td>cpInputChange</td> <td> Input name and its value, send when user changes color through inputs. <b>({{ '{' }}input: string, value: string{{ '}' }})</b> </td> </tr> <tr> <td>cpSliderChange</td> <td> Slider name and its value, send when user changes color through slider. <b>({{ '{' }}slider: string, value: Object{{ '}' }})</b> </td> </tr> <tr> <td>cpCmykColorChange</td> <td> CMYK color value, send when on color change if cpCmykEnabled is true. <b>(value: string)</b> </td> </tr> <tr> <td>cpPresetColorsChange</td> <td> Preset colors value, send when Add Color button is pressed. <b>(value: array)</b> </td> </tr> </tbody> </table> </div> </div> <hr> <br> </div> |