Skip to content

Commit

Permalink
feat(color): major restructure, new types/conversions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: replace color classes w/ dynamically generated impls

- add ColorSpec, ColorType, ColorFactory types
- add defColor() color type factory based on declarative ColorSpec
- all color types now based on defColor()
- remove obsolete AColor class
- color factories now also act as converters
- add color() factory to wrap color in class for given mode
- remove CSS and Int types (use plain strings/ints now, and use css()
  or resolveAsCss() to convert to CSS strings)
- parseCss() now returns ParsedColor (circumvents circular deps)
- replace convert() w/ new simplified version
- add/update generic isGray(), isBlack(), isWhite(), luminance()
  • Loading branch information
postspectacular committed Jan 28, 2021
1 parent 97dda16 commit 6389f7c
Show file tree
Hide file tree
Showing 102 changed files with 1,280 additions and 1,226 deletions.
24 changes: 21 additions & 3 deletions packages/color/package.json
@@ -1,7 +1,7 @@
{
"name": "@thi.ng/color",
"version": "2.1.5",
"description": "Array-based color types, conversions, transformations, declarative theme generation, multi-color gradients, presets",
"description": "Array-based color types, conversions, transformations, declarative theme generation, gradients, presets",
"module": "./index.js",
"main": "./lib/index.js",
"umd:main": "./lib/index.umd.js",
Expand Down Expand Up @@ -67,7 +67,23 @@
"*.js",
"*.d.ts",
"lib",
"internal"
"api",
"css",
"hcy",
"hsi",
"hsl",
"hsv",
"int",
"internal",
"lab",
"lch",
"oklab",
"ops",
"rgb",
"srgb",
"xyy",
"xyz",
"ycc"
],
"keywords": [
"alpha",
Expand Down Expand Up @@ -103,5 +119,7 @@
"setTimeout": false
},
"sideEffects": false,
"thi.ng": {}
"thi.ng": {
"related": "pixel"
}
}
56 changes: 50 additions & 6 deletions packages/color/src/api.ts
@@ -1,14 +1,12 @@
import type { FnN, FnU, FnU2, Tuple } from "@thi.ng/api";
import type { FnN, FnU, FnU2, IDeref, Tuple } from "@thi.ng/api";
import type { IRandom } from "@thi.ng/random";
import type { ReadonlyVec, Vec } from "@thi.ng/vectors";
import type { IVector, ReadonlyVec, Vec } from "@thi.ng/vectors";

export type ColorMode =
| "css"
| "hcy"
| "hsi"
| "hsl"
| "hsv"
| "int"
| "lab"
| "lch"
| "oklab"
Expand Down Expand Up @@ -167,8 +165,8 @@ export type CSSColorName =
| "yellow"
| "yellowgreen"
// additions
| "transparent"
| "rebeccapurple";
| "rebeccapurple"
| "transparent";

export type ColorRangePreset =
| "light"
Expand Down Expand Up @@ -248,6 +246,52 @@ export interface IColor {
readonly mode: ColorMode;
}

export interface ChannelSpec {
/**
* @defaultValue [0,1]
*/
range?: [number, number];
default?: number;
}

export interface ColorSpec<M extends ColorMode, K extends string> {
mode: M;
channels: Record<K, ChannelSpec>;
order: readonly K[];
from: Partial<Record<ColorMode, ColorOp>> & { rgb: ColorOp };
}

export interface ColorFactory<T extends ColorType<any>> {
(
col: ColorType<any> | ParsedColor | string | number,
buf?: Color,
idx?: number,
stride?: number
): T;
(col?: Color, idx?: number, stride?: number): T;
(a: number, b: number, c: number, ...xs: number[]): T;

random(rnd?: IRandom): T;
}

export interface ColorType<T> extends IDeref<Color>, IVector<T> {
readonly mode: ColorMode;
readonly length: number;
buf: Color;
offset: number;
stride: number;
set(src: ReadonlyColor): this;
toJSON(): number[];
}

export class ParsedColor implements IDeref<Color> {
constructor(public readonly mode: ColorMode, public value: Color) {}

deref() {
return this.value;
}
}

export type Range = [number, number];

export interface ColorRange {
Expand Down
File renamed without changes.
@@ -1,4 +1,4 @@
import type { CSSColorName } from "./api";
import type { CSSColorName } from "../api";

export const CSS_NAMES: Record<CSSColorName, string> = {
aliceblue: "f0f8ff",
Expand Down
@@ -1,4 +1,4 @@
import type { SystemColors } from "./api";
import type { SystemColors } from "../api";

/**
* Default CSS system colors used by {@link parseCss}. Use
Expand Down
22 changes: 0 additions & 22 deletions packages/color/src/checks.ts

This file was deleted.

64 changes: 64 additions & 0 deletions packages/color/src/color.ts
@@ -0,0 +1,64 @@
import { isString } from "@thi.ng/checks";
import type {
Color,
ColorFactory,
ColorMode,
ColorType,
ParsedColor,
} from "./api";
import { hcy } from "./hcy/hcy";
import { hsi } from "./hsi/hsi";
import { hsl } from "./hsl/hsl";
import { hsv } from "./hsv/hsv";
import { lab } from "./lab/lab";
import { lch } from "./lch/lch";
import { oklab } from "./oklab/oklab";
import { rgb } from "./rgb/rgb";
import { srgb } from "./srgb/srgb";
import { xyy } from "./xyy/xyy";
import { xyz } from "./xyz/xyz";
import { ycc } from "./ycc/ycc";

const FACTORIES: Record<ColorMode, ColorFactory<any>> = {
hcy,
hsi,
hsl: hsl,
hsv: hsv,
lab,
lch,
oklab,
rgb,
srgb,
xyy,
xyz,
ycc,
};

export function color(
src: ParsedColor,
buf?: Color,
idx?: number,
stride?: number
): ColorType<any>;
export function color(
mode: ColorMode,
buf: Color,
idx?: number,
stride?: number
): ColorType<any>;
export function color(
src: any,
buf?: any,
idx?: number,
stride?: number
): ColorType<any> {
if (isString(src)) {
return FACTORIES[<ColorMode>src](buf, idx, stride);
}
if (buf) {
const res = FACTORIES[(<ParsedColor>src).mode](buf, idx, stride);
res.set(src.deref());
return res;
}
return FACTORIES[(<ParsedColor>src).mode](src.deref());
}

0 comments on commit 6389f7c

Please sign in to comment.