@@ -17,6 +17,11 @@ interface CssVariable {
17
17
val : string ;
18
18
}
19
19
20
+ interface Color {
21
+ rgb : Numberify < RGBA > ;
22
+ isLight : boolean ;
23
+ }
24
+
20
25
// @see : https://github.com/angular/angular/issues/20351
21
26
/** @dynamic */
22
27
@Injectable ( {
@@ -26,20 +31,18 @@ export class MaterialCssVarsService {
26
31
private static CONTRAST_PREFIX = 'contrast-' ;
27
32
private static DARK_TEXT_VAR = '--dark-primary-text' ;
28
33
private static LIGHT_TEXT_VAR = '--light-primary-text' ;
29
-
30
- private renderer : Renderer2 ;
31
- private ROOT : HTMLElement ;
32
-
33
34
// This should be readonly from the outside
34
35
cfg : MaterialCssVariablesConfig ;
35
- primary : string ;
36
- accent : string ;
37
- warn : string ;
38
- isDarkTheme : boolean ;
36
+ primary = '#03a9f4' ;
37
+ accent = '#e91e63' ;
38
+ warn = '#f44336' ;
39
+ isDarkTheme = false ; // ToDo: make this attribute optional in next major version
39
40
contrastColorThresholdPrimary : HueValue = '400' ;
40
41
contrastColorThresholdAccent : HueValue = '400' ;
41
42
contrastColorThresholdWarn : HueValue = '400' ;
42
43
isAutoContrast = false ;
44
+ private renderer : Renderer2 ;
45
+ private ROOT : HTMLElement ;
43
46
44
47
constructor (
45
48
rendererFactory : RendererFactory2 ,
@@ -203,6 +206,31 @@ export class MaterialCssVarsService {
203
206
}
204
207
}
205
208
209
+ getPaletteWithContrastForColor ( hex : string ) : MatCssHueColorContrastMapItem [ ] {
210
+ const lightText = this . _getCssVarValue ( MaterialCssVarsService . LIGHT_TEXT_VAR ) ;
211
+ const darkText = this . _getCssVarValue ( MaterialCssVarsService . DARK_TEXT_VAR ) ;
212
+ const palette = this . getPaletteForColor ( hex ) ;
213
+
214
+ // TODO handle non auto case
215
+ return palette . map ( ( item ) => {
216
+ const contrastStr = item . isLight
217
+ ? lightText
218
+ : darkText ;
219
+
220
+ const sLight = this . _replaceNoRgbValue ( '' , contrastStr )
221
+ . split ( ',' )
222
+ . map ( ( v ) => + v ) ;
223
+ const cco = { r : sLight [ 0 ] , g : sLight [ 1 ] , b : sLight [ 2 ] , a : 1 } ;
224
+ return {
225
+ ...item ,
226
+ contrast : {
227
+ ...cco ,
228
+ str : `${ cco . r } ,${ cco . g } ,${ cco . b } `
229
+ } ,
230
+ } ;
231
+ } ) ;
232
+ }
233
+
206
234
private getTraditionalPaletteForColor ( hex : string ) : MatCssHueColorMapItem [ ] {
207
235
return this . cfg . colorMap . map ( item => {
208
236
const mappedColor = new TinyColor ( hex )
@@ -235,31 +263,6 @@ export class MaterialCssVarsService {
235
263
} ) ;
236
264
}
237
265
238
- getPaletteWithContrastForColor ( hex : string ) : MatCssHueColorContrastMapItem [ ] {
239
- const lightText = this . _getCssVarValue ( MaterialCssVarsService . LIGHT_TEXT_VAR ) ;
240
- const darkText = this . _getCssVarValue ( MaterialCssVarsService . DARK_TEXT_VAR ) ;
241
- const palette = this . getPaletteForColor ( hex ) ;
242
-
243
- // TODO handle non auto case
244
- return palette . map ( ( item ) => {
245
- const contrastStr = item . isLight
246
- ? lightText
247
- : darkText ;
248
-
249
- const sLight = this . _replaceNoRgbValue ( '' , contrastStr )
250
- . split ( ',' )
251
- . map ( ( v ) => + v ) ;
252
- const cco = { r : sLight [ 0 ] , g : sLight [ 1 ] , b : sLight [ 2 ] , a : 1 } ;
253
- return {
254
- ...item ,
255
- contrast : {
256
- ...cco ,
257
- str : `${ cco . r } ,${ cco . g } ,${ cco . b } `
258
- } ,
259
- } ;
260
- } ) ;
261
- }
262
-
263
266
private _computePaletteColors ( prefix : MatCssPalettePrefix , hex : string ) : CssVariable [ ] {
264
267
return this . getPaletteForColor ( hex ) . map ( item => {
265
268
const c = item . color ;
@@ -336,11 +339,11 @@ export class MaterialCssVarsService {
336
339
* Compute pallet colors based on a Triad (Constantin)
337
340
* see: https://github.com/mbitson/mcg
338
341
*/
339
- private computePalletTriad ( hex : string , hue : HueValue ) {
342
+ private computePalletTriad ( hex : string , hue : HueValue ) : Color {
340
343
const baseLight = new TinyColor ( '#ffffff' ) ;
341
344
const baseDark = this . multiply ( new TinyColor ( hex ) . toRgb ( ) , new TinyColor ( hex ) . toRgb ( ) ) ;
342
345
const baseTriad = new TinyColor ( hex ) . tetrad ( ) ;
343
- let color : { rgb : Numberify < RGBA > , isLight : boolean } ;
346
+ let color : Color ;
344
347
345
348
switch ( hue ) {
346
349
case '50' :
@@ -385,8 +388,6 @@ export class MaterialCssVarsService {
385
388
case 'A700' :
386
389
color = this . getColorObject ( baseDark . mix ( baseTriad [ 4 ] , 15 ) . saturate ( 100 ) . lighten ( 40 ) ) ;
387
390
break ;
388
- default :
389
- break ;
390
391
}
391
392
return color ;
392
393
}
@@ -398,7 +399,7 @@ export class MaterialCssVarsService {
398
399
return new TinyColor ( 'rgb ' + rgb1 . r + ' ' + rgb1 . g + ' ' + rgb1 . b ) ;
399
400
}
400
401
401
- private getColorObject ( value : TinyColor ) {
402
+ private getColorObject ( value : TinyColor ) : Color {
402
403
const c = new TinyColor ( value ) ;
403
404
return { rgb : c . toRgb ( ) , isLight : c . isLight ( ) } ;
404
405
}
0 commit comments