Skip to content

Commit

Permalink
refactor(color): update imports, internal restruct
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 24, 2020
1 parent 0653e1b commit 592409c
Show file tree
Hide file tree
Showing 51 changed files with 168 additions and 152 deletions.
2 changes: 1 addition & 1 deletion packages/color/src/alpha.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setC4 } from "@thi.ng/vectors";
import { Color, ReadonlyColor } from "./api";
import type { Color, ReadonlyColor } from "./api";

export const alpha = (src: ReadonlyColor) =>
src[3] !== undefined ? src[3] : 1;
Expand Down
80 changes: 2 additions & 78 deletions packages/color/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { float, percent } from "@thi.ng/strings";
import { ReadonlyVec, Vec } from "@thi.ng/vectors";
import { ColorMode } from "./constants";
import type { ReadonlyVec, Vec } from "@thi.ng/vectors";

export type Color = Vec;
export type ReadonlyColor = ReadonlyVec;
Expand All @@ -18,82 +18,6 @@ export type CosGradientSpec = [CosCoeffs, CosCoeffs, CosCoeffs, CosCoeffs];
export type ColorConversion<T> = (out: Color, src: T) => Color;
export type ColorOp = (out: Color | null, src: ReadonlyColor) => Color;

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

export interface IColor {
readonly mode: ColorMode;
}

// RGBA constants

export const BLACK = Object.freeze([0, 0, 0, 1]);
export const WHITE = Object.freeze([1, 1, 1, 1]);

export const RED = Object.freeze([1, 0, 0, 1]);
export const GREEN = Object.freeze([0, 1, 0, 1]);
export const BLUE = Object.freeze([0, 0, 1, 1]);

export const CYAN = Object.freeze([0, 1, 1, 1]);
export const MAGENTA = Object.freeze([1, 0, 1, 1]);
export const YELLOW = Object.freeze([1, 1, 0, 1]);

export const RGB_LUMINANCE = [0.299, 0.587, 0.114];

// Hue names

export const enum Hue {
RED,
ORANGE,
YELLOW,
CHARTREUSE,
GREEN,
SPRING_GREEN,
CYAN,
AZURE,
BLUE,
VIOLET,
MAGENTA,
ROSE
}

// internal helpers

export const SRGB_ALPHA = 0.055;

export const RGB_XYZ = [
0.4124564,
0.3575761,
0.1804375,
0.2126729,
0.7151522,
0.072175,
0.0193339,
0.119192,
0.9503041
];

export const XYZ_RGB = [
3.2404542,
-1.5371385,
-0.4985314,
-0.969266,
1.8760108,
0.041556,
0.0556434,
-0.2040259,
1.0572252
];

export const FF = float(2);
export const PC = percent(2);
export const INV8BIT = 1 / 0xff;
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 { 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
2 changes: 1 addition & 1 deletion packages/color/src/closest-hue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hue } from "./api";
import { Hue } from "./constants";
import { ensureHue } from "./internal/ensure-hue";

