import toHex from 'colornames'
import { utils } from 'pixi.js'
import { getValue, setValue } from '../../utils'

export function colorname(co: any) {
    co.on('beforeCreated beforeUpdate', (style) => {
        const attrs = ['backgroundColor', 'borderColor', 'text.color', 'text.fill']
        attrs.map((key: string) => {
            const v = getValue(style, key)
            if (typeof v === 'string') {
                const colorHex = utils.string2hex(v.includes('#') ? v : toHex.get(v).value)
                colorHex && setValue(style, key, colorHex)
            }
        })
    })
}
