1- import { Inject , Injectable , InjectionToken } from '@angular/core' ;
1+ import { Inject , Injectable , InjectionToken , Optional } from '@angular/core' ;
22import { rgb } from 'd3-color' ;
33import { ColorPalette } from './color-palette' ;
4- import { ColorPaletteType } from './color-palette-type' ;
54
6- export const BLUE_COLOR_PALETTE : InjectionToken < string [ ] > = new InjectionToken ( ColorPaletteType . Blue ) ;
7- export const RED_COLOR_PALETTE : InjectionToken < string [ ] > = new InjectionToken ( ColorPaletteType . Red ) ;
5+ export const DEFAULT_COLOR_PALETTE : InjectionToken < ColorPaletteDefinition > = new InjectionToken (
6+ 'Default Color Palette'
7+ ) ;
8+ export const ALTERNATE_COLOR_PALETTES : InjectionToken < ColorPaletteDefinition [ ] > = new InjectionToken (
9+ 'Alternate Color Palettes'
10+ ) ;
811
912export type ColorPaletteKey = string | symbol ;
1013
1114@Injectable ( { providedIn : 'root' } )
1215export class ColorService {
13- private static readonly DEFAULT_PALETTE_KEY : string = ColorPaletteType . Blue ;
16+ private static readonly DEFAULT_PALETTE_KEY : unique symbol = Symbol ( 'Default Palette Key' ) ;
1417
1518 private readonly registeredPalettes : Map < ColorPaletteKey , string [ ] > = new Map ( ) ;
1619
1720 public constructor (
18- @Inject ( BLUE_COLOR_PALETTE ) blueColorPalette : string [ ] ,
19- @Inject ( RED_COLOR_PALETTE ) redColorPalette : string [ ]
21+ @Inject ( DEFAULT_COLOR_PALETTE ) defaultPalette : ColorPaletteDefinition ,
22+ @Optional ( ) @ Inject ( ALTERNATE_COLOR_PALETTES ) alternatePalettes : ColorPaletteDefinition [ ] | null
2023 ) {
21- this . registerColorPalette ( ColorPaletteType . Blue , blueColorPalette ) ;
22- this . registerColorPalette ( ColorPaletteType . Red , redColorPalette ) ;
24+ this . registerColorPalette ( ColorService . DEFAULT_PALETTE_KEY , defaultPalette . colors ) ;
25+ [ defaultPalette , ...( alternatePalettes ?? [ ] ) ] . forEach ( palette =>
26+ this . registerColorPalette ( palette . key , palette . colors )
27+ ) ;
2328 }
2429
2530 public getColorPalette ( colorPalette : ColorPaletteKey | string [ ] = ColorService . DEFAULT_PALETTE_KEY ) : ColorPalette {
@@ -44,3 +49,8 @@ export class ColorService {
4449 return this . registeredPalettes . get ( key ) || this . registeredPalettes . get ( ColorService . DEFAULT_PALETTE_KEY ) ! ;
4550 }
4651}
52+
53+ export interface ColorPaletteDefinition {
54+ key : ColorPaletteKey ;
55+ colors : string [ ] ;
56+ }
0 commit comments