/**
Expand Down
77 changes: 77 additions & 0 deletions packages/color/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { float, percent } from "@thi.ng/strings";

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

// RGBA constants

export const BLACK = Object.freeze([0, 0, 0, 1]);
export const WHITE = Object.freeze([1, 1, 1, 1]);

export const RED = Object.freeze([1, 0, 0, 1]);
export const GREEN = Object.freeze([0, 1, 0, 1]);
export const BLUE = Object.freeze([0, 0, 1, 1]);

export const CYAN = Object.freeze([0, 1, 1, 1]);
export const MAGENTA = Object.freeze([1, 0, 1, 1]);
export const YELLOW = Object.freeze([1, 1, 0, 1]);

export const RGB_LUMINANCE = [0.299, 0.587, 0.114];

// Hue names

export const enum Hue {
RED,
ORANGE,
YELLOW,
CHARTREUSE,
GREEN,
SPRING_GREEN,
CYAN,
AZURE,
BLUE,
VIOLET,
MAGENTA,
ROSE
}

// internal helpers

export const SRGB_ALPHA = 0.055;

export const RGB_XYZ = [
0.4124564,
0.3575761,
0.1804375,
0.2126729,
0.7151522,
0.072175,
0.0193339,
0.119192,
0.9503041
];

export const XYZ_RGB = [
3.2404542,
-1.5371385,
-0.4985314,
-0.969266,
1.8760108,
0.041556,
0.0556434,
-0.2040259,
1.0572252
];

export const FF = float(2);
export const PC = percent(2);
export const INV8BIT = 1 / 0xff;
22 changes: 9 additions & 13 deletions packages/color/src/convert.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import {
DEFAULT,
defmulti,
Implementation2O,
MultiFn2O
} from "@thi.ng/defmulti";
import { DEFAULT, defmulti } from "@thi.ng/defmulti";
import { illegalArgs } from "@thi.ng/errors";
import {
Color,
ColorConversion,
ColorMode,
IColor,
ReadonlyColor
} from "./api";
import { ColorMode } from "./constants";
import { hcyaRgba } from "./hcya-rgba";
import { hsiaRgba } from "./hsia-rgba";
import { hslaCss } from "./hsla-css";
Expand All @@ -33,6 +22,13 @@ 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
4 changes: 2 additions & 2 deletions packages/color/src/cosine-gradients.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { IObjectOf } from "@thi.ng/api";
import { partial } from "@thi.ng/compose";
import { clamp01, TAU } from "@thi.ng/math";
import {
Expand All @@ -9,8 +8,9 @@ import {
tween,
zip
} from "@thi.ng/transducers";
import { Color, 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
5 changes: 3 additions & 2 deletions packages/color/src/css.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ICopy, IDeref } from "@thi.ng/api";
import { ColorMode, IColor } from "./api";
import { ColorMode } from "./constants";
import type { ICopy, IDeref } from "@thi.ng/api";
import type { IColor } from "./api";

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

Expand Down
3 changes: 2 additions & 1 deletion packages/color/src/hcya-rgba.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { clamp01 } from "@thi.ng/math";
import { dot3, setC3 } from "@thi.ng/vectors";
import { ColorOp, RGB_LUMINANCE } 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
3 changes: 2 additions & 1 deletion packages/color/src/hcya.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { declareIndices, IVector } from "@thi.ng/vectors";
import { Color, ColorMode } 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 { ColorOp } from "./api";
import { clampH } from "./clamp";
import type { ColorOp } from "./api";

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

Expand Down
3 changes: 2 additions & 1 deletion packages/color/src/hsia.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { declareIndices, IVector } from "@thi.ng/vectors";
import { Color, ColorMode } 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
3 changes: 2 additions & 1 deletion packages/color/src/hsla-css.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { clamp01 } from "@thi.ng/math";
import { FF, PC, 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 { ColorOp } from "./api";
import { clampH } from "./clamp";
import type { ColorOp } from "./api";

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 { 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
3 changes: 2 additions & 1 deletion packages/color/src/hsla.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { declareIndices, IVector } from "@thi.ng/vectors";
import { Color, ColorMode } 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
4 changes: 2 additions & 2 deletions packages/color/src/hsva-css.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReadonlyColor } from "./api";
import { hsvaHsla } from "./hsva-hsla";
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 { ColorOp } from "./api";
import { clampH } from "./clamp";
import type { ColorOp } from "./api";

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 { 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
3 changes: 2 additions & 1 deletion packages/color/src/hsva.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { declareIndices, IVector } from "@thi.ng/vectors";
import { Color, ColorMode } 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
3 changes: 2 additions & 1 deletion packages/color/src/hue-rgba.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { clamp01 } from "@thi.ng/math";
import { setC4 } from "@thi.ng/vectors";
import { Color, Hue } 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
3 changes: 2 additions & 1 deletion packages/color/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./api";
export type * from "./api";
export * from "./constants";
export * from "./names";

export * from "./hcya-rgba";
Expand Down
4 changes: 2 additions & 2 deletions packages/color/src/int-css.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IDeref } from "@thi.ng/api";
import { U24 } from "@thi.ng/strings";
import { FF, INV8BIT } from "./api";
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
5 changes: 3 additions & 2 deletions packages/color/src/int-rgba.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IDeref } from "@thi.ng/api";
import { setC4 } from "@thi.ng/vectors";
import { Color, INV8BIT } from "./api";
import { INV8BIT } from "./constants";
import type { Color } from "./api";
import type { IDeref } from "@thi.ng/api";

export const int32Rgba = (out: Color | null, src: number | IDeref<number>) => {
src = typeof src === "number" ? src : src.deref();
Expand Down
Loading

0 comments on commit 592409c

Please sign in to comment.