Skip to content

Commit

Permalink
feat(generator): implement fix(?) for issue TimMikeladze#4
Browse files Browse the repository at this point in the history
If both `count` and `colors` parameters are passed, but length of
`colors` is less than `count`, randomly generate extra colors to satisfy
the desired color count.
  • Loading branch information
jalavosus committed Apr 26, 2022
1 parent 1ee753e commit ae7d0ff
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/util/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ export const generator = async (
const key = options.key || options.k;
const id = options.id ? options.id : customId();

const customCount = options.count || options.c;
const customColors = options.colors || options.cs;

if (width > MAX_DIMENSION || height > MAX_DIMENSION) {
throw new Error(`Dimensions must be no greater than ${MAX_DIMENSION}`);
}
Expand Down Expand Up @@ -168,18 +171,35 @@ export const generator = async (
}
}

const [svg, canvas] = patterns[options.type || options.t || `simple`]({
...allOptions,
colors:
(options.colors || options.cs)?.split(`-`)?.map((x) => `#${x}`) ||
randomColor({
count,
let colors: string[] =
(options.colors || options.cs)?.split(`-`)?.map((x) => `#${x}`) ||
randomColor({
count,
hue,
luminosity,
seed,
format,
alpha,
});

if (customColors && customCount && count > colors.length) {
const countDiff = count - colors.length;
colors = [
...colors,
...randomColor({
count: countDiff,
hue,
luminosity,
seed,
format,
alpha,
}),
];
}

const [svg, canvas] = patterns[options.type || options.t || `simple`]({
...allOptions,
colors,
});

await redis.incr(`numberOfImagesCreated`);
Expand Down

0 comments on commit ae7d0ff

Please sign in to comment.