ggaction 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +27 -0
- package/LICENSE +21 -0
- package/README.md +108 -0
- package/package.json +84 -0
- package/src/AGENTS.md +119 -0
- package/src/ChartProgram.js +10 -0
- package/src/actions/boxPlots/components.js +88 -0
- package/src/actions/boxPlots/create.js +84 -0
- package/src/actions/boxPlots/index.js +10 -0
- package/src/actions/boxPlots/materialize.js +183 -0
- package/src/actions/boxPlots/options.js +147 -0
- package/src/actions/boxPlots/resolve.js +38 -0
- package/src/actions/canvas/actions.js +144 -0
- package/src/actions/canvas/index.js +6 -0
- package/src/actions/coordinates/actions.js +85 -0
- package/src/actions/coordinates/index.js +5 -0
- package/src/actions/data/box.js +40 -0
- package/src/actions/data/create.js +33 -0
- package/src/actions/data/density.js +62 -0
- package/src/actions/data/derived.js +52 -0
- package/src/actions/data/filter.js +168 -0
- package/src/actions/data/index.js +32 -0
- package/src/actions/data/interval.js +69 -0
- package/src/actions/data/regression.js +71 -0
- package/src/actions/data/shared.js +25 -0
- package/src/actions/encodings/appearance.js +243 -0
- package/src/actions/encodings/barWidth.js +60 -0
- package/src/actions/encodings/color.js +329 -0
- package/src/actions/encodings/density.js +225 -0
- package/src/actions/encodings/histogram.js +50 -0
- package/src/actions/encodings/index.js +23 -0
- package/src/actions/encodings/offset.js +104 -0
- package/src/actions/encodings/position/policies/area.js +24 -0
- package/src/actions/encodings/position/policies/bar.js +110 -0
- package/src/actions/encodings/position/policies/common.js +17 -0
- package/src/actions/encodings/position/policies/index.js +25 -0
- package/src/actions/encodings/position/policies/line.js +74 -0
- package/src/actions/encodings/position/policies/point.js +19 -0
- package/src/actions/encodings/position/policies/rule.js +17 -0
- package/src/actions/encodings/position/resolve.js +170 -0
- package/src/actions/encodings/position.js +176 -0
- package/src/actions/encodings/ranged.js +276 -0
- package/src/actions/encodings/ruleAppearance.js +47 -0
- package/src/actions/encodings/shared.js +164 -0
- package/src/actions/encodings/strokeDash.js +145 -0
- package/src/actions/errorBands/components.js +81 -0
- package/src/actions/errorBands/create.js +159 -0
- package/src/actions/errorBands/index.js +7 -0
- package/src/actions/errorBands/resolve.js +28 -0
- package/src/actions/errorBars/components.js +62 -0
- package/src/actions/errorBars/create.js +144 -0
- package/src/actions/errorBars/index.js +7 -0
- package/src/actions/errorBars/resolve.js +25 -0
- package/src/actions/guides/axes/axes.js +204 -0
- package/src/actions/guides/axes/axis.js +131 -0
- package/src/actions/guides/axes/index.js +17 -0
- package/src/actions/guides/axes/labels.js +231 -0
- package/src/actions/guides/axes/lines.js +149 -0
- package/src/actions/guides/axes/policy.js +155 -0
- package/src/actions/guides/axes/tickGroups.js +149 -0
- package/src/actions/guides/axes/ticks.js +179 -0
- package/src/actions/guides/axes/titles.js +264 -0
- package/src/actions/guides/grids/grid.js +228 -0
- package/src/actions/guides/grids/index.js +1 -0
- package/src/actions/guides/grids/resolve.js +258 -0
- package/src/actions/guides/guides.js +138 -0
- package/src/actions/guides/index.js +11 -0
- package/src/actions/guides/legends/categorical/actions.js +329 -0
- package/src/actions/guides/legends/categorical/components.js +140 -0
- package/src/actions/guides/legends/categorical/index.js +49 -0
- package/src/actions/guides/legends/categorical/layout.js +445 -0
- package/src/actions/guides/legends/categorical/options.js +188 -0
- package/src/actions/guides/legends/categorical/recipes.js +120 -0
- package/src/actions/guides/legends/categorical/resolve.js +131 -0
- package/src/actions/guides/legends/categorical/symbols.js +280 -0
- package/src/actions/guides/legends/categorical/validation.js +27 -0
- package/src/actions/guides/legends/continuous/common.js +273 -0
- package/src/actions/guides/legends/continuous/gradient.js +363 -0
- package/src/actions/guides/legends/continuous/index.js +25 -0
- package/src/actions/guides/legends/continuous/interval.js +234 -0
- package/src/actions/guides/legends/continuous/opacity.js +382 -0
- package/src/actions/guides/legends/edit.js +314 -0
- package/src/actions/guides/legends/index.js +9 -0
- package/src/actions/guides/legends/size.js +220 -0
- package/src/actions/guides/tickValues.js +84 -0
- package/src/actions/index.js +31 -0
- package/src/actions/intervals/resolve.js +352 -0
- package/src/actions/marks/area.js +369 -0
- package/src/actions/marks/bar/create.js +46 -0
- package/src/actions/marks/bar/edit.js +90 -0
- package/src/actions/marks/bar/index.js +9 -0
- package/src/actions/marks/bar/materialize.js +130 -0
- package/src/actions/marks/index.js +13 -0
- package/src/actions/marks/line.js +248 -0
- package/src/actions/marks/point.js +420 -0
- package/src/actions/marks/rule.js +287 -0
- package/src/actions/marks/shared.js +112 -0
- package/src/actions/primitives/createGraphics.js +163 -0
- package/src/actions/primitives/editGraphics.js +208 -0
- package/src/actions/primitives/index.js +9 -0
- package/src/actions/primitives/semantic.js +168 -0
- package/src/actions/primitives/semanticValue.js +182 -0
- package/src/actions/regression/components.js +216 -0
- package/src/actions/regression/create.js +102 -0
- package/src/actions/regression/index.js +23 -0
- package/src/actions/regression/resolve.js +57 -0
- package/src/actions/scales/consumers.js +283 -0
- package/src/actions/scales/create.js +148 -0
- package/src/actions/scales/definitions.js +319 -0
- package/src/actions/scales/edit.js +290 -0
- package/src/actions/scales/index.js +11 -0
- package/src/actions/scales/materialize.js +466 -0
- package/src/actions/scales/quantitativeColor.js +45 -0
- package/src/actions/selection/actions.js +384 -0
- package/src/actions/selection/index.js +23 -0
- package/src/actions/titles/actions.js +235 -0
- package/src/actions/titles/index.js +19 -0
- package/src/actions/titles/resolve.js +321 -0
- package/src/core/ChartProgram.js +316 -0
- package/src/core/action.js +120 -0
- package/src/core/identifiers.js +28 -0
- package/src/core/immutable.js +94 -0
- package/src/core/specs.js +29 -0
- package/src/core/textMetrics.js +23 -0
- package/src/core/validation.js +54 -0
- package/src/core/vocabulary.js +58 -0
- package/src/extension.js +2 -0
- package/src/grammar/aggregate.js +259 -0
- package/src/grammar/areaSeries.js +217 -0
- package/src/grammar/bars/aggregate.js +98 -0
- package/src/grammar/bars/geometry.js +54 -0
- package/src/grammar/bars/policy.js +90 -0
- package/src/grammar/boxPlot.js +140 -0
- package/src/grammar/coordinates.js +25 -0
- package/src/grammar/curveCommands.js +356 -0
- package/src/grammar/density.js +296 -0
- package/src/grammar/filter.js +169 -0
- package/src/grammar/histogram.js +332 -0
- package/src/grammar/interval.js +343 -0
- package/src/grammar/lineSeries.js +197 -0
- package/src/grammar/markFilter.js +46 -0
- package/src/grammar/markSelection.js +326 -0
- package/src/grammar/palettes.js +208 -0
- package/src/grammar/pathCommands.js +86 -0
- package/src/grammar/pointShapes.js +164 -0
- package/src/grammar/positionCompatibility.js +32 -0
- package/src/grammar/regression.js +578 -0
- package/src/grammar/ruleAppearance.js +12 -0
- package/src/grammar/rules.js +90 -0
- package/src/grammar/scales/appearance.js +185 -0
- package/src/grammar/scales/color.js +349 -0
- package/src/grammar/scales/continuous.js +111 -0
- package/src/grammar/scales/definition.js +153 -0
- package/src/grammar/scales/discretized.js +159 -0
- package/src/grammar/scales/fields.js +133 -0
- package/src/grammar/scales/index.js +12 -0
- package/src/grammar/scales/mapping.js +27 -0
- package/src/grammar/scales/ordinal.js +221 -0
- package/src/grammar/scales/policies.js +50 -0
- package/src/grammar/scales/temporal.js +53 -0
- package/src/grammar/scales/transformed.js +236 -0
- package/src/grammar/scales/types.js +102 -0
- package/src/grammar/scales/validation.js +66 -0
- package/src/grammar/scales.js +1 -0
- package/src/grammar/schemas/concreteGraphic.js +73 -0
- package/src/grammar/schemas/graphic.js +131 -0
- package/src/grammar/schemas/graphicBounds.js +146 -0
- package/src/grammar/schemas/graphicTree.js +133 -0
- package/src/grammar/schemas/mark.js +4 -0
- package/src/grammar/schemas/semanticPath.js +174 -0
- package/src/grammar/seriesLayout.js +76 -0
- package/src/grammar/ticks.js +149 -0
- package/src/grammar/transforms.js +37 -0
- package/src/index.js +2 -0
- package/src/layout/canvas.js +114 -0
- package/src/layout/grid.js +17 -0
- package/src/layout/legend.js +51 -0
- package/src/layout/text.js +62 -0
- package/src/layout/title.js +107 -0
- package/src/materialization/bars/aggregate.js +184 -0
- package/src/materialization/bars/grouped.js +117 -0
- package/src/materialization/bars/histogram.js +163 -0
- package/src/materialization/bars/ranged.js +53 -0
- package/src/materialization/bars/resolve.js +56 -0
- package/src/materialization/dataProvenance.js +15 -0
- package/src/materialization/density.js +29 -0
- package/src/materialization/dependencies.js +134 -0
- package/src/materialization/encodings.js +50 -0
- package/src/materialization/graphicHierarchy.js +104 -0
- package/src/materialization/legends.js +20 -0
- package/src/materialization/marks.js +110 -0
- package/src/materialization/planner.js +38 -0
- package/src/materialization/selection/filter.js +40 -0
- package/src/materialization/selection/items.js +503 -0
- package/src/materialization/selection/path.js +37 -0
- package/src/materialization/selection/point.js +118 -0
- package/src/materialization/selection/policies/area.js +10 -0
- package/src/materialization/selection/policies/bar.js +10 -0
- package/src/materialization/selection/policies/index.js +40 -0
- package/src/materialization/selection/policies/line.js +10 -0
- package/src/materialization/selection/policies/point.js +10 -0
- package/src/materialization/selection/policies/rule.js +10 -0
- package/src/materialization/selection/state.js +67 -0
- package/src/materialization/selection/styles.js +183 -0
- package/src/renderers/canvas/circle.js +62 -0
- package/src/renderers/canvas/index.js +132 -0
- package/src/renderers/canvas/line.js +54 -0
- package/src/renderers/canvas/path.js +98 -0
- package/src/renderers/canvas/rect.js +57 -0
- package/src/renderers/canvas/text.js +96 -0
- package/src/renderers/canvas/validation.js +23 -0
- package/src/renderers/png.js +29 -0
- package/src/selectors/coordinates.js +13 -0
- package/src/selectors/datasets.js +13 -0
- package/src/selectors/index.js +4 -0
- package/src/selectors/layers.js +32 -0
- package/src/selectors/scales.js +21 -0
- package/src/theme/defaults.js +13 -0
- package/types/extension.d.ts +14 -0
- package/types/index.d.ts +81 -0
- package/types/png.d.ts +14 -0
- package/types/program.d.ts +1096 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { cloneAndFreeze, isPlainObject } from "../../core/immutable.js";
|
|
2
|
+
import { validateUnitInterval } from "../../core/validation.js";
|
|
3
|
+
import { POINT_SHAPES } from "../pointShapes.js";
|
|
4
|
+
import {
|
|
5
|
+
normalizePalette,
|
|
6
|
+
resolvePalette
|
|
7
|
+
} from "../palettes.js";
|
|
8
|
+
|
|
9
|
+
export const DASH10 = cloneAndFreeze([
|
|
10
|
+
[], [8, 4], [3, 3], [12, 4], [8, 3, 2, 3],
|
|
11
|
+
[12, 3, 3, 3], [2, 2], [10, 3, 2, 3, 2, 3], [14, 4, 4, 4], [6, 2, 2, 2]
|
|
12
|
+
]);
|
|
13
|
+
export const NAMED_DASH_PATTERNS = cloneAndFreeze({
|
|
14
|
+
solid: [],
|
|
15
|
+
dashed: [6, 4],
|
|
16
|
+
dotted: [1, 3],
|
|
17
|
+
dashdot: [6, 3, 1, 3]
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export { POINT_SHAPES } from "../pointShapes.js";
|
|
21
|
+
export const DEFAULT_SIZE_RANGE = cloneAndFreeze([24, 196]);
|
|
22
|
+
export const DEFAULT_OPACITY_RANGE = cloneAndFreeze([0.2, 1]);
|
|
23
|
+
|
|
24
|
+
export function validateColorRange(range) {
|
|
25
|
+
if (range === "auto") return range;
|
|
26
|
+
if (Array.isArray(range)) {
|
|
27
|
+
if (
|
|
28
|
+
range.length === 0 ||
|
|
29
|
+
!range.every(color => typeof color === "string" && color.length > 0)
|
|
30
|
+
) {
|
|
31
|
+
throw new TypeError("Color range must contain non-empty color strings.");
|
|
32
|
+
}
|
|
33
|
+
return cloneAndFreeze(range);
|
|
34
|
+
}
|
|
35
|
+
if (
|
|
36
|
+
!isPlainObject(range) ||
|
|
37
|
+
Object.keys(range).length !== 1 ||
|
|
38
|
+
!Object.hasOwn(range, "palette")
|
|
39
|
+
) {
|
|
40
|
+
throw new Error("Color range must contain only a palette descriptor.");
|
|
41
|
+
}
|
|
42
|
+
normalizePalette(range.palette);
|
|
43
|
+
return cloneAndFreeze(range);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isDashPattern(pattern) {
|
|
47
|
+
return (
|
|
48
|
+
Array.isArray(pattern) &&
|
|
49
|
+
pattern.length % 2 === 0 &&
|
|
50
|
+
pattern.every(value => Number.isFinite(value) && value >= 0) &&
|
|
51
|
+
(pattern.length === 0 || pattern.some(value => value > 0))
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function normalizeStrokeDashPattern(pattern) {
|
|
56
|
+
if (typeof pattern === "string") {
|
|
57
|
+
const resolved = NAMED_DASH_PATTERNS[pattern];
|
|
58
|
+
if (resolved === undefined) {
|
|
59
|
+
throw new Error(`Unknown stroke dash style "${pattern}".`);
|
|
60
|
+
}
|
|
61
|
+
return cloneAndFreeze(resolved);
|
|
62
|
+
}
|
|
63
|
+
if (!isDashPattern(pattern)) {
|
|
64
|
+
throw new TypeError(
|
|
65
|
+
"Stroke dash pattern must be a named style or an empty or nonzero even-length array of non-negative finite numbers."
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return cloneAndFreeze(pattern);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function validateStrokeDashRange(range) {
|
|
72
|
+
if (range === "auto") return range;
|
|
73
|
+
if (!Array.isArray(range) || range.length === 0) {
|
|
74
|
+
throw new TypeError(
|
|
75
|
+
"StrokeDash range must contain one or more named styles or direct patterns."
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
for (const pattern of range) normalizeStrokeDashPattern(pattern);
|
|
79
|
+
return cloneAndFreeze(range);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function validateShapeRange(range) {
|
|
83
|
+
if (range === "auto") return range;
|
|
84
|
+
if (
|
|
85
|
+
!Array.isArray(range) ||
|
|
86
|
+
range.length === 0 ||
|
|
87
|
+
!range.every(shape => POINT_SHAPES.includes(shape)) ||
|
|
88
|
+
new Set(range).size !== range.length
|
|
89
|
+
) {
|
|
90
|
+
throw new TypeError(
|
|
91
|
+
"Shape range must contain unique supported point shapes."
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
return cloneAndFreeze(range);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function validateSizeRange(range) {
|
|
98
|
+
if (range === "auto") return range;
|
|
99
|
+
if (
|
|
100
|
+
!Array.isArray(range) ||
|
|
101
|
+
range.length !== 2 ||
|
|
102
|
+
!range.every(value => Number.isFinite(value) && value >= 0) ||
|
|
103
|
+
range[0] > range[1]
|
|
104
|
+
) {
|
|
105
|
+
throw new TypeError(
|
|
106
|
+
"Size range must be an ascending pair of non-negative finite areas."
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
return cloneAndFreeze(range);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function validateOpacityRange(range) {
|
|
113
|
+
if (range === "auto") return range;
|
|
114
|
+
if (
|
|
115
|
+
!Array.isArray(range) ||
|
|
116
|
+
range.length !== 2 ||
|
|
117
|
+
!range.every(value => Number.isFinite(value) && value >= 0 && value <= 1)
|
|
118
|
+
) {
|
|
119
|
+
throw new TypeError(
|
|
120
|
+
"Opacity range must contain two finite values from 0 to 1."
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return cloneAndFreeze(range);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function validateOpacityValue(value, label = "Opacity") {
|
|
127
|
+
return validateUnitInterval(value, label);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function validateSemanticScaleRange(range) {
|
|
131
|
+
if (range === "auto") return range;
|
|
132
|
+
if (Array.isArray(range) && range.length > 0) return cloneAndFreeze(range);
|
|
133
|
+
if (isPlainObject(range) && Object.hasOwn(range, "palette")) {
|
|
134
|
+
normalizePalette(range.palette);
|
|
135
|
+
return cloneAndFreeze(range);
|
|
136
|
+
}
|
|
137
|
+
throw new TypeError("Scale range has an unsupported value.");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function resolveColorRange(range, domainCount) {
|
|
141
|
+
const validated = validateColorRange(range);
|
|
142
|
+
if (validated === "auto") return TABLEAU10;
|
|
143
|
+
if (Array.isArray(validated)) return validated;
|
|
144
|
+
return resolvePalette(validated.palette, domainCount);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function resolveStrokeDashRange(range) {
|
|
148
|
+
const validated = validateStrokeDashRange(range);
|
|
149
|
+
return validated === "auto"
|
|
150
|
+
? DASH10
|
|
151
|
+
: cloneAndFreeze(validated.map(normalizeStrokeDashPattern));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function resolveShapeRange(range) {
|
|
155
|
+
const validated = validateShapeRange(range);
|
|
156
|
+
return validated === "auto" ? POINT_SHAPES : validated;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function resolveSizeRange(range) {
|
|
160
|
+
const validated = validateSizeRange(range);
|
|
161
|
+
return validated === "auto" ? DEFAULT_SIZE_RANGE : validated;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function resolveOpacityRange(range) {
|
|
165
|
+
const validated = validateOpacityRange(range);
|
|
166
|
+
return validated === "auto" ? DEFAULT_OPACITY_RANGE : validated;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function mapOrdinalValues(values, domain, range, options = {}) {
|
|
170
|
+
const hasUnknown = Object.hasOwn(options, "unknown");
|
|
171
|
+
const indices = new Map(domain.map((value, index) => [value, index]));
|
|
172
|
+
return cloneAndFreeze(values.map(value => {
|
|
173
|
+
const index = indices.get(value);
|
|
174
|
+
if (index === undefined) {
|
|
175
|
+
if (hasUnknown) return options.unknown;
|
|
176
|
+
throw new Error(`Value "${value}" is outside the ordinal domain.`);
|
|
177
|
+
}
|
|
178
|
+
return range[index % range.length];
|
|
179
|
+
}));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export const TABLEAU10 = resolvePalette(
|
|
183
|
+
{ name: "tableau10", count: 10 },
|
|
184
|
+
10
|
|
185
|
+
);
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { cloneAndFreeze, isPlainObject } from "../../core/immutable.js";
|
|
2
|
+
import {
|
|
3
|
+
normalizePalette,
|
|
4
|
+
resolveContinuousPalette
|
|
5
|
+
} from "../palettes.js";
|
|
6
|
+
|
|
7
|
+
export const CONTINUOUS_COLOR_INTERPOLATIONS = cloneAndFreeze([
|
|
8
|
+
"rgb",
|
|
9
|
+
"hsl",
|
|
10
|
+
"hsl-long",
|
|
11
|
+
"lab",
|
|
12
|
+
"hcl",
|
|
13
|
+
"hcl-long",
|
|
14
|
+
"cubehelix",
|
|
15
|
+
"cubehelix-long"
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
const BASIC_COLORS = Object.freeze({
|
|
19
|
+
black: "#000000",
|
|
20
|
+
blue: "#0000ff",
|
|
21
|
+
cyan: "#00ffff",
|
|
22
|
+
gray: "#808080",
|
|
23
|
+
green: "#008000",
|
|
24
|
+
grey: "#808080",
|
|
25
|
+
magenta: "#ff00ff",
|
|
26
|
+
navy: "#000080",
|
|
27
|
+
orange: "#ffa500",
|
|
28
|
+
purple: "#800080",
|
|
29
|
+
red: "#ff0000",
|
|
30
|
+
teal: "#008080",
|
|
31
|
+
white: "#ffffff",
|
|
32
|
+
yellow: "#ffff00"
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
function clamp(value, minimum = 0, maximum = 1) {
|
|
36
|
+
return Math.max(minimum, Math.min(maximum, value));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function hue(value) {
|
|
40
|
+
return ((value % 360) + 360) % 360;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function hslToRgb({ h, s, l }) {
|
|
44
|
+
const chroma = (1 - Math.abs(2 * l - 1)) * s;
|
|
45
|
+
const segment = hue(h) / 60;
|
|
46
|
+
const second = chroma * (1 - Math.abs(segment % 2 - 1));
|
|
47
|
+
const [red, green, blue] = segment < 1 ? [chroma, second, 0]
|
|
48
|
+
: segment < 2 ? [second, chroma, 0]
|
|
49
|
+
: segment < 3 ? [0, chroma, second]
|
|
50
|
+
: segment < 4 ? [0, second, chroma]
|
|
51
|
+
: segment < 5 ? [second, 0, chroma]
|
|
52
|
+
: [chroma, 0, second];
|
|
53
|
+
const match = l - chroma / 2;
|
|
54
|
+
return { r: red + match, g: green + match, b: blue + match };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function parseColor(value) {
|
|
58
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
59
|
+
throw new TypeError("Continuous color range requires non-empty color strings.");
|
|
60
|
+
}
|
|
61
|
+
const normalized = BASIC_COLORS[value.toLowerCase()] ?? value.toLowerCase();
|
|
62
|
+
let match = normalized.match(/^#([0-9a-f]{3})$/i);
|
|
63
|
+
if (match) {
|
|
64
|
+
return Object.fromEntries(["r", "g", "b"].map((channel, index) => [
|
|
65
|
+
channel,
|
|
66
|
+
Number.parseInt(match[1][index].repeat(2), 16) / 255
|
|
67
|
+
]));
|
|
68
|
+
}
|
|
69
|
+
match = normalized.match(/^#([0-9a-f]{6})$/i);
|
|
70
|
+
if (match) {
|
|
71
|
+
return {
|
|
72
|
+
r: Number.parseInt(match[1].slice(0, 2), 16) / 255,
|
|
73
|
+
g: Number.parseInt(match[1].slice(2, 4), 16) / 255,
|
|
74
|
+
b: Number.parseInt(match[1].slice(4, 6), 16) / 255
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
match = normalized.match(
|
|
78
|
+
/^rgb\(\s*([\d.]+)%?\s*,\s*([\d.]+)%?\s*,\s*([\d.]+)%?\s*\)$/
|
|
79
|
+
);
|
|
80
|
+
if (match) {
|
|
81
|
+
const percentage = normalized.includes("%");
|
|
82
|
+
const divisor = percentage ? 100 : 255;
|
|
83
|
+
const channels = match.slice(1).map(item => Number(item) / divisor);
|
|
84
|
+
if (channels.every(item => Number.isFinite(item) && item >= 0 && item <= 1)) {
|
|
85
|
+
return { r: channels[0], g: channels[1], b: channels[2] };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
match = normalized.match(
|
|
89
|
+
/^hsl\(\s*([-\d.]+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*\)$/
|
|
90
|
+
);
|
|
91
|
+
if (match) {
|
|
92
|
+
const h = Number(match[1]);
|
|
93
|
+
const s = Number(match[2]) / 100;
|
|
94
|
+
const l = Number(match[3]) / 100;
|
|
95
|
+
if ([h, s, l].every(Number.isFinite) && s >= 0 && s <= 1 && l >= 0 && l <= 1) {
|
|
96
|
+
return hslToRgb({ h, s, l });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
throw new Error(`Unsupported continuous color "${value}".`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function rgbToHex({ r, g, b }) {
|
|
103
|
+
return `#${[r, g, b].map(channel =>
|
|
104
|
+
Math.round(clamp(channel) * 255).toString(16).padStart(2, "0")
|
|
105
|
+
).join("")}`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function rgbToHsl({ r, g, b }) {
|
|
109
|
+
const maximum = Math.max(r, g, b);
|
|
110
|
+
const minimum = Math.min(r, g, b);
|
|
111
|
+
const delta = maximum - minimum;
|
|
112
|
+
const l = (maximum + minimum) / 2;
|
|
113
|
+
if (delta === 0) return { h: 0, s: 0, l };
|
|
114
|
+
const s = delta / (1 - Math.abs(2 * l - 1));
|
|
115
|
+
const raw = maximum === r
|
|
116
|
+
? ((g - b) / delta) % 6
|
|
117
|
+
: maximum === g
|
|
118
|
+
? (b - r) / delta + 2
|
|
119
|
+
: (r - g) / delta + 4;
|
|
120
|
+
return { h: hue(raw * 60), s, l };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function linearRgb(channel) {
|
|
124
|
+
return channel <= 0.04045
|
|
125
|
+
? channel / 12.92
|
|
126
|
+
: ((channel + 0.055) / 1.055) ** 2.4;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function encodedRgb(channel) {
|
|
130
|
+
return channel <= 0.0031308
|
|
131
|
+
? 12.92 * channel
|
|
132
|
+
: 1.055 * channel ** (1 / 2.4) - 0.055;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function rgbToLab(color) {
|
|
136
|
+
const r = linearRgb(color.r);
|
|
137
|
+
const g = linearRgb(color.g);
|
|
138
|
+
const b = linearRgb(color.b);
|
|
139
|
+
const x = (0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / 0.95047;
|
|
140
|
+
const y = 0.2126729 * r + 0.7151522 * g + 0.072175 * b;
|
|
141
|
+
const z = (0.0193339 * r + 0.119192 * g + 0.9503041 * b) / 1.08883;
|
|
142
|
+
const convert = value => value > 216 / 24389
|
|
143
|
+
? Math.cbrt(value)
|
|
144
|
+
: 841 / 108 * value + 4 / 29;
|
|
145
|
+
const fx = convert(x);
|
|
146
|
+
const fy = convert(y);
|
|
147
|
+
const fz = convert(z);
|
|
148
|
+
return { l: 116 * fy - 16, a: 500 * (fx - fy), b: 200 * (fy - fz) };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function labToRgb({ l, a, b }) {
|
|
152
|
+
const fy = (l + 16) / 116;
|
|
153
|
+
const fx = fy + a / 500;
|
|
154
|
+
const fz = fy - b / 200;
|
|
155
|
+
const convert = value => value ** 3 > 216 / 24389
|
|
156
|
+
? value ** 3
|
|
157
|
+
: 108 / 841 * (value - 4 / 29);
|
|
158
|
+
const x = 0.95047 * convert(fx);
|
|
159
|
+
const y = convert(fy);
|
|
160
|
+
const z = 1.08883 * convert(fz);
|
|
161
|
+
return {
|
|
162
|
+
r: encodedRgb(3.2404542 * x - 1.5371385 * y - 0.4985314 * z),
|
|
163
|
+
g: encodedRgb(-0.969266 * x + 1.8760108 * y + 0.041556 * z),
|
|
164
|
+
b: encodedRgb(0.0556434 * x - 0.2040259 * y + 1.0572252 * z)
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function labToHcl({ l, a, b }) {
|
|
169
|
+
return { h: hue(Math.atan2(b, a) * 180 / Math.PI), c: Math.hypot(a, b), l };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function hclToLab({ h, c, l }) {
|
|
173
|
+
const radians = h * Math.PI / 180;
|
|
174
|
+
return { l, a: Math.cos(radians) * c, b: Math.sin(radians) * c };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function rgbToCubehelix({ r, g, b }) {
|
|
178
|
+
const A = -0.14861;
|
|
179
|
+
const B = 1.78277;
|
|
180
|
+
const C = -0.29227;
|
|
181
|
+
const D = -0.90649;
|
|
182
|
+
const E = 1.97294;
|
|
183
|
+
const ED = E * D;
|
|
184
|
+
const EB = E * B;
|
|
185
|
+
const BC_DA = B * C - D * A;
|
|
186
|
+
const l = (BC_DA * b + ED * r - EB * g) / (BC_DA + ED - EB);
|
|
187
|
+
const blue = b - l;
|
|
188
|
+
const k = (E * (g - l) - C * blue) / D;
|
|
189
|
+
const denominator = E * l * (1 - l);
|
|
190
|
+
return {
|
|
191
|
+
h: hue(Math.atan2(k, blue) * 180 / Math.PI - 120),
|
|
192
|
+
s: denominator === 0 ? 0 : Math.hypot(k, blue) / denominator,
|
|
193
|
+
l
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function cubehelixToRgb({ h, s, l }) {
|
|
198
|
+
const angle = (h + 120) * Math.PI / 180;
|
|
199
|
+
const amplitude = s * l * (1 - l);
|
|
200
|
+
const cosine = Math.cos(angle);
|
|
201
|
+
const sine = Math.sin(angle);
|
|
202
|
+
return {
|
|
203
|
+
r: l + amplitude * (-0.14861 * cosine + 1.78277 * sine),
|
|
204
|
+
g: l + amplitude * (-0.29227 * cosine - 0.90649 * sine),
|
|
205
|
+
b: l + amplitude * (1.97294 * cosine)
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function interpolateHue(left, right, amount, long) {
|
|
210
|
+
let delta = right - left;
|
|
211
|
+
if (!long) delta = ((delta + 540) % 360) - 180;
|
|
212
|
+
return hue(left + delta * amount);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function mix(left, right, amount, method) {
|
|
216
|
+
const linear = key => left[key] + (right[key] - left[key]) * amount;
|
|
217
|
+
if (method === "rgb") {
|
|
218
|
+
return { r: linear("r"), g: linear("g"), b: linear("b") };
|
|
219
|
+
}
|
|
220
|
+
if (method.startsWith("hsl")) {
|
|
221
|
+
const a = rgbToHsl(left);
|
|
222
|
+
const b = rgbToHsl(right);
|
|
223
|
+
return hslToRgb({
|
|
224
|
+
h: interpolateHue(a.h, b.h, amount, method.endsWith("-long")),
|
|
225
|
+
s: a.s + (b.s - a.s) * amount,
|
|
226
|
+
l: a.l + (b.l - a.l) * amount
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
if (method === "lab") {
|
|
230
|
+
const a = rgbToLab(left);
|
|
231
|
+
const b = rgbToLab(right);
|
|
232
|
+
return labToRgb({
|
|
233
|
+
l: a.l + (b.l - a.l) * amount,
|
|
234
|
+
a: a.a + (b.a - a.a) * amount,
|
|
235
|
+
b: a.b + (b.b - a.b) * amount
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
if (method.startsWith("hcl")) {
|
|
239
|
+
const a = labToHcl(rgbToLab(left));
|
|
240
|
+
const b = labToHcl(rgbToLab(right));
|
|
241
|
+
return labToRgb(hclToLab({
|
|
242
|
+
h: interpolateHue(a.h, b.h, amount, method.endsWith("-long")),
|
|
243
|
+
c: a.c + (b.c - a.c) * amount,
|
|
244
|
+
l: a.l + (b.l - a.l) * amount
|
|
245
|
+
}));
|
|
246
|
+
}
|
|
247
|
+
const a = rgbToCubehelix(left);
|
|
248
|
+
const b = rgbToCubehelix(right);
|
|
249
|
+
return cubehelixToRgb({
|
|
250
|
+
h: interpolateHue(a.h, b.h, amount, method.endsWith("-long")),
|
|
251
|
+
s: a.s + (b.s - a.s) * amount,
|
|
252
|
+
l: a.l + (b.l - a.l) * amount
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function validateContinuousColorInterpolation(value) {
|
|
257
|
+
if (!CONTINUOUS_COLOR_INTERPOLATIONS.includes(value)) {
|
|
258
|
+
throw new Error(`Unsupported continuous color interpolation "${value}".`);
|
|
259
|
+
}
|
|
260
|
+
return value;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export function validateSequentialColorRange(value) {
|
|
264
|
+
if (value === "auto") return value;
|
|
265
|
+
if (Array.isArray(value)) {
|
|
266
|
+
if (value.length < 2) {
|
|
267
|
+
throw new TypeError("Sequential color range requires at least two colors.");
|
|
268
|
+
}
|
|
269
|
+
value.forEach(parseColor);
|
|
270
|
+
return cloneAndFreeze(value);
|
|
271
|
+
}
|
|
272
|
+
if (
|
|
273
|
+
!isPlainObject(value) ||
|
|
274
|
+
Object.keys(value).length !== 1 ||
|
|
275
|
+
!Object.hasOwn(value, "palette")
|
|
276
|
+
) {
|
|
277
|
+
throw new TypeError("Sequential color range requires colors or a palette descriptor.");
|
|
278
|
+
}
|
|
279
|
+
const palette = normalizePalette(value.palette);
|
|
280
|
+
if (palette.count !== undefined) {
|
|
281
|
+
throw new Error("Continuous palette does not accept count.");
|
|
282
|
+
}
|
|
283
|
+
return cloneAndFreeze({ palette });
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function resolveSequentialColorStops(value) {
|
|
287
|
+
const range = validateSequentialColorRange(value);
|
|
288
|
+
if (range === "auto") return resolveContinuousPalette("viridis");
|
|
289
|
+
return Array.isArray(range)
|
|
290
|
+
? cloneAndFreeze(range.map(color => rgbToHex(parseColor(color))))
|
|
291
|
+
: resolveContinuousPalette(range.palette);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function interpolateColorStops(stops, position, interpolation = "rgb") {
|
|
295
|
+
const method = validateContinuousColorInterpolation(interpolation);
|
|
296
|
+
if (!Array.isArray(stops) || stops.length < 2) {
|
|
297
|
+
throw new TypeError("Color interpolation requires at least two stops.");
|
|
298
|
+
}
|
|
299
|
+
const colors = stops.map(parseColor);
|
|
300
|
+
const bounded = clamp(position);
|
|
301
|
+
const scaled = bounded * (colors.length - 1);
|
|
302
|
+
const left = Math.floor(scaled);
|
|
303
|
+
const right = Math.min(colors.length - 1, left + 1);
|
|
304
|
+
if (method === "rgb") {
|
|
305
|
+
const leftHex = rgbToHex(colors[left]);
|
|
306
|
+
const rightHex = rgbToHex(colors[right]);
|
|
307
|
+
const amount = scaled - left;
|
|
308
|
+
return `#${[1, 3, 5].map(offset =>
|
|
309
|
+
Math.round(
|
|
310
|
+
Number.parseInt(leftHex.slice(offset, offset + 2), 16) +
|
|
311
|
+
(Number.parseInt(rightHex.slice(offset, offset + 2), 16) -
|
|
312
|
+
Number.parseInt(leftHex.slice(offset, offset + 2), 16)) * amount
|
|
313
|
+
).toString(16).padStart(2, "0")
|
|
314
|
+
).join("")}`;
|
|
315
|
+
}
|
|
316
|
+
return rgbToHex(mix(colors[left], colors[right], scaled - left, method));
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function mapSequentialColors(
|
|
320
|
+
values,
|
|
321
|
+
domain,
|
|
322
|
+
stops,
|
|
323
|
+
options = {}
|
|
324
|
+
) {
|
|
325
|
+
const { interpolation = "rgb", clamp: shouldClamp = false } = options;
|
|
326
|
+
const hasUnknown = Object.hasOwn(options, "unknown");
|
|
327
|
+
if (!Array.isArray(domain) || domain.length !== 2 || !domain.every(Number.isFinite)) {
|
|
328
|
+
throw new TypeError("Sequential color domain must contain two finite numbers.");
|
|
329
|
+
}
|
|
330
|
+
if (!Array.isArray(values) || (!hasUnknown && !values.every(Number.isFinite))) {
|
|
331
|
+
throw new TypeError("Sequential color values must be finite numbers.");
|
|
332
|
+
}
|
|
333
|
+
if (typeof shouldClamp !== "boolean") {
|
|
334
|
+
throw new TypeError("Sequential color clamp must be a boolean.");
|
|
335
|
+
}
|
|
336
|
+
const [start, end] = domain;
|
|
337
|
+
const constant = start === end;
|
|
338
|
+
return cloneAndFreeze(values.map(value => {
|
|
339
|
+
if (!Number.isFinite(value)) {
|
|
340
|
+
if (hasUnknown) return options.unknown;
|
|
341
|
+
throw new TypeError("Sequential color values must be finite numbers.");
|
|
342
|
+
}
|
|
343
|
+
const raw = constant ? 0.5 : (value - start) / (end - start);
|
|
344
|
+
if (!shouldClamp && (raw < 0 || raw > 1)) {
|
|
345
|
+
return interpolateColorStops(stops, raw < 0 ? 0 : 1, interpolation);
|
|
346
|
+
}
|
|
347
|
+
return interpolateColorStops(stops, clamp(raw), interpolation);
|
|
348
|
+
}));
|
|
349
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { cloneAndFreeze, isPlainObject } from "../../core/immutable.js";
|
|
2
|
+
import { niceTimeDomain } from "./temporal.js";
|
|
3
|
+
import {
|
|
4
|
+
validatePair,
|
|
5
|
+
validatePositionChannel,
|
|
6
|
+
validateScaleDomain,
|
|
7
|
+
validateScaleRange
|
|
8
|
+
} from "./validation.js";
|
|
9
|
+
|
|
10
|
+
export function resolveScaleDomain(domain, values) {
|
|
11
|
+
const validated = validateScaleDomain(domain);
|
|
12
|
+
if (validated !== "auto") return validated;
|
|
13
|
+
if (values.length === 0) {
|
|
14
|
+
throw new Error("Cannot infer an automatic scale domain from no values.");
|
|
15
|
+
}
|
|
16
|
+
let minimum = values[0];
|
|
17
|
+
let maximum = values[0];
|
|
18
|
+
for (const value of values.slice(1)) {
|
|
19
|
+
minimum = Math.min(minimum, value);
|
|
20
|
+
maximum = Math.max(maximum, value);
|
|
21
|
+
}
|
|
22
|
+
return cloneAndFreeze([minimum, maximum]);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function niceLinearStep(span, count = 5) {
|
|
26
|
+
const rough = span / Math.max(1, count);
|
|
27
|
+
const power = 10 ** Math.floor(Math.log10(rough));
|
|
28
|
+
const fraction = rough / power;
|
|
29
|
+
const factor = fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 3 ? 3
|
|
30
|
+
: fraction <= 5 ? 5 : 10;
|
|
31
|
+
return factor * power;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function niceLinearDomain(domain) {
|
|
35
|
+
const [minimum, maximum] = domain;
|
|
36
|
+
const tolerance = Number.EPSILON * 16 * Math.max(
|
|
37
|
+
1, Math.abs(minimum), Math.abs(maximum)
|
|
38
|
+
);
|
|
39
|
+
if (maximum - minimum <= tolerance) {
|
|
40
|
+
const midpoint = (minimum + maximum) / 2;
|
|
41
|
+
return cloneAndFreeze([midpoint, midpoint]);
|
|
42
|
+
}
|
|
43
|
+
const step = niceLinearStep(maximum - minimum);
|
|
44
|
+
return cloneAndFreeze([
|
|
45
|
+
Number((Math.floor(minimum / step) * step).toPrecision(12)),
|
|
46
|
+
Number((Math.ceil(maximum / step) * step).toPrecision(12))
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function resolveContinuousDomain({ domain, values, type, nice, zero }) {
|
|
51
|
+
const explicit = domain !== "auto";
|
|
52
|
+
let resolved = resolveScaleDomain(domain, values);
|
|
53
|
+
if (explicit) return resolved;
|
|
54
|
+
if (zero === true) {
|
|
55
|
+
resolved = cloneAndFreeze([
|
|
56
|
+
Math.min(0, resolved[0]),
|
|
57
|
+
Math.max(0, resolved[1])
|
|
58
|
+
]);
|
|
59
|
+
}
|
|
60
|
+
if (nice === true) {
|
|
61
|
+
resolved = type === "time"
|
|
62
|
+
? niceTimeDomain(resolved)
|
|
63
|
+
: niceLinearDomain(resolved);
|
|
64
|
+
}
|
|
65
|
+
return resolved;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function resolveScaleRange(range, channel, bounds) {
|
|
69
|
+
const validated = validateScaleRange(range);
|
|
70
|
+
validatePositionChannel(channel);
|
|
71
|
+
if (validated !== "auto") return validated;
|
|
72
|
+
if (
|
|
73
|
+
!isPlainObject(bounds) ||
|
|
74
|
+
![bounds.x, bounds.y, bounds.width, bounds.height].every(Number.isFinite)
|
|
75
|
+
) {
|
|
76
|
+
throw new Error("Automatic position range requires graphical bounds.");
|
|
77
|
+
}
|
|
78
|
+
return channel === "x"
|
|
79
|
+
? cloneAndFreeze([bounds.x, bounds.x + bounds.width])
|
|
80
|
+
: cloneAndFreeze([bounds.y + bounds.height, bounds.y]);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function mapLinearValues(values, domain, range, options = {}) {
|
|
84
|
+
const { clamp = false } = options;
|
|
85
|
+
const hasUnknown = Object.hasOwn(options, "unknown");
|
|
86
|
+
const [domainStart, domainEnd] = validatePair(domain, "Resolved domain");
|
|
87
|
+
const [rangeStart, rangeEnd] = validatePair(range, "Resolved range");
|
|
88
|
+
if (typeof clamp !== "boolean") {
|
|
89
|
+
throw new TypeError("Linear scale clamp must be a boolean.");
|
|
90
|
+
}
|
|
91
|
+
if (!hasUnknown && !values.every(Number.isFinite)) {
|
|
92
|
+
throw new TypeError("Linear scale values must be finite numbers.");
|
|
93
|
+
}
|
|
94
|
+
if (domainStart === domainEnd) {
|
|
95
|
+
const midpoint = (rangeStart + rangeEnd) / 2;
|
|
96
|
+
return cloneAndFreeze(values.map(value =>
|
|
97
|
+
Number.isFinite(value) ? midpoint : options.unknown
|
|
98
|
+
));
|
|
99
|
+
}
|
|
100
|
+
const domainSpan = domainEnd - domainStart;
|
|
101
|
+
const rangeSpan = rangeEnd - rangeStart;
|
|
102
|
+
return cloneAndFreeze(values.map(value => {
|
|
103
|
+
if (!Number.isFinite(value)) {
|
|
104
|
+
if (hasUnknown) return options.unknown;
|
|
105
|
+
throw new TypeError("Linear scale values must be finite numbers.");
|
|
106
|
+
}
|
|
107
|
+
const proportion = (value - domainStart) / domainSpan;
|
|
108
|
+
const resolved = clamp ? Math.max(0, Math.min(1, proportion)) : proportion;
|
|
109
|
+
return rangeStart + resolved * rangeSpan;
|
|
110
|
+
}));
|
|
111
|
+
}
|