Skip to content

Commit

Permalink
Merge pull request #1011 from ppillot/colormaker-constructor
Browse files Browse the repository at this point in the history
#1010 Colormaker-constructor
  • Loading branch information
ppillot committed Jan 9, 2024
2 parents d9b2914 + 9554f04 commit ddea04f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/color/colormaker-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
*/

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'

type ColormakerDefinitionFunction = ((this: Colormaker, param?: ColormakerParameters) => void)

const ColormakerScales = {
'': '',

Expand Down Expand Up @@ -75,8 +77,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 +89,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 +109,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 +141,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,8 +172,8 @@ class ColormakerRegistry {
* @param {String} label - scheme label
* @return {String} id to refer to the registered scheme
*/
addScheme (scheme: any, label?: string) {
if (!(scheme instanceof Colormaker)) {
addScheme (scheme: ColormakerConstructor|ColormakerDefinitionFunction, label?: string) {
if (!(isColormakerSubClass(scheme))) {
scheme = this._createScheme(scheme)
}

Expand All @@ -183,7 +186,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,15 +204,13 @@ class ColormakerRegistry {
delete this.userSchemes[ id ]
}

_createScheme (constructor: any) {
const _Colormaker = function (this: any, params: ColormakerParameters) {
Colormaker.call(this, params)
constructor.call(this, params)
_createScheme (constructor: ColormakerDefinitionFunction): ColormakerConstructor {
class _Colormaker extends Colormaker {
constructor (params: ColormakerParameters) {
super(params)
constructor.call(this, params)
}
}

_Colormaker.prototype = Colormaker.prototype
_Colormaker.prototype.constructor = Colormaker

return _Colormaker
}

Expand Down Expand Up @@ -257,4 +258,10 @@ class ColormakerRegistry {
}
}

function isColormakerSubClass (
scheme: ColormakerConstructor|((this: Colormaker, param?: ColormakerParameters) => void)
): scheme is ColormakerConstructor {
return (scheme instanceof Colormaker)
}

export default ColormakerRegistry
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 ddea04f

Please sign in to comment.