diff --git a/src/Format.js b/src/Format.js index 2177808c7..9dac1a886 100644 --- a/src/Format.js +++ b/src/Format.js @@ -1,13 +1,28 @@ import { isNone } from "./util.js"; import Type from "./Type.js"; -let instance = Symbol("instance"); +// Type "imports" +/** @typedef {import("./types.js").ColorSpace} ColorSpace */ +/** @typedef {import("./types.js").Format} FormatInterface */ + +export const instance = Symbol("instance"); /** * @class Format * Class to hold a color serialization format */ export default class Format { + // Class properties - declared here so that type inference works + type; + name; + spaceCoords; + /** @type {[Type, Type, Type]} */ + coords; + + /** + * @param {FormatInterface} format + * @param {ColorSpace} space + */ constructor (format, space = format.space) { format[instance] = this; this.type = "function"; diff --git a/src/Type.js b/src/Type.js index 9a1164b2a..691342e97 100644 --- a/src/Type.js +++ b/src/Type.js @@ -1,6 +1,15 @@ import { serializeNumber, mapRange } from "./util.js"; export default class Type { + // Class properties - declared here so that type inference works + type; + coordMeta; + coordRange; + + /** + * @param {any} type + * @param {import("./types.js").CoordMeta} coordMeta + */ constructor (type, coordMeta) { if (typeof type === "object") { this.coordMeta = type; @@ -49,7 +58,6 @@ export default class Type { /** * Map a number to the internal representation * @param {number} number - * @returns */ resolve (number) { if (this.type === "") { @@ -70,7 +78,6 @@ export default class Type { * Serialize a number from the internal representation to a string * @param {number} number * @param {number} [precision] - * @returns */ serialize (number, precision) { let toRange = this.type === "" ? [-100, 100] : this.range; diff --git a/src/space.d.ts b/src/space.d.ts index c251303aa..26b97e055 100644 --- a/src/space.d.ts +++ b/src/space.d.ts @@ -5,6 +5,8 @@ */ import { White } from "./adapt.js"; import { ColorConstructor, Coords, ColorTypes } from "./color.js"; +import type FormatClasa from "./Format.js"; +import type { instance } from "./Format.js"; export interface Format { /** @default "function" */ @@ -31,6 +33,7 @@ export interface Format { * Must be defined for a format to support serialization */ serialize?: ((coords: Coords, alpha: number, opts?: Record) => string) | undefined; + [instance]?: FormatClass | undefined; } export interface CoordMeta { diff --git a/src/types.d.ts b/src/types.d.ts index 429ebca1e..3e5cf4bfa 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -118,6 +118,7 @@ export interface SerializeOptions { // space.js import type ColorSpace from "./space.js"; import type { Format, Ref, SpaceOptions } from "./space.js"; +export type { ColorSpace }; export type * from "./space.js"; // toGamut.js