From 5e6cf7c96d9554936460814d66427b4bdae64055 Mon Sep 17 00:00:00 2001 From: ppillot Date: Sat, 23 Dec 2023 15:34:14 -0500 Subject: [PATCH] Typings: colormaker registry --- src/color/colormaker-registry.ts | 19 ++++++++++--------- src/color/colormaker.ts | 1 + 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/color/colormaker-registry.ts b/src/color/colormaker-registry.ts index 6fcfa34e..5fce134c 100644 --- a/src/color/colormaker-registry.ts +++ b/src/color/colormaker-registry.ts @@ -5,7 +5,7 @@ */ import { generateUUID } from '../math/math-utils' -import Colormaker, { ColormakerParameters } from './colormaker' +import Colormaker, { ColormakerConstructor, ColormakerParameters } from './colormaker' import SelectionColormaker, { SelectionSchemeData } from './selection-colormaker' import Structure from '../structure/structure' @@ -75,8 +75,8 @@ const ColormakerModes = { * global {@link src/globals.js~ColormakerRegistry} instance. */ class ColormakerRegistry { - schemes: { [k: string]: any } - userSchemes: { [k: string]: any } + schemes: { [k: string]: ColormakerConstructor } + userSchemes: { [k: string]: ColormakerConstructor } constructor () { this.schemes = {} @@ -87,13 +87,14 @@ class ColormakerRegistry { const p = params || {} const id = (p.scheme || '').toLowerCase() - let SchemeClass + let SchemeClass: ColormakerConstructor if (id in this.schemes) { SchemeClass = this.schemes[ id ] } else if (id in this.userSchemes) { SchemeClass = this.userSchemes[ id ] } else { + //@ts-expect-error abstract class used as a constructor SchemeClass = Colormaker } @@ -106,7 +107,7 @@ class ColormakerRegistry { * @return {Object} available schemes */ getSchemes () { - const types: { [k: string]: any } = {} + const types: { [k: string]: string } = {} Object.keys(this.schemes).forEach(function (k) { types[ k ] = k @@ -138,7 +139,7 @@ class ColormakerRegistry { * @param {Colormaker} scheme - the colormaker * @return {undefined} */ - add (id: string, scheme: typeof Colormaker) { + add (id: string, scheme: ColormakerConstructor) { id = id.toLowerCase() this.schemes[ id ] = scheme } @@ -169,7 +170,7 @@ class ColormakerRegistry { * @param {String} label - scheme label * @return {String} id to refer to the registered scheme */ - addScheme (scheme: any, label?: string) { + addScheme (scheme: ColormakerConstructor, label?: string) { if (!(scheme instanceof Colormaker)) { scheme = this._createScheme(scheme) } @@ -183,7 +184,7 @@ class ColormakerRegistry { * @param {String} [label] - scheme label * @return {String} id to refer to the registered scheme */ - _addUserScheme (scheme: any, label?: string) { + _addUserScheme (scheme: ColormakerConstructor, label?: string) { label = label || '' const id = `${generateUUID()}|${label}`.toLowerCase() this.userSchemes[ id ] = scheme @@ -201,7 +202,7 @@ class ColormakerRegistry { delete this.userSchemes[ id ] } - _createScheme (constructor: any): typeof Colormaker { + _createScheme (constructor: any): ColormakerConstructor { class _Colormaker extends Colormaker { constructor (params: ColormakerParameters) { super(params) diff --git a/src/color/colormaker.ts b/src/color/colormaker.ts index 18335498..5866bd7a 100644 --- a/src/color/colormaker.ts +++ b/src/color/colormaker.ts @@ -61,6 +61,7 @@ export interface ColormakerParameters extends ScaleParameters { export type StuctureColormakerParams = { structure: Structure } & Partial export type VolumeColormakerParams = { volume: Volume } & Partial export type ColormakerScale = (v: number) => number +export type ColormakerConstructor = new (...p: ConstructorParameters) => Colormaker const tmpColor = new Color()