From c3dce07d4d3f3163182c24b2ceed99096b9c4d5a Mon Sep 17 00:00:00 2001 From: Adam Thompson-Sharpe Date: Wed, 26 Jun 2024 16:46:01 -0400 Subject: [PATCH] [types] Remove module augmentation (#566) Removes the module augmentation from `index.d.ts`, instead putting the relevant properties directly into `color.d.ts`. --- types/src/color.d.ts | 123 ++++++++++++++++++++++++++++++++++++++++ types/src/index.d.ts | 130 ++----------------------------------------- 2 files changed, 127 insertions(+), 126 deletions(-) diff --git a/types/src/color.d.ts b/types/src/color.d.ts index 6853dd240..93d24e2db 100644 --- a/types/src/color.d.ts +++ b/types/src/color.d.ts @@ -19,6 +19,29 @@ import { display, } from "./index-fn.js"; +import { uv, xy } from "./chromaticity.js"; +import contrast from "./contrast.js"; +import { + contrastWCAG21, + contrastAPCA, + contrastMichelson, + contrastWeber, + contrastLstar, + contrastDeltaPhi, +} from "./contrast/index.js"; +import deltaE from "./deltaE.js"; +import deltaEMethods, { + deltaE76, + deltaECMC, + deltaE2000, + deltaEJz, + deltaEITP, + deltaEOK, +} from "./deltaE/index.js"; +import { range, Range, MixOptions, StepsOptions } from "./interpolation.js"; +import { getLuminance } from "./luminance.js"; +import { lighten, darken } from "./variations.js"; + export type { SpaceAccessor } from "./space-coord-accessors.js"; export type Coords = [number, number, number]; @@ -155,6 +178,106 @@ declare class Color extends SpaceAccessors implements PlainColorObject { // These should always match the signature of the original function set (prop: Ref, value: number | ((coord: number) => number)): Color; set (props: Record number)>): Color; + + /* + * ========================================== + * Types for properties defined in `index.js` + * ========================================== + */ + // chromaticity + uv: ToColorPrototype; + xy: ToColorPrototype; + + // contrast + contrast: ToColorPrototype; + static contrast: typeof contrast; + + // contrastMethods + contrastWCAG21: ToColorPrototype; + contrastAPCA: ToColorPrototype; + contrastMichelson: ToColorPrototype; + contrastWeber: ToColorPrototype; + contrastLstar: ToColorPrototype; + contrastDeltaPhi: ToColorPrototype; + + static contrastWCAG21: typeof contrastWCAG21; + static contrastAPCA: typeof contrastAPCA; + static contrastMichelson: typeof contrastMichelson; + static contrastWeber: typeof contrastWeber; + static contrastLstar: typeof contrastLstar; + static contrastDeltaPhi: typeof contrastDeltaPhi; + + // deltaE + deltaE: ToColorPrototype; + deltaE76: ToColorPrototype; + deltaECMC: ToColorPrototype; + deltaE2000: ToColorPrototype; + deltaEJz: ToColorPrototype; + deltaEITP: ToColorPrototype; + deltaEOK: ToColorPrototype; + + static deltaE: typeof deltaE; + static deltaE76: typeof deltaE76; + static deltaECMC: typeof deltaECMC; + static deltaE2000: typeof deltaE2000; + static deltaEJz: typeof deltaEJz; + static deltaEITP: typeof deltaEITP; + static deltaEOK: typeof deltaEOK; + static deltaEMethods: typeof deltaEMethods; + + // interpolation + // These signatures should always match those in interpolation.d.ts, + // including the static versions + /** Create color mixtures in any desired proportion between two colors */ + mix (color2: ColorTypes, options?: MixOptions): Color; + mix (color2: ColorTypes, p: number, options?: MixOptions): Color; + /** + * Creates a function that accepts a number and returns a color. + * For numbers in the range 0 to 1, the function interpolates; + * for numbers outside that range, the function extrapolates + * (and thus may not return the results you expect) + */ + range: ToColorPrototype; + /** Get an array of discrete steps */ + steps (color2: ColorTypes, options?: StepsOptions): Color[]; + + /** Create color mixtures in any desired proportion between two colors */ + static mix ( + color1: ColorTypes, + color2: ColorTypes, + options?: MixOptions + ): Color; + static mix ( + color1: ColorTypes, + color2: ColorTypes, + p: number, + options?: MixOptions + ): Color; + /** + * Creates a function that accepts a number and returns a color. + * For numbers in the range 0 to 1, the function interpolates; + * for numbers outside that range, the function extrapolates + * (and thus may not return the results you expect) + */ + static range: typeof range; + /** Get an array of discrete steps */ + static steps ( + color1: ColorTypes, + color2: ColorTypes, + options?: StepsOptions + ): Color[]; + static steps (range: Range, options?: StepsOptions): Color[]; + + // luminance + get luminance (): ReturnType; + // the definition for this set in the orignial code like it doesn't actually use the parameter? + set luminance (_: number); + + // variations + lighten: ToColorPrototype; + darken: ToColorPrototype; + static lighten: typeof lighten; + static darken: typeof darken; } export default Color; diff --git a/types/src/index.d.ts b/types/src/index.d.ts index 0b672dcb2..a80f5943d 100644 --- a/types/src/index.d.ts +++ b/types/src/index.d.ts @@ -1,127 +1,5 @@ -import { uv, xy } from "./chromaticity.js"; -import BaseColor, { ColorTypes, ToColorPrototype } from "./color.js"; -import contrast from "./contrast.js"; -import { - contrastWCAG21, - contrastAPCA, - contrastMichelson, - contrastWeber, - contrastLstar, - contrastDeltaPhi, -} from "./contrast/index.js"; -import deltaE from "./deltaE.js"; -import deltaEMethods, { - deltaE76, - deltaECMC, - deltaE2000, - deltaEJz, - deltaEITP, - deltaEOK, -} from "./deltaE/index.js"; -import { range, Range, MixOptions, StepsOptions } from "./interpolation.js"; -import { getLuminance } from "./luminance.js"; -import { lighten, darken } from "./variations.js"; -// Augment existing Color object -declare module "./color" { - // https://github.com/color-js/color.js/issues/560 - // Seems to cause problems if the original class is not expicitly extended? - export default class Color extends BaseColor { - // chromaticity - uv: ToColorPrototype; - xy: ToColorPrototype; - - // contrast - contrast: ToColorPrototype; - static contrast: typeof contrast; - - // contrastMethods - contrastWCAG21: ToColorPrototype; - contrastAPCA: ToColorPrototype; - contrastMichelson: ToColorPrototype; - contrastWeber: ToColorPrototype; - contrastLstar: ToColorPrototype; - contrastDeltaPhi: ToColorPrototype; - - static contrastWCAG21: typeof contrastWCAG21; - static contrastAPCA: typeof contrastAPCA; - static contrastMichelson: typeof contrastMichelson; - static contrastWeber: typeof contrastWeber; - static contrastLstar: typeof contrastLstar; - static contrastDeltaPhi: typeof contrastDeltaPhi; - - // deltaE - deltaE: ToColorPrototype; - deltaE76: ToColorPrototype; - deltaECMC: ToColorPrototype; - deltaE2000: ToColorPrototype; - deltaEJz: ToColorPrototype; - deltaEITP: ToColorPrototype; - deltaEOK: ToColorPrototype; - - static deltaE: typeof deltaE; - static deltaE76: typeof deltaE76; - static deltaECMC: typeof deltaECMC; - static deltaE2000: typeof deltaE2000; - static deltaEJz: typeof deltaEJz; - static deltaEITP: typeof deltaEITP; - static deltaEOK: typeof deltaEOK; - static deltaEMethods: typeof deltaEMethods; - - // interpolation - // These signatures should always match those in interpolation.d.ts, - // including the static versions - /** Create color mixtures in any desired proportion between two colors */ - mix (color2: ColorTypes, options?: MixOptions): this; - mix (color2: ColorTypes, p: number, options?: MixOptions): this; - /** - * Creates a function that accepts a number and returns a color. - * For numbers in the range 0 to 1, the function interpolates; - * for numbers outside that range, the function extrapolates - * (and thus may not return the results you expect) - */ - range: ToColorPrototype; - /** Get an array of discrete steps */ - steps (color2: ColorTypes, options?: StepsOptions): this[]; - - /** Create color mixtures in any desired proportion between two colors */ - static mix ( - color1: ColorTypes, - color2: ColorTypes, - options?: MixOptions - ): BaseColor; - static mix ( - color1: ColorTypes, - color2: ColorTypes, - p: number, - options?: MixOptions - ): BaseColor; - /** - * Creates a function that accepts a number and returns a color. - * For numbers in the range 0 to 1, the function interpolates; - * for numbers outside that range, the function extrapolates - * (and thus may not return the results you expect) - */ - static range: typeof range; - /** Get an array of discrete steps */ - static steps ( - color1: ColorTypes, - color2: ColorTypes, - options?: StepsOptions - ): BaseColor[]; - static steps (range: Range, options?: StepsOptions): BaseColor[]; - - // luminance - get luminance (): ReturnType; - // the definition for this set in the orignial code like it doesn't actually use the parameter? - set luminance (_: number); - - // variations - lighten: ToColorPrototype; - darken: ToColorPrototype; - static lighten: typeof lighten; - static darken: typeof darken; - } -} - -export default BaseColor; +import Color from "./color.js"; +// Module augmentation has instead been merged with the definition in `color.d.ts`. +// If any new properties are added to the JS source, place them in `color.d.ts` +export default Color;