import { getColorRampCenterColor , getColorPalette } from '../../../color_palettes' ; import { COLOR_MAP_TYPE , STYLE_TYPE } from '../../../../../../common/constants' ; import { ColorDynamicOptions , ColorStylePropertyDescriptor , } from '../../../../../../common/descriptor_types' ; export function extractColorFromStyleProperty ( colorStyleProperty , defaultColor ) { if ( ! colorStyleProperty ) { return defaultColor ; } if ( colorStyleProperty . type === STYLE_TYPE . STATIC ) { return colorStyleProperty . options . color ; } const dynamicOptions = colorStyleProperty . options ; if ( ! dynamicOptions . field || ! dynamicOptions . field . name ) { return defaultColor ; } if ( dynamicOptions . type === COLOR_MAP_TYPE . CATEGORICAL ) { if ( dynamicOptions . useCustomColorPalette ) { return dynamicOptions . customColorPalette && dynamicOptions . customColorPalette . length ? dynamicOptions . customColorPalette [ 0 ] . color : defaultColor ; } if ( ! dynamicOptions . colorCategory ) { return defaultColor ; } const palette = getColorPalette ( dynamicOptions . colorCategory ) ; return palette ? palette [ 0 ] : defaultColor ; } else { if ( dynamicOptions . useCustomColorRamp ) { if ( ! dynamicOptions . customColorRamp || ! dynamicOptions . customColorRamp . length ) { return defaultColor ; } const middleIndex = Math . floor ( ( dynamicOptions . customColorRamp . length - 1 ) / 2 ) ; return dynamicOptions . customColorRamp [ middleIndex ] . color ; } if ( ! dynamicOptions . color ) { return defaultColor ; } const centerColor = getColorRampCenterColor ( dynamicOptions . color ) ; return centerColor ? centerColor : defaultColor ; } }