Skip to content

Commit d200b32

Browse files
committed
Add bestContrast function
1 parent 5dc1ce3 commit d200b32

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/renderers/_base.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9386,6 +9386,12 @@ export abstract class RendererBase {
93869386
const base = hex2rgb(this.resolveColour(val.colour) as string);
93879387
colour = rgb2hex(lighten(base, val.ds, val.dl));
93889388
}
9389+
// bestContrast
9390+
else if (val.func === "bestContrast") {
9391+
const bg = this.resolveColour(val.bg) as string;
9392+
const fg = val.fg.map(c => this.resolveColour(c) as string);
9393+
return tinycolor.mostReadable(bg, fg).toHexString();
9394+
}
93899395
}
93909396
} else if (typeof val === "number") {
93919397
colour = this.options.colours[val - 1];

src/schemas/schema.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type Colourstrings = string;
1212
/**
1313
* Colours can also be derived using various functions.
1414
*/
15-
export type Colourfuncs = FunctionFlatten | FunctionLighten;
15+
export type Colourfuncs = FunctionFlatten | FunctionBestContrast | FunctionLighten;
1616
export type PositiveInteger = number;
1717
/**
1818
* Schema for the `matrix` part of a polyomino-related feature
@@ -284,6 +284,14 @@ export interface FunctionFlatten {
284284
bg: PositiveInteger | Colourstrings;
285285
opacity: number;
286286
}
287+
/**
288+
* Chooses the foreground colour with the best contrast against the given background.
289+
*/
290+
export interface FunctionBestContrast {
291+
func: "bestContrast";
292+
bg: PositiveInteger | Colourstrings;
293+
fg: (PositiveInteger | Colourstrings)[];
294+
}
287295
/**
288296
* Lightens or darkens a colour by the specified amount of saturation and luminance. Positive deltas lighten, negative darken.
289297
*/

src/schemas/schema.json

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,37 @@
8686
"required": ["func", "colour", "ds", "dl"],
8787
"additionalProperties": false
8888
},
89-
"colourfuncs": {
89+
"functionBestContrast": {
90+
"description": "Chooses the foreground colour with the best contrast against the given background.",
91+
"type": "object",
92+
"properties": {
93+
"func": {
94+
"enum": ["bestContrast"]
95+
},
96+
"bg": {
97+
"anyOf": [
98+
{"$ref": "#/$defs/positiveInteger"},
99+
{"$ref": "#/$defs/colourstrings"}
100+
]
101+
},
102+
"fg": {
103+
"type": "array",
104+
"items": {
105+
"anyOf": [
106+
{"$ref": "#/$defs/positiveInteger"},
107+
{"$ref": "#/$defs/colourstrings"}
108+
]
109+
}
110+
}
111+
},
112+
"required": ["func", "bg", "fg"],
113+
"additionalProperties": false
114+
},
115+
"colourfuncs": {
90116
"description": "Colours can also be derived using various functions.",
91117
"anyOf": [
92118
{"$ref": "#/$defs/functionFlatten"},
119+
{"$ref": "#/$defs/functionBestContrast"},
93120
{"$ref": "#/$defs/functionLighten"}
94121
]
95122
},

0 commit comments

Comments
 (0)