Skip to content

Commit

Permalink
Typings: colormaker registry
Browse files Browse the repository at this point in the history
  • Loading branch information
ppillot committed Jan 8, 2024
1 parent aa68b38 commit 5e6cf7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/color/colormaker-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 = {}
Expand All @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/color/colormaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface ColormakerParameters extends ScaleParameters {
export type StuctureColormakerParams = { structure: Structure } & Partial<ColormakerParameters>
export type VolumeColormakerParams = { volume: Volume } & Partial<ColormakerParameters>
export type ColormakerScale = (v: number) => number
export type ColormakerConstructor = new (...p: ConstructorParameters<typeof Colormaker>) => Colormaker

const tmpColor = new Color()

Expand Down

0 comments on commit 5e6cf7c

Please sign in to comment.