Skip to content

Commit

Permalink
refactor(color): update types, imports
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 6, 2020
1 parent 6951af6 commit f0fa5b0
Show file tree
Hide file tree
Showing 44 changed files with 66 additions and 65 deletions.
18 changes: 8 additions & 10 deletions packages/color/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { ColorMode } from "./constants";
import type { Tuple } from "@thi.ng/api";
import type { ReadonlyVec, Vec } from "@thi.ng/vectors";
import { ColorMode } from "./constants";

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,
];
/**
* A 4x5 matrix in column-major order
*/
export type ColorMatrix = Tuple<number, 20>;

export type CosCoeffs = [number, number, number, number];
export type CosGradientSpec = [CosCoeffs, CosCoeffs, CosCoeffs, CosCoeffs];
export type CosCoeffs = Tuple<number, 4>;
export type CosGradientSpec = Tuple<CosCoeffs, 4>;

export type ColorConversion<T> = (out: Color, src: T) => Color;
export type ColorOp = (out: Color | null, src: ReadonlyColor) => Color;
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/clamp.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { clamp01 } from "@thi.ng/math";
import { setC4 } from "@thi.ng/vectors";
import type { Color, ReadonlyColor } from "./api";
import { ensureAlpha } from "./internal/ensure-alpha";
import { ensureHue } from "./internal/ensure-hue";
import type { Color, ReadonlyColor } from "./api";

