Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1010 Colormaker-constructor #1011

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 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,15 +202,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: any): 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
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