Skip to content

Commit

Permalink
refactor(color): fix #256 replace enum w/ type alias
Browse files Browse the repository at this point in the history
BREAKING CHANGE: replace ColorMode w/ type alias
  • Loading branch information
postspectacular committed Dec 22, 2020
1 parent 88edbe1 commit 17e2449
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 156 deletions.
12 changes: 11 additions & 1 deletion packages/color/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import type { Tuple } from "@thi.ng/api";
import type { ReadonlyVec, Vec } from "@thi.ng/vectors";
import type { ColorMode } from "./constants";

export type ColorMode =
| "rgb"
| "hcy"
| "hsv"
| "hsl"
| "hsi"
| "int"
| "css"
| "xyz"
| "ycbcr";

export type Color = Vec;
export type ReadonlyColor = ReadonlyVec;
Expand Down
12 changes: 0 additions & 12 deletions packages/color/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import { float, percent } from "@thi.ng/strings";

export enum ColorMode {
RGBA,
HCYA,
HSVA,
HSLA,
HSIA,
INT32,
CSS,
XYZA,
YCBCRA,
}

// RGBA constants

export const BLACK = Object.freeze([0, 0, 0, 1]);
Expand Down
177 changes: 71 additions & 106 deletions packages/color/src/convert.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { Implementation2O, MultiFn2O } from "@thi.ng/defmulti";
import { DEFAULT, defmulti } from "@thi.ng/defmulti";
import { illegalArgs } from "@thi.ng/errors";
import type { Color, ColorConversion, IColor, ReadonlyColor } from "./api";
import { ColorMode } from "./constants";
import type {
Color,
ColorConversion,
ColorMode,
IColor,
ReadonlyColor,
} from "./api";
import { hcyaRgba } from "./hcya-rgba";
import { hsiaRgba } from "./hsia-rgba";
import { hslaCss } from "./hsla-css";
Expand Down Expand Up @@ -49,7 +54,7 @@ export function asCSS(
mode: ColorMode
): string;
export function asCSS(col: any, mode?: ColorMode) {
return <string>convert(col, ColorMode.CSS, mode);
return <string>convert(col, "css", mode);
}

export function asRGBA(col: IColor): Color;
Expand All @@ -58,7 +63,7 @@ export function asRGBA(
mode: ColorMode
): Color;
export function asRGBA(col: any, mode?: ColorMode) {
return <Color>convert(col, ColorMode.RGBA, mode);
return <Color>convert(col, "rgb", mode);
}

export function asHCYA(col: IColor): Color;
Expand All @@ -67,7 +72,7 @@ export function asHCYA(
mode: ColorMode
): Color;
export function asHCYA(col: any, mode?: ColorMode) {
return <Color>convert(col, ColorMode.HCYA, mode);
return <Color>convert(col, "hcy", mode);
}

export function asHSIA(col: IColor): Color;
Expand All @@ -76,7 +81,7 @@ export function asHSIA(
mode: ColorMode
): Color;
export function asHSIA(col: any, mode?: ColorMode) {
return <Color>convert(col, ColorMode.HSIA, mode);
return <Color>convert(col, "hsi", mode);
}

export function asHSLA(col: IColor): Color;
Expand All @@ -85,7 +90,7 @@ export function asHSLA(
mode: ColorMode
): Color;
export function asHSLA(col: any, mode?: ColorMode) {
return <Color>convert(col, ColorMode.HSLA, mode);
return <Color>convert(col, "hsl", mode);
}

export function asHSVA(col: IColor): Color;
Expand All @@ -94,7 +99,7 @@ export function asHSVA(
mode: ColorMode
): Color;
export function asHSVA(col: any, mode?: ColorMode) {
return <Color>convert(col, ColorMode.HSVA, mode);
return <Color>convert(col, "hsv", mode);
}

export function asXYZA(col: IColor): Color;
Expand All @@ -103,7 +108,7 @@ export function asXYZA(
mode: ColorMode
): Color;
export function asXYZA(col: any, mode?: ColorMode) {
return <Color>convert(col, ColorMode.XYZA, mode);
return <Color>convert(col, "xyz", mode);
}

export function asYCbCrA(col: IColor): Color;
Expand All @@ -112,7 +117,7 @@ export function asYCbCrA(
mode: ColorMode
): Color;
export function asYCbCrA(col: any, mode?: ColorMode) {
return <Color>convert(col, ColorMode.YCBCRA, mode);
return <Color>convert(col, "ycbcr", mode);
}

