Skip to content

Commit

Permalink
feat: Update Format and Type classes
Browse files Browse the repository at this point in the history
  • Loading branch information
MysteryBlokHed committed May 31, 2024
1 parent 7ed2ce5 commit a990da2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/Format.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
11 changes: 9 additions & 2 deletions src/Type.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -49,7 +58,6 @@ export default class Type {
/**
* Map a number to the internal representation
* @param {number} number
* @returns
*/
resolve (number) {
if (this.type === "<angle>") {
Expand All @@ -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 === "<percentage>" ? [-100, 100] : this.range;
Expand Down
3 changes: 3 additions & 0 deletions src/space.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" */
Expand All @@ -31,6 +33,7 @@ export interface Format {
* Must be defined for a format to support serialization
*/
serialize?: ((coords: Coords, alpha: number, opts?: Record<string, any>) => string) | undefined;
[instance]?: FormatClass | undefined;
}

export interface CoordMeta {
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a990da2

Please sign in to comment.