'use strict' ; import type ColorString from './ColorString' ; import type { ColorLike , ColorType } from './ColorType' ; import type GradientColor from './GradientColor' ; import H from '../Globals.js' ; import U from '../Utilities.js' ; const { isNumber , merge , pInt } = U ; class name param { Highcharts . ColorType } input class Color implements ColorLike { private public static names : Record < string , ColorString > = { white : '#ffffff' , black : '#000000' } ; /**
     * Collection of parsers. This can be extended from the outside by pushing
     * parsers to `Color.parsers`.
     */ public static parsers = [ { regex : /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]?(?:\.[0-9]+)?)\s*\)/ , parse : function ( result ) : Color . RGBA { return [ pInt ( result [ 1 ] ) , pInt ( result [ 2 ] ) , pInt ( result [ 3 ] ) , ( parseFloat as Function ) ( result [ 4 ] , 10 ) ] ; } } , { regex : /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/ , parse : function ( result ) : Color . RGBA { return [ pInt ( result [ 1 ] ) , pInt ( result [ 2 ] ) , pInt ( result [ 3 ] ) , 1 ] ; } } ] ; public static readonly None = new Color ( '' ) ; function param { Highcharts . ColorType } input return { Highcharts . Color } public static parse ( input ? ) { return input ? new Color ( input ) : Color . None ; } public constructor ( input ) { this . input = input ; const GlobalColor = ( H as AnyRecord ) . Color ; if ( GlobalColor && GlobalColor !== Color ) { return new GlobalColor ( input ) ; } if ( ! ( this instanceof Color ) ) { return new Color ( input ) ; } this . init ( input ) ; } public input : ColorType ; public rgba : Color . RGBA = [ NaN , NaN , NaN , NaN ] ; public stops ? : Array < Color > ; private function param { Highcharts . ColorType } input private init ( input ) { let result , rgba , i , parser ; if ( typeof input === 'object' && typeof ( input as GradientColor ) . stops !== 'undefined' ) { this . stops = ( input as GradientColor ) . stops . map ( ( stop ) : Color => new Color ( stop [ 1 ] ) ) ; } else if ( typeof input === 'string' ) { this . input = input = ( Color . names [ input . toLowerCase ( ) ] || input ) ; if ( input . charAt ( 0 ) === '#' ) { const len = input . length , col = parseInt ( input . substr ( 1 ) , 16 ) ; if ( len === 7 ) { rgba = [ ( col & 0xFF0000 ) >> 16 , ( col & 0xFF00 ) >> 8 , ( col & 0xFF ) , 1 ] ; } else if ( len === 4 ) { rgba = [ ( ( ( col & 0xF00 ) >> 4 ) | ( col & 0xF00 ) >> 8 ) , ( ( ( col & 0xF0 ) >> 4 ) | ( col & 0xF0 ) ) , ( ( col & 0xF ) << 4 ) | ( col & 0xF ) , 1 ] ; } } if ( ! rgba ) { i = Color . parsers . length ; while ( i -- && ! rgba ) { parser = Color . parsers [ i ] ; result = parser . regex . exec ( input ) ; if ( result ) { rgba = parser . parse ( result ) ; } } } } if ( rgba ) { this . rgba = rgba ; } } function param { string } format return { Highcharts . ColorType } public get ( format ? ) { const input = this . input , rgba = this . rgba ; if ( typeof input === 'object' && typeof this . stops !== 'undefined' ) { const ret = merge ( input as GradientColor ) ; ret . stops = [ ] . slice . call ( ret . stops ) ; this . stops . forEach ( ( stop , i ) : void => { ret . stops [ i ] = [ ret . stops [ i ] [ 0 ] , stop . get ( format ) as string ] ; } ) ; return ret ; } if ( rgba && isNumber ( rgba [ 0 ] ) ) { if ( format === 'rgb' || ( ! format && rgba [ 3 ] === 1 ) ) { return 'rgb(' + rgba [ 0 ] + ',' + rgba [ 1 ] + ',' + rgba [ 2 ] + ')' ; } if ( format === 'a' ) { return `${ rgba [ 3 ] }` ; } return 'rgba(' + rgba . join ( ',' ) + ')' ; } return input ; } function param { number } alpha return { Highcharts . Color } public brighten ( alpha ) { const rgba = this . rgba ; if ( this . stops ) { this . stops . forEach ( function ( stop ) : void { stop . brighten ( alpha ) ; } ) ; } else if ( isNumber ( alpha ) && alpha !== 0 ) { for ( let i = 0 ; i < 3 ; i ++ ) { rgba [ i ] += pInt ( alpha * 255 ) ; if ( rgba [ i ] < 0 ) { rgba [ i ] = 0 ; } if ( rgba [ i ] > 255 ) { rgba [ i ] = 255 ; } } } return this ; } function param { number } alpha return { Highcharts . Color } public setOpacity ( alpha ) { this . rgba [ 3 ] = alpha ; return this ; } function param { Highcharts . Color } to param { number } pos return { Highcharts . ColorType } public tweenTo ( to , pos ) { const fromRgba = this . rgba , toRgba = to . rgba ; if ( ! isNumber ( fromRgba [ 0 ] ) || ! isNumber ( toRgba [ 0 ] ) ) { return to . input || 'none' ; } const hasAlpha = ( toRgba [ 3 ] !== 1 || fromRgba [ 3 ] !== 1 ) ; return ( hasAlpha ? 'rgba(' : 'rgb(' ) + Math . round ( toRgba [ 0 ] + ( fromRgba [ 0 ] - toRgba [ 0 ] ) * ( 1 - pos ) ) + ',' + Math . round ( toRgba [ 1 ] + ( fromRgba [ 1 ] - toRgba [ 1 ] ) * ( 1 - pos ) ) + ',' + Math . round ( toRgba [ 2 ] + ( fromRgba [ 2 ] - toRgba [ 2 ] ) * ( 1 - pos ) ) + ( hasAlpha ? ( ',' + ( toRgba [ 3 ] + ( fromRgba [ 3 ] - toRgba [ 3 ] ) * ( 1 - pos ) ) ) : '' ) + ')' ; } } namespace Color { export interface Parser { regex : RegExp ; parse : ( result : RegExpExecArray ) => RGBA ; } export type RGBA = [ number , number , number , number ] ; } export default Color ; typedef { string } Highcharts . ColorString typedef { Highcharts . ColorString | Highcharts . GradientColorObject | Highcharts . PatternObject } Highcharts . ColorType example interface name type { Highcharts . LinearGradientColorObject | undefined } name type { Highcharts . RadialGradientColorObject | undefined } name type { Array < Highcharts . GradientColorStopObject > } see Highcharts . GradientColorObject interface name type { number } name type { Highcharts . ColorString } name type { Highcharts . Color | undefined } interface name type { number } name type { number } name type { number } name type { number } interface name type { number } name type { number } name type { number } function param { Highcharts . ColorType } input return { Highcharts . Color } ( '' ) ;