colorizr 3.0.2 → 3.0.4
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/README.md +40 -21
- package/dist/index.d.mts +79 -14
- package/dist/index.d.ts +79 -14
- package/dist/index.js +76 -54
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +73 -51
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
- package/src/colorizr.ts +5 -3
- package/src/format-css.ts +7 -3
- package/src/index.ts +7 -0
- package/src/modules/invariant.ts +0 -1
- package/src/palette.ts +34 -18
- package/src/scheme.ts +5 -0
- package/src/swatch.ts +93 -36
- package/src/text-color.ts +15 -5
- package/src/types/index.ts +4 -1
package/dist/index.mjs
CHANGED
|
@@ -1163,9 +1163,15 @@ function textColor(input, options = {}) {
|
|
|
1163
1163
|
const { darkColor = "#000000", lightColor = "#ffffff", threshold = 128 } = options;
|
|
1164
1164
|
invariant(isString(input), MESSAGES.inputString);
|
|
1165
1165
|
invariant(threshold >= 0 && threshold <= 255, MESSAGES.threshold);
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1166
|
+
try {
|
|
1167
|
+
const { r, g, b } = hex2rgb(parseCSS(input, "hex"));
|
|
1168
|
+
const yiq = (r * 299 + g * 587 + b * 114) / 1e3;
|
|
1169
|
+
return yiq >= threshold ? darkColor : lightColor;
|
|
1170
|
+
} catch (error) {
|
|
1171
|
+
console.warn(`Invalid color input: ${input}`);
|
|
1172
|
+
console.warn(error);
|
|
1173
|
+
return darkColor;
|
|
1174
|
+
}
|
|
1169
1175
|
}
|
|
1170
1176
|
|
|
1171
1177
|
// src/transparentize.ts
|
|
@@ -1366,24 +1372,19 @@ function palette(input, options = {}) {
|
|
|
1366
1372
|
invariant(isPlainObject(options), MESSAGES.options);
|
|
1367
1373
|
const { format, lightness, saturation, size = 6, type } = options;
|
|
1368
1374
|
const hsl = parseCSS(input, "hsl");
|
|
1369
|
-
const output = [];
|
|
1370
1375
|
const colorFormat = isHex(input) || isNamedColor(input) ? "hex" : extractColorParts(input).model;
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
}
|
|
1377
|
-
break;
|
|
1376
|
+
const output = [];
|
|
1377
|
+
if (type === "monochromatic") {
|
|
1378
|
+
const step = 80 / size;
|
|
1379
|
+
for (let index = size; index > 0; index--) {
|
|
1380
|
+
output.push(hsl2hex({ ...hsl, l: step * index }));
|
|
1378
1381
|
}
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
}
|
|
1386
|
-
break;
|
|
1382
|
+
} else {
|
|
1383
|
+
const step = 360 / size;
|
|
1384
|
+
output.push(hsl2hex({ ...hsl, l: lightness ?? hsl.l, s: saturation ?? hsl.s }));
|
|
1385
|
+
for (let index = 1; index < size; index++) {
|
|
1386
|
+
const color = rotate(input, hsl.h + step * index, "hex");
|
|
1387
|
+
output.push(hsl2hex({ ...hex2hsl(color), l: lightness ?? hsl.l, s: saturation ?? hsl.s }));
|
|
1387
1388
|
}
|
|
1388
1389
|
}
|
|
1389
1390
|
return output.map((color) => convert(color, format ?? colorFormat));
|
|
@@ -1490,51 +1491,72 @@ function scheme(input, typeOrOptions) {
|
|
|
1490
1491
|
}
|
|
1491
1492
|
|
|
1492
1493
|
// src/swatch.ts
|
|
1493
|
-
var MIN_LIGHTNESS =
|
|
1494
|
-
var MAX_LIGHTNESS =
|
|
1495
|
-
function shadeColorDynamic(input, lightnessTuningFactor,
|
|
1494
|
+
var MIN_LIGHTNESS = 5;
|
|
1495
|
+
var MAX_LIGHTNESS = 95;
|
|
1496
|
+
function shadeColorDynamic(input, lightnessTuningFactor, chromaFactor = 0) {
|
|
1496
1497
|
if (lightnessTuningFactor === 0) {
|
|
1497
|
-
|
|
1498
|
+
const { l } = input;
|
|
1499
|
+
return rgb2hex(oklch2rgb({ ...input, l: l / 100 }));
|
|
1498
1500
|
}
|
|
1499
|
-
return shadeColor(input, input.l + lightnessTuningFactor,
|
|
1501
|
+
return shadeColor(input, input.l + lightnessTuningFactor, chromaFactor);
|
|
1500
1502
|
}
|
|
1501
|
-
function shadeColor(input, lightness,
|
|
1503
|
+
function shadeColor(input, lightness, chromaFactor = 0) {
|
|
1502
1504
|
const { c, h } = input;
|
|
1503
|
-
|
|
1505
|
+
const adjustedChroma = clamp(c + chromaFactor, 0, 0.4);
|
|
1506
|
+
return oklch2hex({ l: lightness / 100, c: adjustedChroma, h });
|
|
1504
1507
|
}
|
|
1505
1508
|
function swatch(input, options = {}) {
|
|
1506
1509
|
invariant(isString(input), MESSAGES.inputString);
|
|
1507
|
-
const { format, scale = "dynamic" } = options;
|
|
1510
|
+
const { format, monochromatic = false, scale = "dynamic", mode, variant = "base" } = options;
|
|
1508
1511
|
const lch = parseCSS(input, "oklch");
|
|
1512
|
+
const chromaScale = {
|
|
1513
|
+
base: 1,
|
|
1514
|
+
deep: 0.8,
|
|
1515
|
+
neutral: 0.5,
|
|
1516
|
+
pastel: 0.3,
|
|
1517
|
+
subtle: 0.2,
|
|
1518
|
+
vibrant: 1.25
|
|
1519
|
+
}[variant];
|
|
1509
1520
|
lch.l = 50;
|
|
1521
|
+
lch.c *= chromaScale;
|
|
1522
|
+
if (variant === "deep") {
|
|
1523
|
+
lch.l *= 0.7;
|
|
1524
|
+
}
|
|
1510
1525
|
const colorFormat = isHex(input) || isNamedColor(input) ? "hex" : extractColorParts(input).model;
|
|
1511
1526
|
const currentLightness = lch.l;
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1527
|
+
let lightSteps = 5;
|
|
1528
|
+
let darkSteps = 8;
|
|
1529
|
+
if (mode) {
|
|
1530
|
+
lightSteps *= mode === "light" ? 0.75 : 1.25;
|
|
1531
|
+
darkSteps *= mode === "light" ? 1.25 : 0.75;
|
|
1532
|
+
}
|
|
1533
|
+
const lightStepSize = (MAX_LIGHTNESS - currentLightness) / lightSteps;
|
|
1534
|
+
const darkStepSize = -1 * (currentLightness - MIN_LIGHTNESS) / darkSteps;
|
|
1535
|
+
const getChromaFactor = (value) => {
|
|
1536
|
+
return monochromatic ? 0 : value;
|
|
1537
|
+
};
|
|
1516
1538
|
const output = scale === "linear" ? {
|
|
1517
|
-
50: shadeColor(lch, 95, -375e-5),
|
|
1518
|
-
100: shadeColor(lch, 90, -375e-5),
|
|
1519
|
-
200: shadeColor(lch, 80, -375e-5),
|
|
1520
|
-
300: shadeColor(lch, 70, -375e-5),
|
|
1521
|
-
400: shadeColor(lch, 60, -375e-5),
|
|
1539
|
+
50: shadeColor(lch, 95, getChromaFactor(-375e-5)),
|
|
1540
|
+
100: shadeColor(lch, 90, getChromaFactor(-375e-5)),
|
|
1541
|
+
200: shadeColor(lch, 80, getChromaFactor(-375e-5)),
|
|
1542
|
+
300: shadeColor(lch, 70, getChromaFactor(-375e-5)),
|
|
1543
|
+
400: shadeColor(lch, 60, getChromaFactor(-375e-5)),
|
|
1522
1544
|
500: shadeColor(lch, 50),
|
|
1523
|
-
600: shadeColor(lch, 40, 0.025),
|
|
1524
|
-
700: shadeColor(lch, 30, 0.05),
|
|
1525
|
-
800: shadeColor(lch, 20, 0.075),
|
|
1526
|
-
900: shadeColor(lch, 10, 0.1)
|
|
1545
|
+
600: shadeColor(lch, 40, getChromaFactor(0.025)),
|
|
1546
|
+
700: shadeColor(lch, 30, getChromaFactor(0.05)),
|
|
1547
|
+
800: shadeColor(lch, 20, getChromaFactor(0.075)),
|
|
1548
|
+
900: shadeColor(lch, 10, getChromaFactor(0.1))
|
|
1527
1549
|
} : {
|
|
1528
|
-
50: shadeColorDynamic(lch, 5 *
|
|
1529
|
-
100: shadeColorDynamic(lch, 4 *
|
|
1530
|
-
200: shadeColorDynamic(lch, 3 *
|
|
1531
|
-
300: shadeColorDynamic(lch, 2 *
|
|
1532
|
-
400: shadeColorDynamic(lch,
|
|
1550
|
+
50: shadeColorDynamic(lch, 5 * lightStepSize, getChromaFactor(-375e-5)),
|
|
1551
|
+
100: shadeColorDynamic(lch, 4 * lightStepSize, getChromaFactor(-375e-5)),
|
|
1552
|
+
200: shadeColorDynamic(lch, 3 * lightStepSize, getChromaFactor(-375e-5)),
|
|
1553
|
+
300: shadeColorDynamic(lch, 2 * lightStepSize, getChromaFactor(-375e-5)),
|
|
1554
|
+
400: shadeColorDynamic(lch, lightStepSize, getChromaFactor(-375e-5)),
|
|
1533
1555
|
500: shadeColorDynamic(lch, 0),
|
|
1534
|
-
600: shadeColorDynamic(lch, 1.6 *
|
|
1535
|
-
700: shadeColorDynamic(lch,
|
|
1536
|
-
800: shadeColorDynamic(lch,
|
|
1537
|
-
900: shadeColorDynamic(lch, 4 *
|
|
1556
|
+
600: shadeColorDynamic(lch, 1.6 * darkStepSize, getChromaFactor(0.025)),
|
|
1557
|
+
700: shadeColorDynamic(lch, 3.2 * darkStepSize, getChromaFactor(0.05)),
|
|
1558
|
+
800: shadeColorDynamic(lch, 4.8 * darkStepSize, getChromaFactor(0.075)),
|
|
1559
|
+
900: shadeColorDynamic(lch, 6.4 * darkStepSize, getChromaFactor(0.1))
|
|
1538
1560
|
};
|
|
1539
1561
|
return Object.entries(output).reduce((acc, [key, value]) => {
|
|
1540
1562
|
return {
|
|
@@ -1545,7 +1567,7 @@ function swatch(input, options = {}) {
|
|
|
1545
1567
|
}
|
|
1546
1568
|
|
|
1547
1569
|
// src/index.ts
|
|
1548
|
-
var
|
|
1570
|
+
var index_default = Colorizr;
|
|
1549
1571
|
export {
|
|
1550
1572
|
addAlphaToHex,
|
|
1551
1573
|
brightnessDifference,
|
|
@@ -1556,7 +1578,7 @@ export {
|
|
|
1556
1578
|
convert,
|
|
1557
1579
|
convertAlphaToHex,
|
|
1558
1580
|
darken,
|
|
1559
|
-
|
|
1581
|
+
index_default as default,
|
|
1560
1582
|
desaturate,
|
|
1561
1583
|
extractAlphaFromHex,
|
|
1562
1584
|
extractColorParts,
|