const defConversion = (
Expand All @@ -131,147 +136,107 @@ const defConversions = (
toRGBA: ColorConversion<any>,
...dest: ColorMode[]
) => {
defConversion(ColorMode.RGBA, src, (x: any) => toRGBA([], x));
defConversion("rgb", src, (x: any) => toRGBA([], x));
dest.forEach((id) =>
defConversion(id, src, (x: any) =>
convert(toRGBA([], x), id, ColorMode.RGBA)
)
defConversion(id, src, (x: any) => convert(toRGBA([], x), id, "rgb"))
);
};

// CSS

defConversion(ColorMode.RGBA, ColorMode.CSS, (x: any) => parseCss(x));

[
ColorMode.HCYA,
ColorMode.HSIA,
ColorMode.HSLA,
ColorMode.HSVA,
ColorMode.INT32,
ColorMode.XYZA,
ColorMode.YCBCRA,
].forEach((id) =>
defConversion(id, ColorMode.CSS, (x: any) =>
convert(parseCss(x), id, ColorMode.RGBA)
)
defConversion("rgb", "css", (x: any) => parseCss(x));

(<ColorMode[]>[
"hcy",
"hsi",
"hsl",
"hsv",
"int",
"xyz",
"ycbcr",
]).forEach((id) =>
defConversion(id, "css", (x: any) => convert(parseCss(x), id, "rgb"))
);

// Int

defConversions(
ColorMode.INT32,
int32Rgba,
ColorMode.HCYA,
ColorMode.HSIA,
ColorMode.HSLA,
ColorMode.HSVA,
ColorMode.XYZA,
ColorMode.YCBCRA
);
defConversions("int", int32Rgba, "hcy", "hsi", "hsl", "hsv", "xyz", "ycbcr");

defConversion(ColorMode.CSS, ColorMode.INT32, (x: any) => int32Css(x));
defConversion("css", "int", (x: any) => int32Css(x));

// HCYA

defConversions(
ColorMode.HCYA,
hcyaRgba,
ColorMode.CSS,
ColorMode.INT32,
ColorMode.HSLA,
ColorMode.HSVA,
ColorMode.XYZA,
ColorMode.YCBCRA
);
defConversions("hcy", hcyaRgba, "css", "int", "hsl", "hsv", "xyz", "ycbcr");

// HSIA

defConversions(
ColorMode.HSIA,
"hsi",
hsiaRgba,
ColorMode.CSS,
ColorMode.INT32,
ColorMode.HCYA,
ColorMode.HSLA,
ColorMode.HSVA,
ColorMode.XYZA,
ColorMode.YCBCRA
"css",
"int",
"hcy",
"hsl",
"hsv",
"xyz",
"ycbcr"
);

// HSLA

defConversions(
ColorMode.HSLA,
hslaRgba,
ColorMode.HCYA,
ColorMode.HSIA,
ColorMode.INT32,
ColorMode.XYZA,
ColorMode.YCBCRA
);
defConversions("hsl", hslaRgba, "hcy", "hsi", "int", "xyz", "ycbcr");

defConversion(ColorMode.CSS, ColorMode.HSLA, (x: any) => hslaCss(x));
defConversion("css", "hsl", (x: any) => hslaCss(x));

defConversion(ColorMode.HSVA, ColorMode.HSLA, (x: any) => hslaHsva([], x));
defConversion("hsv", "hsl", (x: any) => hslaHsva([], x));

// HSVA

defConversions(
ColorMode.HSVA,
hsvaRgba,
ColorMode.HCYA,
ColorMode.HSIA,
ColorMode.INT32,
ColorMode.XYZA,
ColorMode.YCBCRA
);
defConversions("hsv", hsvaRgba, "hcy", "hsi", "int", "xyz", "ycbcr");

defConversion(ColorMode.CSS, ColorMode.HSVA, (x: any) => hsvaCss(x));
defConversion("css", "hsv", (x: any) => hsvaCss(x));

defConversion(ColorMode.HSLA, ColorMode.HSVA, (x: any) => hsvaHsla([], x));
defConversion("hsl", "hsv", (x: any) => hsvaHsla([], x));

// RGBA

(<[ColorMode, ColorConversion<ReadonlyColor>][]>[
[ColorMode.HCYA, rgbaHcya],
[ColorMode.HSIA, rgbaHsia],
[ColorMode.HSLA, rgbaHsla],
[ColorMode.HSVA, rgbaHsva],
[ColorMode.XYZA, rgbaXyza],
[ColorMode.YCBCRA, rgbaYcbcra],
]).forEach(([id, fn]) =>
defConversion(id, ColorMode.RGBA, (x: any) => fn([], x))
);
["hcy", rgbaHcya],
["hsi", rgbaHsia],
["hsl", rgbaHsla],
["hsv", rgbaHsva],
["xyz", rgbaXyza],
["ycbcr", rgbaYcbcra],
]).forEach(([id, fn]) => defConversion(id, "rgb", (x: any) => fn([], x)));

