Skip to content

Commit 1d7b1ab

Browse files
committed
cli_tool: Add CLI options to configure lightness range.
Earlier, the CLI options offered doesn't include option to configure lightness range of colored and grayscale shapes. The npm package itself has options to configure these, but was not exposed via CLI tool. This commit makes changes to add the following two options: * --lightness-color * --lightness-grayscale Signed-off-by: Prakhar Pratyush <prakhar1144@gmail.com>
1 parent c894f54 commit 1d7b1ab

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

bin/jdenticon.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ function writeHelp() {
5050
console.log(" -f, --format <svg|png> Format of generated icon. Otherwise detected from output path. (default: png)");
5151
console.log(" -b, --back-color <value> Background color on format #rgb, #rgba, #rrggbb or #rrggbbaa. (default: transparent)");
5252
console.log(" -p, --padding <value> Padding in percent in range 0 to 0.5. (default: 0.08)");
53+
console.log(" --lightness-color <min,max> Lightness range of colored shapes in [0,1]. (default: 0.4,0.8)");
54+
console.log(" --lightness-grayscale <min,max> Lightness range of grayscale shapes in [0,1]. (default: 0.3,0.9)");
5355
console.log(" -v, --version Gets the version of Jdenticon.");
5456
console.log(" -h, --help Show this help information.");
5557
console.log("");
@@ -117,10 +119,33 @@ function parseArgs(args) {
117119
format: consume(["-f", "--format"], true),
118120
padding: consume(["-p", "--padding"], true),
119121
backColor: consume(["-b", "--back-color"], true),
122+
lightnessColor: consume(["--lightness-color"], true),
123+
lightnessGrayscale: consume(["--lightness-grayscale"], true),
120124
value: args
121125
};
122126
}
123127

128+
function parseLightnessRange(value) {
129+
var parts = value.split(",");
130+
if (parts.length !== 2) {
131+
return;
132+
}
133+
134+
var min = Number(parts[0]);
135+
var max = Number(parts[1]);
136+
137+
if (
138+
isNaN(min) || isNaN(max) ||
139+
min < 0 || min > 1 ||
140+
max < 0 || max > 1 ||
141+
min > max
142+
) {
143+
return;
144+
}
145+
146+
return [min, max];
147+
}
148+
124149
function validateArgs(args) {
125150
if (args.value.length) {
126151

@@ -153,7 +178,34 @@ function validateArgs(args) {
153178
console.warn("WARN Invalid background color specified. Defaults to transparent.");
154179
}
155180
}
156-
181+
182+
// Lightness
183+
var lightnessColor;
184+
if (args.lightnessColor != null) {
185+
lightnessColor = parseLightnessRange(args.lightnessColor);
186+
if (!lightnessColor) {
187+
lightnessColor = [0.4, 0.8];
188+
console.warn("WARN Invalid lightness range of colored shapes specified. Defaults to 0.4,0.8.");
189+
}
190+
}
191+
192+
var lightnessGrayscale;
193+
if (args.lightnessGrayscale != null) {
194+
lightnessGrayscale = parseLightnessRange(args.lightnessGrayscale);
195+
if (!lightnessGrayscale) {
196+
lightnessGrayscale = [0.3, 0.9];
197+
console.warn("WARN Invalid lightness range of grayscale shapes specified. Defaults to 0.3,0.9.");
198+
}
199+
}
200+
201+
var lightness;
202+
if (lightnessColor || lightnessGrayscale) {
203+
lightness = {
204+
color: lightnessColor,
205+
grayscale: lightnessGrayscale
206+
};
207+
}
208+
157209
// Format
158210
var generateSvg =
159211
args.format ? /^svg$/i.test(args.format) :
@@ -166,7 +218,8 @@ function validateArgs(args) {
166218
return {
167219
config: {
168220
padding: padding,
169-
backColor: backColor
221+
backColor: backColor,
222+
lightness: lightness
170223
},
171224
output: args.output,
172225
size: size,

0 commit comments

Comments
 (0)