import { isArray } from "@thi.ng/checks/is-array" ; import { assert } from "@thi.ng/errors/assert" ; import { unsupported } from "@thi.ng/errors/unsupported" ; import type { Color , ColorMode , ColorSpec , Conversions , ReadonlyColor , } from "./api.js" ; export const CONVERSIONS = { } ; /**
 * Registers conversions for given  ColorSpec . Called by
 *  defColor .
 *
 *  param spec internal export const defConversions = ( mode , spec ) => { for ( let id in spec ) { const val = spec [ < ColorMode > id ] ; if ( isArray ( val ) ) { const [ a , b , c , d ] = val ; spec [ < ColorMode > id ] = val . length === 2 ? ( out , src ) => b ( out , a ( out , src ) ) : val . length === 3 ? ( out , src ) => c ! ( out , b ( out , a ( out , src ) ) ) : ( out , src ) => d ! ( out , c ! ( out , b ( out , a ( out , src ) ) ) ) ; } } CONVERSIONS [ mode ] = { ... CONVERSIONS [ mode ] , ... ( < Conversions > spec ) } ; } ; export const convert = < T extends Color > ( res , src , destMode , srcMode ) : T => { const spec = CONVERSIONS [ destMode ] ; assert ( ! ! spec , `no conversions available for ${ destMode }` ) ; let $convert = spec ! [ srcMode ] ; return $convert ? < T > $convert ( res , src ) : CONVERSIONS . rgb ! [ srcMode ] ? < T > spec ! . rgb ( res , CONVERSIONS . rgb ! [ srcMode ] ! ( [ ] , src ) ) : unsupported ( `can't convert: ${ srcMode } -> ${ destMode }` ) ; } ;