Skip to content

Commit

Permalink
[types] Add types for space accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
MysteryBlokHed authored and LeaVerou committed Oct 13, 2022
1 parent eb2dba8 commit efcd444
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
35 changes: 35 additions & 0 deletions types/src/color.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export type ToColorPrototype<T extends (...args: any[]) => any> = T extends (
? (...args: A) => R
: never;

/** Proxy used for space accessors */
export type SpaceAccessor = Record<string, number> & number[];

declare namespace Color {
export {
getAll,
Expand Down Expand Up @@ -131,6 +134,38 @@ declare class Color {
toGamut: ToColorPrototype<typeof toGamut>;
distance: ToColorPrototype<typeof distance>;
toString: ToColorPrototype<typeof serialize>;

// Space accessors
// A property should technically be added every time a new ColorSpace is initialized,
// but I don't know that there's any good way to do that with TypeScript
a98rgb: SpaceAccessor;
a98rgb_linear: SpaceAccessor;
acescc: SpaceAccessor;
acescg: SpaceAccessor;
hsl: SpaceAccessor;
hsv: SpaceAccessor;
hwb: SpaceAccessor;
ictcp: SpaceAccessor;
jzazbz: SpaceAccessor;
jzczhz: SpaceAccessor;
lab: SpaceAccessor;
lch: SpaceAccessor;
oklab: SpaceAccessor;
oklch: SpaceAccessor;
p3: SpaceAccessor;
p3_linear: SpaceAccessor;
prophoto: SpaceAccessor;
prophoto_linear: SpaceAccessor;
rec2020: SpaceAccessor;
rec2020_linear: SpaceAccessor;
rec2100hlg: SpaceAccessor;
rec2100pq: SpaceAccessor;
srgb: SpaceAccessor;
srgb_linear: SpaceAccessor;
xyz: SpaceAccessor;
xyz_abs_d65: SpaceAccessor;
xyz_d50: SpaceAccessor;
xyz_d65: SpaceAccessor;
}

export default Color;
12 changes: 11 additions & 1 deletion types/test/space-accessors.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
// Doesn't export any values, so just check that it's a module
import Color from "colorjs.io/src/color";
// The itself doesn't export any values, so just check that it's a module
import "colorjs.io/src/space-accessors";

const color = new Color("red");

color.srgb[0] = 1;
color.srgb[1] = 1;
color.srgb[2] = 1;
color.srgb.r = 1;
color.srgb.g = 1;
color.srgb.b = 1;

0 comments on commit efcd444

Please sign in to comment.