@@ -453,6 +453,78 @@ export class UtilsService {
453
453
) ;
454
454
}
455
455
456
+ public blendColor ( hexColor : string , magnitude : number ) : string
457
+ {
458
+ hexColor = hexColor . replace ( `#` , `` ) ;
459
+ if ( hexColor . length === 6 ) {
460
+ const decimalColor = parseInt ( hexColor , 16 ) ;
461
+ let r = ( decimalColor >> 16 ) + magnitude ;
462
+ r > 255 && ( r = 255 ) ;
463
+ r < 0 && ( r = 0 ) ;
464
+ let g = ( decimalColor & 0x0000ff ) + magnitude ;
465
+ g > 255 && ( g = 255 ) ;
466
+ g < 0 && ( g = 0 ) ;
467
+ let b = ( ( decimalColor >> 8 ) & 0x00ff ) + magnitude ;
468
+ b > 255 && ( b = 255 ) ;
469
+ b < 0 && ( b = 0 ) ;
470
+ return `#${ ( g | ( b << 8 ) | ( r << 16 ) ) . toString ( 16 ) } ` ;
471
+ } else {
472
+ return hexColor ;
473
+ }
474
+ }
475
+
476
+ public isColorDark ( color : string ) : boolean {
477
+ let r : number = 0 ;
478
+ let g : number = 0 ;
479
+ let b : number = 0 ;
480
+
481
+ // Check the format of the color, HEX or RGB?
482
+ if ( color . match ( / ^ r g b / ) ) {
483
+
484
+ // If HEX --> store the red, green, blue values in separate variables
485
+ let colorHex = color . match ( / ^ r g b a ? \( ( \d + ) , \s * ( \d + ) , \s * ( \d + ) (?: , \s * ( \d + (?: \. \d + ) ? ) ) ? \) $ / ) ;
486
+
487
+ if ( colorHex )
488
+ {
489
+ r = parseInt ( colorHex [ 1 ] ) ;
490
+ g = parseInt ( colorHex [ 2 ] ) ;
491
+ b = parseInt ( colorHex [ 3 ] ) ;
492
+ }
493
+ }
494
+ else {
495
+ // If RGB --> Convert it to HEX: http://gist.github.com/983661
496
+ //let colorRGB = +("0x" + color.slice(1).replace(color.length < 5 && /./g, '$&$&'));
497
+
498
+ //r = colorRGB >> 16;
499
+ //g = colorRGB >> 8 & 255;
500
+ //b = colorRGB & 255;
501
+
502
+ var result = / ^ # ? ( [ a - f \d ] { 2 } ) ( [ a - f \d ] { 2 } ) ( [ a - f \d ] { 2 } ) $ / i. exec ( color ) ;
503
+
504
+ if ( result )
505
+ {
506
+ r = parseInt ( result [ 1 ] , 16 ) ;
507
+ g = parseInt ( result [ 2 ] , 16 ) ;
508
+ b = parseInt ( result [ 3 ] , 16 ) ;
509
+ }
510
+ }
511
+
512
+ // HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html
513
+ let hsp = Math . sqrt (
514
+ 0.299 * ( r * r ) +
515
+ 0.587 * ( g * g ) +
516
+ 0.114 * ( b * b )
517
+ ) ;
518
+
519
+ // Using the HSP value, determine whether the color is light or dark
520
+ if ( hsp > 127.5 ) {
521
+ return false ;
522
+ }
523
+ else {
524
+ return true ;
525
+ }
526
+ }
527
+
456
528
public padZero ( str : string , len : number ) : string {
457
529
len = len || 2 ;
458
530
var zeros = new Array ( len ) . join ( '0' ) ;
0 commit comments