defConversion(ColorMode.CSS, ColorMode.RGBA, (x: any) => rgbaCss(x));
defConversion("css", "rgb", (x: any) => rgbaCss(x));

defConversion(ColorMode.INT32, ColorMode.RGBA, (x: any) => rgbaInt(x));
defConversion("int", "rgb", (x: any) => rgbaInt(x));

// XYZA

defConversions(
ColorMode.XYZA,
"xyz",
xyzaRgba,
ColorMode.CSS,
ColorMode.HCYA,
ColorMode.HSIA,
ColorMode.HSLA,
ColorMode.HSVA,
ColorMode.INT32,
ColorMode.YCBCRA
"css",
"hcy",
"hsi",
"hsl",
"hsv",
"int",
"ycbcr"
);

// YCbCr

defConversions(
ColorMode.YCBCRA,
"ycbcr",
ycbcraRgba,
ColorMode.CSS,
ColorMode.HCYA,
ColorMode.HSIA,
ColorMode.HSLA,
ColorMode.HSVA,
ColorMode.INT32,
ColorMode.XYZA
"css",
"hcy",
"hsi",
"hsl",
"hsv",
"int",
"xyz"
);
5 changes: 2 additions & 3 deletions packages/color/src/css.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ICopy, IDeref } from "@thi.ng/api";
import type { IColor } from "./api";
import { ColorMode } from "./constants";
import type { ColorMode, IColor } from "./api";

export const css = (col: string) => new CSS(col);

Expand All @@ -12,7 +11,7 @@ export class CSS implements IColor, ICopy<CSS>, IDeref<string> {
}

get mode() {
return ColorMode.CSS;
return <ColorMode>"css";
}

copy() {
Expand Down
5 changes: 2 additions & 3 deletions packages/color/src/hcya.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { declareIndices, IVector } from "@thi.ng/vectors";
import type { Color } from "./api";
import { ColorMode } from "./constants";
import type { Color, ColorMode } from "./api";
import { AColor } from "./internal/acolor";
import { ensureArgs } from "./internal/ensure-args";

Expand All @@ -17,7 +16,7 @@ export class HCYA extends AColor<HCYA> implements IVector<HCYA> {
a!: number;

get mode() {
return ColorMode.HCYA;
return <ColorMode>"hcy";
}

copy() {
Expand Down
5 changes: 2 additions & 3 deletions packages/color/src/hsia.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { declareIndices, IVector } from "@thi.ng/vectors";
import type { Color } from "./api";
import { ColorMode } from "./constants";
import type { Color, ColorMode } from "./api";
import { AColor } from "./internal/acolor";
import { ensureArgs } from "./internal/ensure-args";

Expand All @@ -17,7 +16,7 @@ export class HSIA extends AColor<HSIA> implements IVector<HSIA> {
a!: number;

get mode() {
return ColorMode.HSIA;
return <ColorMode>"hsi";
}

copy() {
Expand Down
5 changes: 2 additions & 3 deletions packages/color/src/hsla.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { declareIndices, IVector } from "@thi.ng/vectors";
import type { Color } from "./api";
import { ColorMode } from "./constants";
import type { Color, ColorMode } from "./api";
import { AColor } from "./internal/acolor";
import { ensureArgs } from "./internal/ensure-args";

Expand All @@ -17,7 +16,7 @@ export class HSLA extends AColor<HSLA> implements IVector<HSLA> {
a!: number;

get mode() {
return ColorMode.HSLA;
return <ColorMode>"hsl";
}

copy() {
Expand Down
Loading

0 comments on commit 17e2449

Please sign in to comment.