diff --git a/packages/color/src/api.ts b/packages/color/src/api.ts index aa95c6b86c..8b41169552 100644 --- a/packages/color/src/api.ts +++ b/packages/color/src/api.ts @@ -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]; diff --git a/packages/color/src/convert.ts b/packages/color/src/convert.ts index efa7046b5d..c93a951dde 100644 --- a/packages/color/src/convert.ts +++ b/packages/color/src/convert.ts @@ -1,7 +1,7 @@ import { DEFAULT, defmulti, - Implementation3, + Implementation2O, MultiFn2O } from "@thi.ng/defmulti"; import { illegalArgs } from "@thi.ng/errors"; @@ -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; @@ -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 @@ -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, @@ -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) ) ); @@ -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 @@ -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 @@ -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][]>[ [ColorMode.HCYA, rgbaHcya], [ColorMode.HSIA, rgbaHsia], [ColorMode.HSLA, rgbaHsla], [ColorMode.HSVA, rgbaHsva], [ColorMode.XYZA, rgbaXyza], [ColorMode.YCBCRA, rgbaYcbcra] -].forEach(([id, fn]: [ColorMode, ColorConversion]) => - 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 diff --git a/packages/color/src/internal/acolor.ts b/packages/color/src/internal/acolor.ts index 01982039cb..00de8a7340 100644 --- a/packages/color/src/internal/acolor.ts +++ b/packages/color/src/internal/acolor.ts @@ -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 implements IColor, IDeref { buf: Color; @@ -19,7 +19,7 @@ export abstract class AColor implements IColor, IDeref { return values(this.buf, 4, this.offset, this.stride); } - abstract get mode(); + abstract get mode(): ColorMode; get length() { return 4; diff --git a/packages/color/src/luminance.ts b/packages/color/src/luminance.ts index 2000adc80a..5f3af11953 100644 --- a/packages/color/src/luminance.ts +++ b/packages/color/src/luminance.ts @@ -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()) diff --git a/packages/color/src/names.ts b/packages/color/src/names.ts index b1e0860842..759fb6a1a4 100644 --- a/packages/color/src/names.ts +++ b/packages/color/src/names.ts @@ -1,4 +1,6 @@ -export const CSS_NAMES = { +import { IObjectOf } from "@thi.ng/api"; + +export const CSS_NAMES: IObjectOf = { aliceblue: "f0f8ff", antiquewhite: "faebd7", aqua: "0ff", diff --git a/packages/color/src/porter-duff.ts b/packages/color/src/porter-duff.ts index 114d7b6170..0f8406fff2 100644 --- a/packages/color/src/porter-duff.ts +++ b/packages/color/src/porter-duff.ts @@ -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) => { diff --git a/packages/color/src/transform.ts b/packages/color/src/transform.ts index b70cadbbdb..0f1573fef9 100644 --- a/packages/color/src/transform.ts +++ b/packages/color/src/transform.ts @@ -1,6 +1,11 @@ import { mix } from "@thi.ng/math"; -import { ColorMatrix, RGB_LUMINANCE, ReadonlyColor, WHITE } from "./api"; -import { mulV45, mulM45 } from "./internal/matrix-ops"; +import { + ColorMatrix, + ReadonlyColor, + RGB_LUMINANCE, + WHITE +} from "./api"; +import { mulM45, mulV45 } from "./internal/matrix-ops"; // https://drafts.fxtf.org/filter-effects/#feColorMatrixElement @@ -46,27 +51,12 @@ export const transform = mulV45; export const concat = (mat: ColorMatrix, ...xs: ColorMatrix[]) => xs.reduce(mulM45, mat); +// prettier-ignore export const IDENTITY: ColorMatrix = [ - 1, - 0, - 0, - 0, - 0, - 0, - 1, - 0, - 0, - 0, - 0, - 0, - 1, - 0, - 0, - 0, - 0, - 0, - 1, - 0 + 1, 0, 0, 0, 0, + 0, 1, 0, 0, 0, + 0, 0, 1, 0, 0, + 0, 0, 0, 1, 0 ]; /** @@ -76,27 +66,12 @@ export const IDENTITY: ColorMatrix = [ * * @param src */ +// prettier-ignore export const subtract = (src: ReadonlyColor = WHITE): ColorMatrix => [ - -1, - 0, - 0, - 0, - src[0], - 0, - -1, - 0, - 0, - src[1], - 0, - 0, - -1, - 0, - src[2], - 0, - 0, - 0, - 1, - 0 + -1, 0, 0, 0, src[0], + 0, -1, 0, 0, src[1], + 0, 0, -1, 0, src[2], + 0, 0, 0, 1, 0 ]; /** @@ -108,192 +83,72 @@ export const subtract = (src: ReadonlyColor = WHITE): ColorMatrix => [ * * @param x */ +// prettier-ignore export const brightness = (x: number): ColorMatrix => [ - 1, - 0, - 0, - 0, - x, - 0, - 1, - 0, - 0, - x, - 0, - 0, - 1, - 0, - x, - 0, - 0, - 0, - 1, - 0 + 1, 0, 0, 0, x, + 0, 1, 0, 0, x, + 0, 0, 1, 0, x, + 0, 0, 0, 1, 0 ]; +// prettier-ignore export const contrast = (x: number, o = 0.5 * (1 - x)): ColorMatrix => [ - x, - 0, - 0, - 0, - o, - 0, - x, - 0, - 0, - o, - 0, - 0, - x, - 0, - o, - 0, - 0, - 0, - 1, - 0 + x, 0, 0, 0, o, + 0, x, 0, 0, o, + 0, 0, x, 0, o, + 0, 0, 0, 1, 0 ]; +// prettier-ignore export const exposure = (x: number): ColorMatrix => [ - x, - 0, - 0, - 0, - 0, - 0, - x, - 0, - 0, - 0, - 0, - 0, - x, - 0, - 0, - 0, - 0, - 0, - 1, - 0 + x, 0, 0, 0, 0, + 0, x, 0, 0, 0, + 0, 0, x, 0, 0, + 0, 0, 0, 1, 0 ]; +// prettier-ignore export const saturation = (x: number): ColorMatrix => [ - S1 + S4 * x, - S3 - S3 * x, - S0 - S0 * x, - 0, - 0, - S1 - S1 * x, - S3 + S2 * x, - S0 - S0 * x, - 0, - 0, - S1 - S1 * x, - S3 - S3 * x, - S0 + S5 * x, - 0, - 0, - 0, - 0, - 0, - 1, - 0 + S1 + S4 * x, S3 - S3 * x, S0 - S0 * x, 0, 0, + S1 - S1 * x, S3 + S2 * x, S0 - S0 * x, 0, 0, + S1 - S1 * x, S3 - S3 * x, S0 + S5 * x, 0, 0, + 0, 0, 0, 1, 0 ]; +// prettier-ignore export const hueRotate = (theta: number): ColorMatrix => { const s = Math.sin(theta); const c = Math.cos(theta); return [ - S1 + c * S4 - s * S1, - S3 - c * S3 - s * S3, - S0 - c * S0 + s * S5, - 0, - 0, - S1 - c * S1 + s * S7, - S3 + c * S2 + s * S6, - S0 - c * S0 - s * S8, - 0, - 0, - S1 - c * S1 - s * S4, - S3 - c * S3 + s * S3, - S0 + c * S5 + s * S0, - 0, - 0, - 0, - 0, - 0, - 1, - 0 + S1 + c * S4 - s * S1, S3 - c * S3 - s * S3, S0 - c * S0 + s * S5, 0, 0, + S1 - c * S1 + s * S7, S3 + c * S2 + s * S6, S0 - c * S0 - s * S8, 0, 0, + S1 - c * S1 - s * S4, S3 - c * S3 + s * S3, S0 + c * S5 + s * S0, 0, 0, + 0, 0, 0, 1, 0 ]; }; +// prettier-ignore export const temperature = (x: number): ColorMatrix => [ - 1 + x, - 0, - 0, - 0, - 0, - 0, - 1, - 0, - 0, - 0, - 0, - 0, - 1 - x, - 0, - 0, - 0, - 0, - 0, - 1, - 0 + 1 + x, 0, 0, 0, 0, + 0, 1, 0, 0, 0, + 0, 0, 1 - x, 0, 0, + 0, 0, 0, 1, 0 ]; +// prettier-ignore export const sepia = (x = 1): ColorMatrix => [ - mix(1, 0.393, x), - 0.769 * x, - 0.189 * x, - 0, - 0, - 0.349 * x, - mix(1, 0.686, x), - 0.168 * x, - 0, - 0, - 0.272 * x, - 0.534 * x, - mix(1, 0.131, x), - 0, - 0, - 0, - 0, - 0, - 1, - 0 + mix(1, 0.393, x), 0.769 * x, 0.189 * x, 0, 0, + 0.349 * x, mix(1, 0.686, x), 0.168 * x, 0, 0, + 0.272 * x, 0.534 * x, mix(1, 0.131, x), 0, 0, + 0, 0, 0, 1, 0 ]; +// prettier-ignore export const tint = (x: number): ColorMatrix => [ - 1 + x, - 0, - 0, - 0, - 0, - 0, - 1, - 0, - 0, - 0, - 0, - 0, - 1 + x, - 0, - 0, - 0, - 0, - 0, - 1, - 0 + 1 + x, 0, 0, 0, 0, + 0, 1, 0, 0, 0, + 0, 0, 1 + x, 0, 0, + 0, 0, 0, 1, 0 ]; /** @@ -303,27 +158,12 @@ export const tint = (x: number): ColorMatrix => [ * @param x * @param coeffs */ +// prettier-ignore export const grayscale = (x = 0, [r, g, b] = RGB_LUMINANCE): ColorMatrix => [ - r, - g, - b, - 0, - x, - r, - g, - b, - 0, - x, - r, - g, - b, - 0, - x, - 0, - 0, - 0, - 1, - 0 + r, g, b, 0, x, + r, g, b, 0, x, + r, g, b, 0, x, + 0, 0, 0, 1, 0 ]; /** @@ -333,25 +173,10 @@ export const grayscale = (x = 0, [r, g, b] = RGB_LUMINANCE): ColorMatrix => [ * * @param coeffs */ +// prettier-ignore export const luminanceAlpha = ([r, g, b] = RGB_LUMINANCE): ColorMatrix => [ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - r, - g, - b, - 0, - 0 + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + r, g, b, 0, 0 ];