Skip to content

Commit a34851e

Browse files
committed
Allow 'null' palette values to use defaults
1 parent a11e041 commit a34851e

1 file changed

Lines changed: 32 additions & 20 deletions

File tree

src/renderers/_base.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export interface IRendererOptionsIn {
5757
* Defaults to a set of nine colours, defined in the constructor.
5858
*
5959
*/
60-
colours?: string[];
60+
colours?: (string|null)[];
6161
/**
6262
* I want to know whether the custom colours being passed are a global preference or
6363
* a game-specific one. The idea being that the colourFunc `custom` should prefer
@@ -129,6 +129,7 @@ export interface IRendererOptionsOut {
129129
contextGlobal: boolean;
130130
colours: string[];
131131
coloursGlobal: boolean;
132+
useDefaults: number[];
132133
patterns: boolean;
133134
patternList: string[];
134135
colourBlind: boolean;
@@ -140,6 +141,9 @@ export interface IRendererOptionsOut {
140141
boardHover?: (row: number, col: number, piece: string) => void;
141142
}
142143

144+
export const paletteDefault = ["#e31a1c", "#1f78b4", "#33a02c", "#ffff99", "#6a3d9a", "#ff7f00", "#b15928", "#fb9a99", "#a6cee3", "#b2df8a", "#fdbf6f", "#cab2d6"];
145+
export const paletteBlind = ["#9f0162", "#8400cd", "#a40122", "#009f81", "#008df9", "#e20134", "#ff5aaf", "#00c2f9", "#ff6e3a", "#00fccf", "#ffb2fd", "#ffc33b"];
146+
143147
export interface IMarkBoardOptions {
144148
svgGroup: SVGG;
145149
preGridLines: boolean;
@@ -241,6 +245,7 @@ export abstract class RendererBase {
241245
colourBlind: false,
242246
colours: [],
243247
coloursGlobal: true,
248+
useDefaults: [],
244249
patterns: false,
245250
patternList: ["microbial", "chevrons", "honeycomb", "triangles", "wavy", "slant", "dots", "starsWhite", "cross", "houndstooth"],
246251
showAnnotations: true,
@@ -292,9 +297,9 @@ export abstract class RendererBase {
292297
this.options.colourBlind = false;
293298
}
294299
if (this.options.colourBlind) {
295-
this.options.colours = ["#9f0162", "#8400cd", "#a40122", "#009f81", "#008df9", "#e20134", "#ff5aaf", "#00c2f9", "#ff6e3a", "#00fccf", "#ffb2fd", "#ffc33b"];
300+
this.options.colours = [...paletteBlind];
296301
} else {
297-
this.options.colours = ["#e31a1c", "#1f78b4", "#33a02c", "#ffff99", "#6a3d9a", "#ff7f00", "#b15928", "#fb9a99", "#a6cee3", "#b2df8a", "#fdbf6f", "#cab2d6"];
302+
this.options.colours = [...paletteDefault];
298303
}
299304

300305
// Validate sheet list
@@ -339,14 +344,21 @@ export abstract class RendererBase {
339344
}
340345

341346
// Validate colour list if given
347+
this.options.useDefaults = [];
342348
if ( (opts.colours !== undefined) && (opts.colours.length > 0) ) {
343349
const normalized: string[] = [];
344-
for (const c of opts.colours) {
345-
const color = tinycolor(c);
346-
if (! color.isValid()) {
347-
throw new Error(`One of the colours you requested is malformed: ${ c }`);
350+
for (let i = 0; i < opts.colours.length; i++) {
351+
const c = opts.colours[i];
352+
if (c === null) {
353+
this.options.useDefaults.push(i+1);
354+
normalized.push(paletteDefault[i]);
355+
} else {
356+
const color = tinycolor(c);
357+
if (! color.isValid()) {
358+
throw new Error(`One of the colours you requested is malformed: ${ c }`);
359+
}
360+
normalized.push(color.toHexString());
348361
}
349-
normalized.push(color.toHexString());
350362
}
351363
this.options.colours = [...normalized];
352364
this.options.coloursGlobal = opts.coloursGlobal ?? true;
@@ -3992,36 +4004,36 @@ export abstract class RendererBase {
39924004
// if `palette` is a number
39934005
if (typeof val.palette === "number") {
39944006
// only choose if passed colours are game-specific customizations
3995-
if (!this.options.coloursGlobal) {
3996-
colour = this.resolveColour(val.palette) as string;
3997-
} else {
4007+
if (this.options.coloursGlobal || this.options.useDefaults.includes(val.palette)) {
39984008
colour = this.resolveColour(val.default) as string;
4009+
} else {
4010+
colour = this.resolveColour(val.palette) as string;
39994011
}
40004012
}
40014013
// if `palette` is a context
40024014
else if (typeof val.palette === "string" && val.palette.startsWith("_context_")) {
40034015
// only choose if passed context is game-specific customization
4004-
if (!this.options.contextGlobal) {
4005-
colour = this.resolveColour(val.palette) as string;
4006-
} else {
4016+
if (this.options.contextGlobal) {
40074017
colour = this.resolveColour(val.default) as string;
4018+
} else {
4019+
colour = this.resolveColour(val.palette) as string;
40084020
}
40094021
}
40104022
// otherwise, rely on `paletteType`
40114023
else if ("paletteType" in val && val.paletteType !== undefined) {
40124024
if (val.paletteType === "context") {
40134025
// only choose if passed context is game-specific customization
4014-
if (!this.options.contextGlobal) {
4015-
colour = this.resolveColour(val.palette) as string;
4016-
} else {
4026+
if (this.options.contextGlobal) {
40174027
colour = this.resolveColour(val.default) as string;
4028+
} else {
4029+
colour = this.resolveColour(val.palette) as string;
40184030
}
40194031
} else {
40204032
// only choose if passed colours are game-specific customizations
4021-
if (!this.options.coloursGlobal) {
4022-
colour = this.resolveColour(val.palette) as string;
4023-
} else {
4033+
if (this.options.coloursGlobal) {
40244034
colour = this.resolveColour(val.default) as string;
4035+
} else {
4036+
colour = this.resolveColour(val.palette) as string;
40254037
}
40264038
}
40274039
}

0 commit comments

Comments
 (0)