Skip to content

Commit

Permalink
refactor(color): add/migrate types for presets
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Dec 29, 2020
1 parent 441cddb commit a8e815b
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 35 deletions.
173 changes: 173 additions & 0 deletions packages/color/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,179 @@ export type ColorMode =
| "xyz"
| "ycbcr";

export type CSSColorName =
| "aliceblue"
| "antiquewhite"
| "aqua"
| "aquamarine"
| "azure"
| "beige"
| "bisque"
| "black"
| "blanchedalmond"
| "blue"
| "blueviolet"
| "brown"
| "burlywood"
| "cadetblue"
| "chartreuse"
| "chocolate"
| "coral"
| "cornflowerblue"
| "cornsilk"
| "crimson"
| "cyan"
| "darkblue"
| "darkcyan"
| "darkgoldenrod"
| "darkgray"
| "darkgreen"
| "darkgrey"
| "darkkhaki"
| "darkmagenta"
| "darkolivegreen"
| "darkorange"
| "darkorchid"
| "darkred"
| "darksalmon"
| "darkseagreen"
| "darkslateblue"
| "darkslategray"
| "darkslategrey"
| "darkturquoise"
| "darkviolet"
| "deeppink"
| "deepskyblue"
| "dimgray"
| "dimgrey"
| "dodgerblue"
| "firebrick"
| "floralwhite"
| "forestgreen"
| "fuchsia"
| "gainsboro"
| "ghostwhite"
| "gold"
| "goldenrod"
| "gray"
| "grey"
| "green"
| "greenyellow"
| "honeydew"
| "hotpink"
| "indianred"
| "indigo"
| "ivory"
| "khaki"
| "lavender"
| "lavenderblush"
| "lawngreen"
| "lemonchiffon"
| "lightblue"
| "lightcoral"
| "lightcyan"
| "lightgoldenrodyellow"
| "lightgray"
| "lightgreen"
| "lightgrey"
| "lightpink"
| "lightsalmon"
| "lightseagreen"
| "lightskyblue"
| "lightslategray"
| "lightslategrey"
| "lightsteelblue"
| "lightyellow"
| "lime"
| "limegreen"
| "linen"
| "magenta"
| "maroon"
| "mediumaquamarine"
| "mediumblue"
| "mediumorchid"
| "mediumpurple"
| "mediumseagreen"
| "mediumslateblue"
| "mediumspringgreen"
| "mediumturquoise"
| "mediumvioletred"
| "midnightblue"
| "mintcream"
| "mistyrose"
| "moccasin"
| "navajowhite"
| "navy"
| "oldlace"
| "olive"
| "olivedrab"
| "orange"
| "orangered"
| "orchid"
| "palegoldenrod"
| "palegreen"
| "paleturquoise"
| "palevioletred"
| "papayawhip"
| "peachpuff"
| "peru"
| "pink"
| "plum"
| "powderblue"
| "purple"
| "red"
| "rosybrown"
| "royalblue"
| "saddlebrown"
| "salmon"
| "sandybrown"
| "seagreen"
| "seashell"
| "sienna"
| "silver"
| "skyblue"
| "slateblue"
| "slategray"
| "slategrey"
| "snow"
| "springgreen"
| "steelblue"
| "tan"
| "teal"
| "thistle"
| "tomato"
| "turquoise"
| "violet"
| "wheat"
| "white"
| "whitesmoke"
| "yellow"
| "yellowgreen";

export type CosineGradientPreset =
| "blue-cyan"
| "blue-magenta-orange"
| "blue-white-red"
| "cyan-magenta"
| "green-blue-orange"
| "green-cyan"
| "green-magenta"
| "green-red"
| "heat1"
| "magenta-green"
| "orange-blue"
| "orange-magenta-blue"
| "purple-orange-cyan"
| "rainbow1"
| "rainbow2"
| "rainbow3"
| "rainbow4"
| "red-blue"
| "yellow-green-blue"
| "yellow-magenta-cyan"
| "yellow-purple-magenta"
| "yellow-red";

export type Color = Vec;
export type ReadonlyColor = ReadonlyVec;

Expand Down
42 changes: 11 additions & 31 deletions packages/color/src/cosine-gradients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,19 @@ import {
tween,
zip,
} from "@thi.ng/transducers";
import type { Color, CosCoeffs, CosGradientSpec, ReadonlyColor } from "./api";
import type {
Color,
CosCoeffs,
CosGradientSpec,
CosineGradientPreset,
ReadonlyColor,
} from "./api";
import { clamp } from "./clamp";

export type GradientPresets = Record<
| "blue-cyan"
| "blue-magenta-orange"
| "blue-white-red"
| "cyan-magenta"
| "green-blue-orange"
| "green-cyan"
| "green-magenta"
| "green-red"
| "heat1"
| "magenta-green"
| "orange-blue"
| "orange-magenta-blue"
| "purple-orange-cyan"
| "rainbow1"
| "rainbow2"
| "rainbow3"
| "rainbow4"
| "red-blue"
| "yellow-green-blue"
| "yellow-magenta-cyan"
| "yellow-purple-magenta"
| "yellow-red",
CosGradientSpec
>;

// see http://dev.thi.ng/gradients/ - unlike the clojure version, these
// presets are for RGBA (though the alpha channel is configured to
// always be 1.0)
export const GRADIENTS: GradientPresets = {
// see http://dev.thi.ng/gradients/ - Note: unlike the original Clojure version,
// these presets are for RGBA (though the alpha channel is configured to always
// be 1.0)
export const GRADIENTS: Record<CosineGradientPreset, CosGradientSpec> = {
"blue-cyan": [
[0, 0.5, 0.5, 1],
[0, 0.5, 0.5, 0],
Expand Down
4 changes: 2 additions & 2 deletions packages/color/src/names.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IObjectOf } from "@thi.ng/api";
import type { CSSColorName } from "./api";

export const CSS_NAMES: IObjectOf<string> = {
export const CSS_NAMES: Record<CSSColorName, string> = {
aliceblue: "f0f8ff",
antiquewhite: "faebd7",
aqua: "0ff",
Expand Down
4 changes: 2 additions & 2 deletions packages/color/src/parse-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 type { Color, CSSColorName } from "./api";
import { INV8BIT } from "./constants";
import { hslaRgba } from "./hsla-rgba";
import { int32Rgba } from "./int-rgba";
Expand Down Expand Up @@ -34,7 +34,7 @@ export const parseCss = (col: string | IDeref<string>) => {
]);
}
} else {
const c = CSS_NAMES[col];
const c = CSS_NAMES[<CSSColorName>col];
!c && illegalArgs(`invalid color: "${col}"`);
return int32Rgba([], parseHex(c));
}
Expand Down

0 comments on commit a8e815b

Please sign in to comment.