import { ChartPalette } from './ChartControl.types' ; import { OFFICE_COLORFUL1 , OFFICE_COLORFUL2 , OFFICE_COLORFUL3 , OFFICE_COLORFUL4 , OFFICE_MONOCHROMATIC1 , OFFICE_MONOCHROMATIC2 , OFFICE_MONOCHROMATIC3 , OFFICE_MONOCHROMATIC4 , OFFICE_MONOCHROMATIC5 , OFFICE_MONOCHROMATIC6 , OFFICE_MONOCHROMATIC7 , OFFICE_MONOCHROMATIC8 , OFFICE_MONOCHROMATIC9 , OFFICE_MONOCHROMATIC10 , OFFICE_MONOCHROMATIC11 , OFFICE_MONOCHROMATIC12 , OFFICE_MONOCHROMATIC13 } from './ChartColorPalettes' ; import * as Color from 'color' ; /**
 * Generates color palettes matching those you get within Office charts
 */ export class PaletteGenerator { param palette param desiredLength public static GetPalette ( palette , desiredLength ) { let paletteColors = [ ] ; switch ( palette ) { case ChartPalette . OfficeColorful4 : paletteColors = this . generateRepeatingPattern ( OFFICE_COLORFUL4 , desiredLength ) ; break ; case ChartPalette . OfficeColorful3 : paletteColors = this . generateRepeatingPattern ( OFFICE_COLORFUL3 , desiredLength ) ; break ; case ChartPalette . OfficeColorful2 : paletteColors = this . generateRepeatingPattern ( OFFICE_COLORFUL2 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic1 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC1 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic2 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC2 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic3 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC3 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic4 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC4 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic5 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC5 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic6 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC6 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic7 : paletteColors = this . generateRepeatingPattern ( OFFICE_MONOCHROMATIC7 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic8 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC8 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic9 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC9 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic10 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC10 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic11 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC11 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic12 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC12 , desiredLength ) ; break ; case ChartPalette . OfficeMonochromatic13 : paletteColors = this . generateNonRepeatingGradient ( OFFICE_MONOCHROMATIC13 , desiredLength ) ; break ; default : paletteColors = this . generateRepeatingPattern ( OFFICE_COLORFUL1 , desiredLength ) ; break ; } return paletteColors ; } param array param desiredLength public static generateRepeatingPattern ( array , desiredLength ) { if ( desiredLength === 0 ) { return [ ] ; } let a = array ; while ( a . length * 2 <= desiredLength ) { a = a . concat ( a ) ; } if ( a . length < desiredLength ) { a = a . concat ( a . slice ( 0 , desiredLength - a . length ) ) ; } return a ; } param colorRange param numSteps public static generateNonRepeatingGradient ( colorRange , numSteps ) { const startHex = colorRange [ 0 ] ; const endHex = colorRange [ colorRange . length - 1 ] ; const startRGB = this . _hexToRGB ( startHex ) ; const endRGB = this . _hexToRGB ( endHex ) ; const colors = [ ] ; const factorStep = 1 / ( numSteps - 1 ) ; for ( let index = 0 ; index < numSteps ; index ++ ) { const interpolatedColor = this . _interpolateColors ( startRGB , endRGB , factorStep * index ) ; const hexColor = this . _rgbToHex ( interpolatedColor ) ; colors . push ( hexColor ) ; } return colors ; } param colors param alphaValue public static alpha ( colors , alphaValue ) { if ( colors instanceof Array ) { return colors . map ( ( color ) => { return Color ( color ) . alpha ( alphaValue ) . toString ( ) ; } ) ; } else { return Color ( colors ) . alpha ( alphaValue ) . toString ( ) ; } } param color1 param color2 param factor private static _interpolateColors ( color1 , color2 , factor ) { if ( arguments . length < 3 ) { factor = 0.5 ; } const result = color1 . slice ( ) ; for ( let i = 0 ; i < 3 ; i ++ ) { result [ i ] = Math . round ( result [ i ] + factor * ( color2 [ i ] - color1 [ i ] ) ) ; } return result ; } param hex private static _hexToRGB ( hex ) { const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i . exec ( hex ) ; return result ? [ parseInt ( result [ 1 ] , 16 ) , parseInt ( result [ 2 ] , 16 ) , parseInt ( result [ 3 ] , 16 ) ] : undefined ; } param rgb private static _rgbToHex ( rgb ) { return '#' + ( ( 1 << 24 ) + ( rgb [ 0 ] << 16 ) + ( rgb [ 1 ] << 8 ) + rgb [ 2 ] ) . toString ( 16 ) . slice ( 1 ) ; } }