import { Inject , Injectable , Optional } from '@angular/core' ; import { ColorSet , colorSets , COLOR_SET_TOKEN } from './color-sets/index' ; import { ThemeColor } from './theme-color' ; @ Injectable ( ) export class ColorService { /** Set the default theme to the Keppel colorset */ private _theme : Readonly < Theme > ; /** Allow the color set to be provided in a forRoot function otherwise set it to the Keppel theme by default */ constructor ( @ Optional ( ) @ Inject ( COLOR_SET_TOKEN ) private _colorSet ) { this . _theme = this . getTheme ( this . _colorSet ) ; } param colorName getColor ( colorName ) { const themeColor = this . _theme [ this . resolveColorName ( colorName ) ] ; if ( ! themeColor ) { throw new Error ( 'Color not found: ' + colorName ) ; } return new ThemeColor ( themeColor . getRed ( ) , themeColor . getGreen ( ) , themeColor . getBlue ( ) , themeColor . getAlpha ( ) ) ; } /**
     * Get the active color set
     */ getColorSet ( ) { return this . _colorSet ; } /**
     * Define the current color set and produce a Theme from it
     */ setColorSet ( colorSet ) { this . _colorSet = colorSet ; this . _theme = this . getTheme ( colorSet ) ; } param value returns resolve ( value ) { if ( ! value ) { return ; } const colorName = this . resolveColorName ( value ) ; for ( const color in this . _theme ) { if ( colorName === color . toLowerCase ( ) ) { return this . getColor ( colorName ) . toRgba ( ) ; } } return value ; } param colorName resolveColorName ( colorName = '' ) { return colorName . replace ( /\s+/g , '-' ) . toLowerCase ( ) ; } /** Determine if the current colorset has a specific color */ colorExists ( name ) { return ! ! Object . keys ( this . _theme ) . find ( colorName => colorName === this . resolveColorName ( name ) ) ; } /** Create a theme from a colorset */ private getTheme ( colorSet ) { const theme = { } ; if ( ! colorSet ) { colorSet = colorSets . keppel ; } for ( const color in colorSet . colorValueSet ) { theme [ color ] = ThemeColor . parse ( colorSet . colorValueSet [ color ] ) ; } return theme ; } } export interface Theme { [ name ] : ThemeColor ; } export type ColorIdentifier = string ;