Skip to content

Commit

Permalink
feat(color): enable TS strict compiler flags (refactor)
Browse files Browse the repository at this point in the history
- reformat color matrices in transform.ts
  • Loading branch information
postspectacular committed Jun 4, 2019
1 parent 90515e7 commit 8c13166
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 308 deletions.
25 changes: 5 additions & 20 deletions packages/color/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,12 @@ import { ReadonlyVec, Vec } from "@thi.ng/vectors";
export type Color = Vec;
export type ReadonlyColor = ReadonlyVec;

// prettier-ignore
export type ColorMatrix = [
number,
number,
number,
number,
number,
number,
number,
number,
number,
number,
number,
number,
number,
number,
number,
number,
number,
number,
number,
number
number, number, number, number, number,
number, number, number, number, number,
number, number, number, number, number,
number, number, number, number, number,
];

export type CosCoeffs = [number, number, number, number];
Expand Down
59 changes: 25 additions & 34 deletions packages/color/src/convert.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
DEFAULT,
defmulti,
Implementation3,
Implementation2O,
MultiFn2O
} from "@thi.ng/defmulti";
import { illegalArgs } from "@thi.ng/errors";
Expand Down Expand Up @@ -39,20 +39,17 @@ export const convert: MultiFn2O<
ColorMode,
ColorMode,
Color | string | number
> = defmulti(
(col: any, mdest, msrc) =>
col.mode !== undefined
? `${mdest}-${col.mode}`
: msrc !== undefined
? `${mdest}-${msrc}`
: illegalArgs(`missing src color mode`)
> = defmulti((col: any, mdest, msrc) =>
col.mode !== undefined
? `${mdest}-${col.mode}`
: msrc !== undefined
? `${mdest}-${msrc}`
: illegalArgs(`missing src color mode`)
);
convert.add(
DEFAULT,
(col: any, mdest, msrc) =>
(col.mode !== undefined && col.mode === mdest) || mdest === msrc
? col
: illegalArgs(`missing conversion for mode ${msrc} -> ${mdest}`)
convert.add(DEFAULT, (col: any, mdest, msrc) =>
(col.mode !== undefined && col.mode === mdest) || mdest === msrc
? col
: illegalArgs(`missing conversion for mode ${msrc} -> ${mdest}`)
);

export function asCSS(col: IColor): string;
Expand Down Expand Up @@ -130,8 +127,8 @@ export function asYCbCrA(col: any, mode?: ColorMode) {
const defConversion = (
dest: ColorMode,
src: ColorMode,
impl: Implementation3<
string | number | ReadonlyColor,
impl: Implementation2O<
string | number | ReadonlyColor | IColor,
ColorMode,
ColorMode,
Color | string | number
Expand All @@ -153,7 +150,7 @@ const defConversions = (

// CSS

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

[
ColorMode.HCYA,
Expand All @@ -164,7 +161,7 @@ defConversion(ColorMode.RGBA, ColorMode.CSS, (x: string) => parseCss(x));
ColorMode.XYZA,
ColorMode.YCBCRA
].forEach((id) =>
defConversion(id, ColorMode.CSS, (x: string) =>
defConversion(id, ColorMode.CSS, (x: any) =>
convert(parseCss(x), id, ColorMode.RGBA)
)
);
Expand All @@ -182,7 +179,7 @@ defConversions(
ColorMode.YCBCRA
);

defConversion(ColorMode.CSS, ColorMode.INT32, (x: number) => int32Css(x));
defConversion(ColorMode.CSS, ColorMode.INT32, (x: any) => int32Css(x));

// HCYA

Expand Down Expand Up @@ -223,11 +220,9 @@ defConversions(
ColorMode.YCBCRA
);

defConversion(ColorMode.CSS, ColorMode.HSLA, (x: ReadonlyColor) => hslaCss(x));
defConversion(ColorMode.CSS, ColorMode.HSLA, (x: any) => hslaCss(x));

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

// HSVA

Expand All @@ -241,30 +236,26 @@ defConversions(
ColorMode.YCBCRA
);

defConversion(ColorMode.CSS, ColorMode.HSVA, (x: ReadonlyColor) => hsvaCss(x));
defConversion(ColorMode.CSS, ColorMode.HSVA, (x: any) => hsvaCss(x));

defConversion(ColorMode.HSLA, ColorMode.HSVA, (x: ReadonlyColor) =>
hsvaHsla([], x)
);
defConversion(ColorMode.HSLA, ColorMode.HSVA, (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]: [ColorMode, ColorConversion<ReadonlyColor>]) =>
defConversion(id, ColorMode.RGBA, (x: ReadonlyColor) => fn([], x))
]).forEach(([id, fn]) =>
defConversion(id, ColorMode.RGBA, (x: any) => fn([], x))
);

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

defConversion(ColorMode.INT32, ColorMode.RGBA, (x: ReadonlyColor) =>
rgbaInt(x)
);
defConversion(ColorMode.INT32, ColorMode.RGBA, (x: any) => rgbaInt(x));

// XYZA

Expand Down
4 changes: 2 additions & 2 deletions packages/color/src/internal/acolor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IDeref } from "@thi.ng/api";
import { EPS } from "@thi.ng/math";
import { eqDelta4, values } from "@thi.ng/vectors";
import { Color, IColor } from "../api";
import { Color, ColorMode, IColor } from "../api";

export abstract class AColor<T extends Color> implements IColor, IDeref<Color> {
buf: Color;
Expand All @@ -19,7 +19,7 @@ export abstract class AColor<T extends Color> implements IColor, IDeref<Color> {
return values(this.buf, 4, this.offset, this.stride);
}

abstract get mode();
abstract get mode(): ColorMode;

get length() {
return 4;
Expand Down
15 changes: 7 additions & 8 deletions packages/color/src/luminance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ export const luminance: MultiFn1O<
number | string | ReadonlyColor | IColor,
ColorMode,
number
> = defmulti(
(col: any, mode) =>
col.mode !== undefined
? col.mode
: mode !== undefined
? mode
: illegalArgs(`missing color mode`)
> = defmulti((col: any, mode) =>
col.mode !== undefined
? col.mode
: mode !== undefined
? mode
: illegalArgs(`missing color mode`)
);

luminance.add(ColorMode.RGBA, (x: ReadonlyColor) => luminanceRGB(x));
luminance.add(ColorMode.RGBA, (x: any) => luminanceRGB(x));

luminance.add(ColorMode.INT32, (x: any) =>
luminanceInt(typeof x === "number" ? x : x.deref())
Expand Down
4 changes: 3 additions & 1 deletion packages/color/src/names.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const CSS_NAMES = {
import { IObjectOf } from "@thi.ng/api";

export const CSS_NAMES: IObjectOf<string> = {
aliceblue: "f0f8ff",
antiquewhite: "faebd7",
aqua: "0ff",
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/porter-duff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const porterDuff = (
: (s: number, d: number, sda: number, sy: number) =>
f(s, d) * sda + s * sy
: z
? (s: number, d: number, sda: number, _, sz: number) =>
? (s: number, d: number, sda: number, _: number, sz: number) =>
f(s, d) * sda + d * sz
: (s: number, d: number, sda: number) => f(s, d) * sda;
return (out: Color, src: ReadonlyColor, dest: ReadonlyColor) => {
Expand Down
Loading

0 comments on commit 8c13166

Please sign in to comment.