/**
* Clamps all color channels to [0,1] interval and calls `ensureAlpha`
Expand Down
4 changes: 2 additions & 2 deletions packages/color/src/convert.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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 { hcyaRgba } from "./hcya-rgba";
import { hsiaRgba } from "./hsia-rgba";
Expand All @@ -22,8 +24,6 @@ import { rgbaXyza } from "./rgba-xyza";
import { rgbaYcbcra } from "./rgba-ycbcra";
import { xyzaRgba } from "./xyza-rgba";
import { ycbcraRgba } from "./ycbcra-rgba";
import type { Implementation2O, MultiFn2O } from "@thi.ng/defmulti";
import type { Color, ColorConversion, IColor, ReadonlyColor } from "./api";

export const convert: MultiFn2O<
string | number | ReadonlyColor | IColor,
Expand Down
16 changes: 10 additions & 6 deletions packages/color/src/cosine-gradients.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FnU2, IObjectOf } from "@thi.ng/api";
import { partial } from "@thi.ng/compose";
import { clamp01, TAU } from "@thi.ng/math";
import {
Expand All @@ -8,9 +9,8 @@ import {
tween,
zip,
} from "@thi.ng/transducers";
import type { Color, CosCoeffs, CosGradientSpec, ReadonlyColor } from "./api";
import { clamp } from "./clamp";
import type { IObjectOf } from "@thi.ng/api";
import type { Color, CosGradientSpec, ReadonlyColor } from "./api";

// see http://dev.thi.ng/gradients/ - unlike the clojure version, these
// presets are for RGBA (though the alpha channel is configured to
Expand Down Expand Up @@ -157,6 +157,7 @@ export const cosineColor = (spec: CosGradientSpec, t: number): Color =>
clamp01(a + b * Math.cos(TAU * (c * t + d)))
),
push(),
// @ts-ignore
zip(...spec)
);

Expand All @@ -170,13 +171,16 @@ export const cosineGradient = (n: number, spec: CosGradientSpec) =>
* @param from - start color
* @param to - end color
*/
export const cosineCoeffs = (from: ReadonlyColor, to: ReadonlyColor) => {
export const cosineCoeffs: FnU2<ReadonlyColor, CosGradientSpec> = (
from,
to
) => {
from = clamp([], from);
to = clamp([], to);
const amp = [...map(([a, b]) => 0.5 * (a - b), zip(from, to))];
return <CosGradientSpec>[
[...map(([s, a]) => s - a, zip(from, amp))],
amp,
return [
<CosCoeffs>[...map(([s, a]) => s - a, zip(from, amp))],
<CosCoeffs>amp,
[-0.5, -0.5, -0.5, -0.5],
[0, 0, 0, 0],
];
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/css.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ColorMode } from "./constants";
import type { ICopy, IDeref } from "@thi.ng/api";
import type { IColor } from "./api";
import { ColorMode } from "./constants";

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

Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hcya-rgba.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { clamp01 } from "@thi.ng/math";
import { dot3, setC3 } from "@thi.ng/vectors";
import type { ColorOp } from "./api";
import { RGB_LUMINANCE } from "./constants";
import { hueRgba } from "./hue-rgba";
import { ensureAlpha } from "./internal/ensure-alpha";
import type { ColorOp } from "./api";

export const hcyaRgba: ColorOp = (out, src) => {
const h = src[0];
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hcya.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { declareIndices, IVector } from "@thi.ng/vectors";
import type { Color } from "./api";
import { ColorMode } from "./constants";
import { AColor } from "./internal/acolor";
import { ensureArgs } from "./internal/ensure-args";
import type { Color } from "./api";

export function hcya(col: Color, offset?: number, stride?: number): HCYA;
export function hcya(h?: number, c?: number, y?: number, a?: number): HCYA;
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hsia-rgba.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { setC3 } from "@thi.ng/vectors";
import { clampH } from "./clamp";
import type { ColorOp } from "./api";
import { clampH } from "./clamp";

// https://en.wikipedia.org/wiki/HSL_and_HSV#From_HSI

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

export function hsia(col: Color, offset?: number, stride?: number): HSIA;
export function hsia(h?: number, s?: number, i?: number, a?: number): HSIA;
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hsla-css.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { clamp01 } from "@thi.ng/math";
import type { ReadonlyColor } from "./api";
import { FF, PC } from "./constants";
import { ensureAlpha } from "./internal/ensure-alpha";
import { ensureHue } from "./internal/ensure-hue";
import type { ReadonlyColor } from "./api";

export const hslaCss = (src: ReadonlyColor) => {
const h = FF(ensureHue(src[0]) * 360);
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hsla-hsva.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clampH } from "./clamp";
import type { ColorOp } from "./api";
import { clampH } from "./clamp";

export const hslaHsva: ColorOp = (out, src) => {
out = clampH(out || src, src);
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hsla-rgba.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { clamp01 } from "@thi.ng/math";
import { setC3 } from "@thi.ng/vectors";
import type { ColorOp } from "./api";
import { hueRgba } from "./hue-rgba";
import { ensureAlpha } from "./internal/ensure-alpha";
import type { ColorOp } from "./api";

export const hslaRgba: ColorOp = (out, src) => {
const s = clamp01(src[1]);
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hsla.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { declareIndices, IVector } from "@thi.ng/vectors";
import type { Color } from "./api";
import { ColorMode } from "./constants";
import { AColor } from "./internal/acolor";
import { ensureArgs } from "./internal/ensure-args";
import type { Color } from "./api";

export function hsla(col: Color, offset?: number, stride?: number): HSLA;
export function hsla(h?: number, s?: number, l?: number, a?: number): HSLA;
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hsva-css.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReadonlyColor } from "./api";
import { hslaCss } from "./hsla-css";
import { hsvaHsla } from "./hsva-hsla";
import type { ReadonlyColor } from "./api";

export const hsvaCss = (src: ReadonlyColor) => hslaCss(hsvaHsla([], src));
2 changes: 1 addition & 1 deletion packages/color/src/hsva-hsla.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clampH } from "./clamp";
import type { ColorOp } from "./api";
import { clampH } from "./clamp";

export const hsvaHsla: ColorOp = (out, src) => {
out = clampH(out || src, src);
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hsva-rgba.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { setC3 } from "@thi.ng/vectors";
import type { ColorOp } from "./api";
import { clampH } from "./clamp";
import { hueRgba } from "./hue-rgba";
import type { ColorOp } from "./api";

export const hsvaRgba: ColorOp = (out, src) => {
out = clampH(out || src, src);
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hsva.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { declareIndices, IVector } from "@thi.ng/vectors";
import type { Color } from "./api";
import { ColorMode } from "./constants";
import { AColor } from "./internal/acolor";
import { ensureArgs } from "./internal/ensure-args";
import type { Color } from "./api";

export function hsva(col: Color, offset?: number, stride?: number): HSVA;
export function hsva(h?: number, s?: number, v?: number, a?: number): HSVA;
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hue-rgba.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { clamp01 } from "@thi.ng/math";
import { setC4 } from "@thi.ng/vectors";
import type { Color } from "./api";
import { Hue } from "./constants";
import { ensureHue } from "./internal/ensure-hue";
import type { Color } from "./api";

/**
* Converts a normalized hue to RGBA with given optional `alpha`
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/int-css.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IDeref } from "@thi.ng/api";
import { U24 } from "@thi.ng/strings";
import { FF, INV8BIT } from "./constants";
import type { IDeref } from "@thi.ng/api";

export const int32Css = (src: number | IDeref<number>) => {
src = typeof src === "number" ? src : src.deref();
Expand Down
4 changes: 2 additions & 2 deletions packages/color/src/int-rgba.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IDeref } from "@thi.ng/api";
import { setC4 } from "@thi.ng/vectors";
import { INV8BIT } from "./constants";
import type { Color } from "./api";
import type { IDeref } from "@thi.ng/api";
import { INV8BIT } from "./constants";

export const int32Rgba = (out: Color | null, src: number | IDeref<number>) => {
src = typeof src === "number" ? src : src.deref();
Expand Down
4 changes: 2 additions & 2 deletions packages/color/src/int.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ColorMode } from "./constants";
import type { IColor } from "./api";
import type { ICopy, IDeref } from "@thi.ng/api";
import type { IColor } from "./api";
import { ColorMode } from "./constants";

/**
* Returns new {@link Int32} wrapping given ARGB int.
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,8 +1,8 @@
import type { IDeref } from "@thi.ng/api";
import { EPS } from "@thi.ng/math";
import { eqDelta4, stridedValues } from "@thi.ng/vectors";
import { ColorMode } from "../constants";
import type { IDeref } from "@thi.ng/api";
import type { Color, IColor } from "../api";
import { ColorMode } from "../constants";

export abstract class AColor<T extends Color> implements IColor, IDeref<Color> {
buf: Color;
Expand Down
5 changes: 2 additions & 3 deletions packages/color/src/internal/matrix-ops.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { clamp01 } from "@thi.ng/math";
import { dotS3, dotS4 } from "@thi.ng/vectors";
import { setC4 } from "@thi.ng/vectors";
import { ensureAlpha } from "./ensure-alpha";
import { dotS3, dotS4, setC4 } from "@thi.ng/vectors";
import type { Color, ColorMatrix, ReadonlyColor } from "../api";
import { ensureAlpha } from "./ensure-alpha";

export const mulV33 = (
out: Color | null,
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/invert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ONE3, sub3 } from "@thi.ng/vectors";
import { clamp } from "./clamp";
import type { ColorOp } from "./api";
import { clamp } from "./clamp";

/**
* Inverts the RGB channels of an RGBA color.
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/luminance-rgb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dot3 } from "@thi.ng/vectors";
import { RGB_LUMINANCE } from "./constants";
import type { ReadonlyColor } from "./api";
import { RGB_LUMINANCE } from "./constants";

export const luminanceRGB = (rgb: ReadonlyColor, weights = RGB_LUMINANCE) =>
dot3(rgb, weights);
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/luminance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DEFAULT, defmulti, MultiFn1O } from "@thi.ng/defmulti";
import { illegalArgs } from "@thi.ng/errors";
import type { IColor, ReadonlyColor } from "./api";
import { ColorMode } from "./constants";
import { convert } from "./convert";
import { luminanceInt, luminanceRGB } from "./luminance-rgb";
import type { IColor, ReadonlyColor } from "./api";

/**
* Multi-method to compute relative luminance from any supported input
Expand Down
4 changes: 2 additions & 2 deletions packages/color/src/parse-css.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { IDeref } from "@thi.ng/api";
import { illegalArgs } from "@thi.ng/errors";
import { clamp01 } from "@thi.ng/math";
import { maybeParseFloat, maybeParseInt } from "@thi.ng/strings";
import type { Color } from "./api";
import { INV8BIT } from "./constants";
import { hslaRgba } from "./hsla-rgba";
import { int32Rgba } from "./int-rgba";
import { CSS_NAMES } from "./names";
import type { IDeref } from "@thi.ng/api";
import type { Color } from "./api";

const RE_HEX = /^#?([0-9a-f]{3,8})$/i;
const RE_CSS = /^(rgb|hsl)a?\(\s*([0-9.]+?),\s*([0-9.]+%?),\s*([0-9.]+%?),?\s*([0-9.]+)?\s*\)$/;
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isArrayLike, isNumber } from "@thi.ng/checks";
import type { ReadonlyColor } from "./api";
import { ColorMode } from "./constants";
import { asCSS } from "./convert";
import type { ReadonlyColor } from "./api";

/**
* Takes a color in one of the following formats and tries to convert it
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/rgba-css.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { clamp01 } from "@thi.ng/math";
import { U24 } from "@thi.ng/strings";
import type { ReadonlyColor } from "./api";
import { FF } from "./constants";
import { ensureAlpha } from "./internal/ensure-alpha";
import type { ReadonlyColor } from "./api";

export const rgbaCss = (src: ReadonlyColor) => {
const r = (clamp01(src[0]) * 0xff + 0.5) | 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/rgba-hcva.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { clamp01, EPS } from "@thi.ng/math";
import { setC3 } from "@thi.ng/vectors";
import { clamp } from "./clamp";
import type { ColorOp } from "./api";
import { clamp } from "./clamp";

/**
* Based on:
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/rgba-hcya.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EPS } from "@thi.ng/math";
import type { ColorOp } from "./api";
import { hueRgba } from "./hue-rgba";
import { luminanceRGB } from "./luminance-rgb";
import { rgbaHcva } from "./rgba-hcva";
import type { ColorOp } from "./api";

/**
* Ported from:
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/rgba-hsia.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { atan2Abs, SQRT3, TAU, THIRD } from "@thi.ng/math";
import { setC3 } from "@thi.ng/vectors";
import { clamp } from "./clamp";
import type { ColorOp } from "./api";
import { clamp } from "./clamp";

// https://en.wikipedia.org/wiki/HSL_and_HSV#Hue_and_chroma

Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/rgba-hsla.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EPS } from "@thi.ng/math";
import { rgbaHcva } from "./rgba-hcva";
import type { ColorOp } from "./api";
import { rgbaHcva } from "./rgba-hcva";

export const rgbaHsla: ColorOp = (out, src) => {
out = rgbaHcva(out, src);
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/rgba-hsva.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EPS } from "@thi.ng/math";
import { rgbaHcva } from "./rgba-hcva";
import type { ColorOp } from "./api";
import { rgbaHcva } from "./rgba-hcva";

export const rgbaHsva: ColorOp = (out, src) => {
out = rgbaHcva(out, src);
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/rgba-int.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { clamp01 } from "@thi.ng/math";
import { ensureAlpha } from "./internal/ensure-alpha";
import type { ReadonlyColor } from "./api";
import { ensureAlpha } from "./internal/ensure-alpha";

export const rgbaInt = (src: ReadonlyColor) =>
(((ensureAlpha(src[3]) * 0xff + 0.5) << 24) |
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/rgba-xyza.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ColorOp } from "./api";
import { clamp } from "./clamp";
import { RGB_XYZ } from "./constants";
import { ensureAlpha } from "./internal/ensure-alpha";
import { mulV33 } from "./internal/matrix-ops";
import type { ColorOp } from "./api";

/**
* {@link https://en.wikipedia.org/wiki/CIE_1931_color_space}
Expand Down
Loading

0 comments on commit f0fa5b0

Please sign in to comment.