@knapsack/color-utils 4.69.15--canary.4433.ca0920f.0 → 4.69.15--canary.4771.9877e61.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.js +24 -197
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6 -154
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
@@ -1,199 +1,26 @@
|
|
1
|
-
|
2
|
-
var __defProp = Object.defineProperty;
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
7
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
8
|
-
var __export = (target, all) => {
|
9
|
-
for (var name in all)
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
-
};
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
-
for (let key of __getOwnPropNames(from))
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
-
}
|
18
|
-
return to;
|
19
|
-
};
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
-
mod
|
27
|
-
));
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
1
|
+
'use strict';
|
29
2
|
|
30
|
-
|
31
|
-
var
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
function roundObjectValuesNumber(obj) {
|
54
|
-
return Object.entries(obj).reduce((cur, [key, value]) => {
|
55
|
-
const k = key;
|
56
|
-
cur[k] = roundNumber(value);
|
57
|
-
return cur;
|
58
|
-
}, {});
|
59
|
-
}
|
60
|
-
__name(roundObjectValuesNumber, "roundObjectValuesNumber");
|
61
|
-
function colorAlphaToHex(value) {
|
62
|
-
let val = Math.max(0, value);
|
63
|
-
val = Math.min(val, 255);
|
64
|
-
val = Math.ceil(val);
|
65
|
-
return "0123456789ABCDEF".charAt((val - val % 16) / 16) + "0123456789ABCDEF".charAt(val % 16);
|
66
|
-
}
|
67
|
-
__name(colorAlphaToHex, "colorAlphaToHex");
|
68
|
-
function getColorFormat(color) {
|
69
|
-
if (color.startsWith("#")) return "hex";
|
70
|
-
if (color.startsWith("rgb")) return "rgb";
|
71
|
-
if (color.startsWith("hsl")) return "hsl";
|
72
|
-
return null;
|
73
|
-
}
|
74
|
-
__name(getColorFormat, "getColorFormat");
|
75
|
-
function analyzeColorBase(color) {
|
76
|
-
try {
|
77
|
-
const c = (0, import_color.default)(color);
|
78
|
-
const rgb = c.rgb();
|
79
|
-
const rgbInfo = roundObjectValuesNumber(
|
80
|
-
rgb.object()
|
81
|
-
);
|
82
|
-
const isTransparent = typeof rgbInfo.alpha === "number" && rgbInfo.alpha !== 1;
|
83
|
-
const rgbString = isTransparent ? `rgba(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b}, ${rgbInfo.alpha})` : `rgb(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b})`;
|
84
|
-
const hsl = c.hsl();
|
85
|
-
const hslInfo = roundObjectValuesNumber(
|
86
|
-
hsl.object()
|
87
|
-
);
|
88
|
-
const hslString = isTransparent ? `hsla(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%, ${hslInfo.alpha})` : `hsl(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%)`;
|
89
|
-
let hex = c.hex();
|
90
|
-
if (isTransparent) {
|
91
|
-
hex = `${hex}${colorAlphaToHex(rgbInfo.alpha * 256)}`;
|
92
|
-
}
|
93
|
-
return {
|
94
|
-
original: color,
|
95
|
-
format: getColorFormat(color),
|
96
|
-
rgbInfo,
|
97
|
-
rgbString,
|
98
|
-
hslInfo,
|
99
|
-
hslString,
|
100
|
-
hex,
|
101
|
-
isTransparent,
|
102
|
-
luminosity: c.luminosity(),
|
103
|
-
isDark: c.isDark(),
|
104
|
-
isLight: c.isLight(),
|
105
|
-
color: c
|
106
|
-
};
|
107
|
-
} catch (e) {
|
108
|
-
throw new Error(`Could not analyze the color "${color}" - ${e.message}`);
|
109
|
-
}
|
110
|
-
}
|
111
|
-
__name(analyzeColorBase, "analyzeColorBase");
|
112
|
-
var analyzeColor = (0, import_memoize.default)(analyzeColorBase);
|
113
|
-
function isColor(color) {
|
114
|
-
try {
|
115
|
-
analyzeColor(color);
|
116
|
-
return true;
|
117
|
-
} catch (e) {
|
118
|
-
return false;
|
119
|
-
}
|
120
|
-
}
|
121
|
-
__name(isColor, "isColor");
|
122
|
-
function assertColor(color) {
|
123
|
-
analyzeColor(color);
|
124
|
-
}
|
125
|
-
__name(assertColor, "assertColor");
|
126
|
-
function getColorRgbInfo(color) {
|
127
|
-
return analyzeColor(color).rgbInfo;
|
128
|
-
}
|
129
|
-
__name(getColorRgbInfo, "getColorRgbInfo");
|
130
|
-
function getColorHslInfo(color) {
|
131
|
-
return analyzeColor(color).hslInfo;
|
132
|
-
}
|
133
|
-
__name(getColorHslInfo, "getColorHslInfo");
|
134
|
-
function hasOpacity(color) {
|
135
|
-
return !analyzeColor(color).isTransparent;
|
136
|
-
}
|
137
|
-
__name(hasOpacity, "hasOpacity");
|
138
|
-
function getColorLuminosity(color) {
|
139
|
-
return analyzeColor(color).luminosity;
|
140
|
-
}
|
141
|
-
__name(getColorLuminosity, "getColorLuminosity");
|
142
|
-
function isDarkColor(color) {
|
143
|
-
return analyzeColor(color).isDark;
|
144
|
-
}
|
145
|
-
__name(isDarkColor, "isDarkColor");
|
146
|
-
function isLightColor(color) {
|
147
|
-
return analyzeColor(color).isLight;
|
148
|
-
}
|
149
|
-
__name(isLightColor, "isLightColor");
|
150
|
-
function convertColor({
|
151
|
-
color,
|
152
|
-
format
|
153
|
-
}) {
|
154
|
-
assertColor(color);
|
155
|
-
switch (format) {
|
156
|
-
case "rgb": {
|
157
|
-
const rgb = getColorRgbInfo(color);
|
158
|
-
return (0, import_color.default)(rgb).string();
|
159
|
-
}
|
160
|
-
case "hsl": {
|
161
|
-
const hsl = getColorHslInfo(color);
|
162
|
-
return (0, import_color.default)(hsl).string();
|
163
|
-
}
|
164
|
-
case "hex":
|
165
|
-
default:
|
166
|
-
return (0, import_color.default)(color).hex();
|
167
|
-
}
|
168
|
-
}
|
169
|
-
__name(convertColor, "convertColor");
|
170
|
-
function convertTransparentColorToHex(color) {
|
171
|
-
return analyzeColor(color).hex;
|
172
|
-
}
|
173
|
-
__name(convertTransparentColorToHex, "convertTransparentColorToHex");
|
174
|
-
function isColorContrastEnough({
|
175
|
-
foregroundColor,
|
176
|
-
bgColor
|
177
|
-
}) {
|
178
|
-
const f = analyzeColor(foregroundColor);
|
179
|
-
const b = analyzeColor(bgColor);
|
180
|
-
const contrast = f.color.contrast(b.color);
|
181
|
-
return contrast >= 4.5;
|
182
|
-
}
|
183
|
-
__name(isColorContrastEnough, "isColorContrastEnough");
|
184
|
-
// Annotate the CommonJS export names for ESM import in node:
|
185
|
-
0 && (module.exports = {
|
186
|
-
analyzeColor,
|
187
|
-
assertColor,
|
188
|
-
convertColor,
|
189
|
-
convertTransparentColorToHex,
|
190
|
-
getColorHslInfo,
|
191
|
-
getColorLuminosity,
|
192
|
-
getColorRgbInfo,
|
193
|
-
hasOpacity,
|
194
|
-
isColor,
|
195
|
-
isColorContrastEnough,
|
196
|
-
isDarkColor,
|
197
|
-
isLightColor
|
198
|
-
});
|
3
|
+
var l = require('color');
|
4
|
+
var m = require('lodash/memoize.js');
|
5
|
+
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
7
|
+
|
8
|
+
var l__default = /*#__PURE__*/_interopDefault(l);
|
9
|
+
var m__default = /*#__PURE__*/_interopDefault(m);
|
10
|
+
|
11
|
+
var f=Object.defineProperty;var n=(r,t)=>f(r,"name",{value:t,configurable:!0});function p(r){return r<1?Number(r.toFixed(2)):Math.round(r)}n(p,"roundNumber");function c(r){return Object.entries(r).reduce((t,[s,o])=>{let a=s;return t[a]=p(o),t},{})}n(c,"roundObjectValuesNumber");function C(r){let t=Math.max(0,r);return t=Math.min(t,255),t=Math.ceil(t),"0123456789ABCDEF".charAt((t-t%16)/16)+"0123456789ABCDEF".charAt(t%16)}n(C,"colorAlphaToHex");function x(r){return r.startsWith("#")?"hex":r.startsWith("rgb")?"rgb":r.startsWith("hsl")?"hsl":null}n(x,"getColorFormat");function $(r){try{let t=l__default.default(r),s=t.rgb(),o=c(s.object()),a=typeof o.alpha=="number"&&o.alpha!==1,g=a?`rgba(${o.r}, ${o.g}, ${o.b}, ${o.alpha})`:`rgb(${o.r}, ${o.g}, ${o.b})`,b=t.hsl(),i=c(b.object()),h=a?`hsla(${i.h}, ${i.s}%, ${i.l}%, ${i.alpha})`:`hsl(${i.h}, ${i.s}%, ${i.l}%)`,u=t.hex();return a&&(u=`${u}${C(o.alpha*256)}`),{original:r,format:x(r),rgbInfo:o,rgbString:g,hslInfo:i,hslString:h,hex:u,isTransparent:a,luminosity:t.luminosity(),isDark:t.isDark(),isLight:t.isLight(),color:t}}catch(t){throw new Error(`Could not analyze the color "${r}" - ${t.message}`)}}n($,"analyzeColorBase");var e=m__default.default($);function F(r){try{return e(r),!0}catch{return !1}}n(F,"isColor");function y(r){e(r);}n(y,"assertColor");function d(r){return e(r).rgbInfo}n(d,"getColorRgbInfo");function I(r){return e(r).hslInfo}n(I,"getColorHslInfo");function R(r){return !e(r).isTransparent}n(R,"hasOpacity");function D(r){return e(r).luminosity}n(D,"getColorLuminosity");function H(r){return e(r).isDark}n(H,"isDarkColor");function w(r){return e(r).isLight}n(w,"isLightColor");function A({color:r,format:t}){switch(y(r),t){case"rgb":{let s=d(r);return l__default.default(s).string()}case"hsl":{let s=I(r);return l__default.default(s).string()}case"hex":default:return l__default.default(r).hex()}}n(A,"convertColor");function L(r){return e(r).hex}n(L,"convertTransparentColorToHex");function M({foregroundColor:r,bgColor:t}){let s=e(r),o=e(t);return s.color.contrast(o.color)>=4.5}n(M,"isColorContrastEnough");
|
12
|
+
|
13
|
+
exports.analyzeColor = e;
|
14
|
+
exports.assertColor = y;
|
15
|
+
exports.convertColor = A;
|
16
|
+
exports.convertTransparentColorToHex = L;
|
17
|
+
exports.getColorHslInfo = I;
|
18
|
+
exports.getColorLuminosity = D;
|
19
|
+
exports.getColorRgbInfo = d;
|
20
|
+
exports.hasOpacity = R;
|
21
|
+
exports.isColor = F;
|
22
|
+
exports.isColorContrastEnough = M;
|
23
|
+
exports.isDarkColor = H;
|
24
|
+
exports.isLightColor = w;
|
25
|
+
//# sourceMappingURL=index.js.map
|
199
26
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import Color from 'color';\nimport loMemo from 'lodash/memoize.js';\n// need to leave this imported even though it doesn't look like it's being used for proper typing of `lodash/memoize`\n\nexport type ColorRgbInfo = {\n /** From 0 - 255 */\n r: number;\n /** From 0 - 255 */\n g: number;\n /** From 0 - 255 */\n b: number;\n /** From 0 - 1 */\n alpha?: number;\n};\n\nexport type ColorHslInfo = {\n /** From 0 - 255 */\n h: number;\n /** From 0 - 255 */\n s: number;\n /** From 0 - 255 */\n l: number;\n /** From 0 - 1 */\n alpha?: number;\n};\n\nexport type ColorFormat = 'hex' | 'hsl' | 'rgb';\n\n/**\n * If a number is less than 1, round it to a hundredth (i.e. 0.87), else round to nearest integer.\n */\nfunction roundNumber(value: number): number {\n return value < 1 ? Number(value.toFixed(2)) : Math.round(value);\n}\n\nfunction roundObjectValuesNumber<Obj extends Record<string, number>>(\n obj: Obj,\n): Record<keyof Obj, number> {\n return Object.entries(obj).reduce((cur, [key, value]) => {\n const k = key as keyof Obj;\n cur[k] = roundNumber(value);\n return cur;\n }, {} as Record<keyof Obj, number>);\n}\n\n/**\n * Convert a transparency to the 7th & 8th character for an 8 digit hex color\n * @link https://gist.github.com/lopspower/03fb1cc0ac9f32ef38f4\n */\nfunction colorAlphaToHex(value: number): string {\n let val = Math.max(0, value);\n val = Math.min(val, 255);\n val = Math.ceil(val);\n return (\n '0123456789ABCDEF'.charAt((val - (val % 16)) / 16) +\n '0123456789ABCDEF'.charAt(val % 16)\n );\n}\n\nfunction getColorFormat(color: string): ColorFormat | null {\n if (color.startsWith('#')) return 'hex';\n if (color.startsWith('rgb')) return 'rgb';\n if (color.startsWith('hsl')) return 'hsl';\n return null;\n}\n\nfunction analyzeColorBase(color: string) {\n try {\n const c = Color(color);\n const rgb = c.rgb();\n const rgbInfo = roundObjectValuesNumber(\n rgb.object() as unknown as ColorRgbInfo,\n );\n const isTransparent =\n typeof rgbInfo.alpha === 'number' && rgbInfo.alpha !== 1;\n const rgbString = isTransparent\n ? `rgba(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b}, ${rgbInfo.alpha})`\n : `rgb(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b})`;\n const hsl = c.hsl();\n const hslInfo = roundObjectValuesNumber(\n hsl.object() as unknown as ColorHslInfo,\n );\n const hslString = isTransparent\n ? `hsla(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%, ${hslInfo.alpha})`\n : `hsl(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%)`;\n let hex = c.hex();\n if (isTransparent) {\n // 6 => 8 character transparent hex\n hex = `${hex}${colorAlphaToHex(rgbInfo.alpha * 256)}`;\n }\n return {\n original: color,\n format: getColorFormat(color),\n rgbInfo,\n rgbString,\n hslInfo,\n hslString,\n hex,\n isTransparent,\n luminosity: c.luminosity(),\n isDark: c.isDark(),\n isLight: c.isLight(),\n color: c,\n };\n } catch (e) {\n throw new Error(`Could not analyze the color \"${color}\" - ${e.message}`);\n }\n}\n\nexport const analyzeColor = loMemo(analyzeColorBase);\n\n/**\n * Is a valid css color\n */\nexport function isColor(color: string): boolean {\n try {\n analyzeColor(color);\n return true;\n } catch (e) {\n return false;\n }\n}\n\nexport function assertColor(color: string): asserts color is string {\n analyzeColor(color);\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorRgbInfo(color: string): ColorRgbInfo {\n return analyzeColor(color).rgbInfo;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorHslInfo(color: string): ColorHslInfo {\n return analyzeColor(color).hslInfo;\n}\n\n/**\n * Has Opacity\n * Returns true if a color has opacity\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function hasOpacity(color: string): boolean {\n return !analyzeColor(color).isTransparent;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorLuminosity(color: string): number {\n return analyzeColor(color).luminosity;\n}\n\n/**\n * Color Contrast\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n * @param color - color value to check. supports any number of formats (hex, rgb, hsl, etc)\n * @returns {boolean} - true if the color is \"dark\" (ie. you should use a lighter color on top) and false if the color is light.\n */\nexport function isDarkColor(color: string): boolean {\n return analyzeColor(color).isDark;\n}\n\n/**\n * Color Contrast\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n * @param color - color value to check. supports any number of formats (hex, rgb, hsl, etc)\n * @returns {boolean} - true if the color is \"light\" (ie. you should use a darker color on top) and false if the color is dark.\n */\nexport function isLightColor(color: string): boolean {\n return analyzeColor(color).isLight;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function convertColor({\n color,\n format,\n}: {\n color: string;\n format: ColorFormat;\n}): string {\n assertColor(color);\n switch (format) {\n case 'rgb': {\n const rgb = getColorRgbInfo(color);\n return Color(rgb).string();\n }\n case 'hsl': {\n const hsl = getColorHslInfo(color);\n return Color(hsl).string();\n }\n case 'hex':\n default:\n return Color(color).hex();\n }\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function convertTransparentColorToHex(color: string): string {\n return analyzeColor(color).hex;\n}\n\nconst toPercent = (n: number): string => `${n * 100}%`;\n\n/**\n * Contrast checking.\n *\n * IMPORTANT: CANNOT USE ALPHA CHANNELS IN CONTRAST CALCULATIONS.\n * This can only compare 2 SOLID colors.\n */\nexport function isColorContrastEnough({\n foregroundColor,\n bgColor,\n}: {\n foregroundColor: string;\n bgColor: string;\n}): boolean {\n const f = analyzeColor(foregroundColor);\n const b = analyzeColor(bgColor);\n\n // https://www.w3.org/TR/WCAG20/#contrast-ratiodef\n // (L1 + 0.05) / (L2 + 0.05), where\n // L1 is the relative luminance of the lighter of the colors, and\n // L2 is the relative luminance of the darker of the colors.\n const contrast = f.color.contrast(b.color);\n\n // https://webaim.org/resources/contrastchecker/\n return contrast >= 4.5;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AAClB,qBAAmB;AA8BnB,SAAS,YAAY,OAAuB;AAC1C,SAAO,QAAQ,IAAI,OAAO,MAAM,QAAQ,CAAC,CAAC,IAAI,KAAK,MAAM,KAAK;AAChE;AAFS;AAIT,SAAS,wBAC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A,IACd,KAAK,OAAO;AACV,YAAM,MAAM,gBAAgB,KAAK;AACjC,iBAAO,aAAAD,SAAM,GAAG,EAAE,OAAO;AAAA,IAC3B;AAAA,IACA,KAAK,OAAO;AACV,YAAM,MAAM,gBAAgB,KAAK;AACjC,iBAAO,aAAAA,SAAM,GAAG,EAAE,OAAO;AAAA,IAC3B;AAAA,IACA,KAAK;AAAA,IACL;AACE,iBAAO,aAAAA,SAAM,KAAK,EAAE,IAAI;AAAA,EAC5B;AACF;AArBgB;AA2BT,SAAS,6BAA6B,OAAuB;AAClE,SAAO,aAAa,KAAK,EAAE;AAC7B;AAFgB;AAYT,SAAS,sBAAsB;AAAA,EACpC;AAAA,EACA;AACF,GAGY;AACV,QAAM,IAAI,aAAa,eAAe;AACtC,QAAM,IAAI,aAAa,OAAO;AAM9B,QAAM,WAAW,EAAE,MAAM,SAAS,EAAE,KAAK;AAGzC,SAAO,YAAY;AACrB;AAlBgB;","names":["Color","loMemo"]}
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["roundNumber","value","__name","roundObjectValuesNumber","obj","cur","key","k","colorAlphaToHex","val","getColorFormat","color","analyzeColorBase","c","Color","rgb","rgbInfo","isTransparent","rgbString","hsl","hslInfo","hslString","hex","e","analyzeColor","loMemo","isColor","assertColor","getColorRgbInfo","getColorHslInfo","hasOpacity","getColorLuminosity","isDarkColor","isLightColor","convertColor","format","convertTransparentColorToHex","isColorContrastEnough","foregroundColor","bgColor","f","b"],"mappings":";;;;;;;;;;+EA+BA,SAASA,CAAAA,CAAYC,CAAuB,CAAA,CAC1C,OAAOA,CAAAA,CAAQ,CAAI,CAAA,MAAA,CAAOA,CAAM,CAAA,OAAA,CAAQ,CAAC,CAAC,CAAI,CAAA,IAAA,CAAK,KAAMA,CAAAA,CAAK,CAChE,CAFSC,CAAAF,CAAAA,CAAAA,CAAA,aAIT,CAAA,CAAA,SAASG,CACPC,CAAAA,CAAAA,CAC2B,CAC3B,OAAO,MAAA,CAAO,OAAQA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,CAAIhB,CAAAA,CAAAA,CAAac,CAAe,CAAA,CAChCG,CAAIjB,CAAAA,CAAAA,CAAae,CAAO,CAAA,CAS9B,OAHiBC,CAAAA,CAAE,KAAM,CAAA,QAAA,CAASC,CAAE,CAAA,KAAK,CAGtB,EAAA,GACrB,CAlBgBvC,CAAAA,CAAAmC,CAAA,CAAA,uBAAA,CAAA","file":"index.js","sourcesContent":["import Color from 'color';\nimport loMemo from 'lodash/memoize.js';\n// need to leave this imported even though it doesn't look like it's being used for proper typing of `lodash/memoize`\n\nexport type ColorRgbInfo = {\n /** From 0 - 255 */\n r: number;\n /** From 0 - 255 */\n g: number;\n /** From 0 - 255 */\n b: number;\n /** From 0 - 1 */\n alpha?: number;\n};\n\nexport type ColorHslInfo = {\n /** From 0 - 255 */\n h: number;\n /** From 0 - 255 */\n s: number;\n /** From 0 - 255 */\n l: number;\n /** From 0 - 1 */\n alpha?: number;\n};\n\nexport type ColorFormat = 'hex' | 'hsl' | 'rgb';\n\n/**\n * If a number is less than 1, round it to a hundredth (i.e. 0.87), else round to nearest integer.\n */\nfunction roundNumber(value: number): number {\n return value < 1 ? Number(value.toFixed(2)) : Math.round(value);\n}\n\nfunction roundObjectValuesNumber<Obj extends Record<string, number>>(\n obj: Obj,\n): Record<keyof Obj, number> {\n return Object.entries(obj).reduce((cur, [key, value]) => {\n const k = key as keyof Obj;\n cur[k] = roundNumber(value);\n return cur;\n }, {} as Record<keyof Obj, number>);\n}\n\n/**\n * Convert a transparency to the 7th & 8th character for an 8 digit hex color\n * @link https://gist.github.com/lopspower/03fb1cc0ac9f32ef38f4\n */\nfunction colorAlphaToHex(value: number): string {\n let val = Math.max(0, value);\n val = Math.min(val, 255);\n val = Math.ceil(val);\n return (\n '0123456789ABCDEF'.charAt((val - (val % 16)) / 16) +\n '0123456789ABCDEF'.charAt(val % 16)\n );\n}\n\nfunction getColorFormat(color: string): ColorFormat | null {\n if (color.startsWith('#')) return 'hex';\n if (color.startsWith('rgb')) return 'rgb';\n if (color.startsWith('hsl')) return 'hsl';\n return null;\n}\n\nfunction analyzeColorBase(color: string) {\n try {\n const c = Color(color);\n const rgb = c.rgb();\n const rgbInfo = roundObjectValuesNumber(\n rgb.object() as unknown as ColorRgbInfo,\n );\n const isTransparent =\n typeof rgbInfo.alpha === 'number' && rgbInfo.alpha !== 1;\n const rgbString = isTransparent\n ? `rgba(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b}, ${rgbInfo.alpha})`\n : `rgb(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b})`;\n const hsl = c.hsl();\n const hslInfo = roundObjectValuesNumber(\n hsl.object() as unknown as ColorHslInfo,\n );\n const hslString = isTransparent\n ? `hsla(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%, ${hslInfo.alpha})`\n : `hsl(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%)`;\n let hex = c.hex();\n if (isTransparent) {\n // 6 => 8 character transparent hex\n hex = `${hex}${colorAlphaToHex(rgbInfo.alpha * 256)}`;\n }\n return {\n original: color,\n format: getColorFormat(color),\n rgbInfo,\n rgbString,\n hslInfo,\n hslString,\n hex,\n isTransparent,\n luminosity: c.luminosity(),\n isDark: c.isDark(),\n isLight: c.isLight(),\n color: c,\n };\n } catch (e) {\n throw new Error(`Could not analyze the color \"${color}\" - ${e.message}`);\n }\n}\n\nexport const analyzeColor = loMemo(analyzeColorBase);\n\n/**\n * Is a valid css color\n */\nexport function isColor(color: string): boolean {\n try {\n analyzeColor(color);\n return true;\n } catch (e) {\n return false;\n }\n}\n\nexport function assertColor(color: string): asserts color is string {\n analyzeColor(color);\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorRgbInfo(color: string): ColorRgbInfo {\n return analyzeColor(color).rgbInfo;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorHslInfo(color: string): ColorHslInfo {\n return analyzeColor(color).hslInfo;\n}\n\n/**\n * Has Opacity\n * Returns true if a color has opacity\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function hasOpacity(color: string): boolean {\n return !analyzeColor(color).isTransparent;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorLuminosity(color: string): number {\n return analyzeColor(color).luminosity;\n}\n\n/**\n * Color Contrast\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n * @param color - color value to check. supports any number of formats (hex, rgb, hsl, etc)\n * @returns {boolean} - true if the color is \"dark\" (ie. you should use a lighter color on top) and false if the color is light.\n */\nexport function isDarkColor(color: string): boolean {\n return analyzeColor(color).isDark;\n}\n\n/**\n * Color Contrast\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n * @param color - color value to check. supports any number of formats (hex, rgb, hsl, etc)\n * @returns {boolean} - true if the color is \"light\" (ie. you should use a darker color on top) and false if the color is dark.\n */\nexport function isLightColor(color: string): boolean {\n return analyzeColor(color).isLight;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function convertColor({\n color,\n format,\n}: {\n color: string;\n format: ColorFormat;\n}): string {\n assertColor(color);\n switch (format) {\n case 'rgb': {\n const rgb = getColorRgbInfo(color);\n return Color(rgb).string();\n }\n case 'hsl': {\n const hsl = getColorHslInfo(color);\n return Color(hsl).string();\n }\n case 'hex':\n default:\n return Color(color).hex();\n }\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function convertTransparentColorToHex(color: string): string {\n return analyzeColor(color).hex;\n}\n\nconst toPercent = (n: number): string => `${n * 100}%`;\n\n/**\n * Contrast checking.\n *\n * IMPORTANT: CANNOT USE ALPHA CHANNELS IN CONTRAST CALCULATIONS.\n * This can only compare 2 SOLID colors.\n */\nexport function isColorContrastEnough({\n foregroundColor,\n bgColor,\n}: {\n foregroundColor: string;\n bgColor: string;\n}): boolean {\n const f = analyzeColor(foregroundColor);\n const b = analyzeColor(bgColor);\n\n // https://www.w3.org/TR/WCAG20/#contrast-ratiodef\n // (L1 + 0.05) / (L2 + 0.05), where\n // L1 is the relative luminance of the lighter of the colors, and\n // L2 is the relative luminance of the darker of the colors.\n const contrast = f.color.contrast(b.color);\n\n // https://webaim.org/resources/contrastchecker/\n return contrast >= 4.5;\n}\n"]}
|
package/dist/index.mjs
CHANGED
@@ -1,156 +1,8 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
import l from 'color';
|
2
|
+
import m from 'lodash/memoize.js';
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
return value < 1 ? Number(value.toFixed(2)) : Math.round(value);
|
9
|
-
}
|
10
|
-
__name(roundNumber, "roundNumber");
|
11
|
-
function roundObjectValuesNumber(obj) {
|
12
|
-
return Object.entries(obj).reduce((cur, [key, value]) => {
|
13
|
-
const k = key;
|
14
|
-
cur[k] = roundNumber(value);
|
15
|
-
return cur;
|
16
|
-
}, {});
|
17
|
-
}
|
18
|
-
__name(roundObjectValuesNumber, "roundObjectValuesNumber");
|
19
|
-
function colorAlphaToHex(value) {
|
20
|
-
let val = Math.max(0, value);
|
21
|
-
val = Math.min(val, 255);
|
22
|
-
val = Math.ceil(val);
|
23
|
-
return "0123456789ABCDEF".charAt((val - val % 16) / 16) + "0123456789ABCDEF".charAt(val % 16);
|
24
|
-
}
|
25
|
-
__name(colorAlphaToHex, "colorAlphaToHex");
|
26
|
-
function getColorFormat(color) {
|
27
|
-
if (color.startsWith("#")) return "hex";
|
28
|
-
if (color.startsWith("rgb")) return "rgb";
|
29
|
-
if (color.startsWith("hsl")) return "hsl";
|
30
|
-
return null;
|
31
|
-
}
|
32
|
-
__name(getColorFormat, "getColorFormat");
|
33
|
-
function analyzeColorBase(color) {
|
34
|
-
try {
|
35
|
-
const c = Color(color);
|
36
|
-
const rgb = c.rgb();
|
37
|
-
const rgbInfo = roundObjectValuesNumber(
|
38
|
-
rgb.object()
|
39
|
-
);
|
40
|
-
const isTransparent = typeof rgbInfo.alpha === "number" && rgbInfo.alpha !== 1;
|
41
|
-
const rgbString = isTransparent ? `rgba(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b}, ${rgbInfo.alpha})` : `rgb(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b})`;
|
42
|
-
const hsl = c.hsl();
|
43
|
-
const hslInfo = roundObjectValuesNumber(
|
44
|
-
hsl.object()
|
45
|
-
);
|
46
|
-
const hslString = isTransparent ? `hsla(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%, ${hslInfo.alpha})` : `hsl(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%)`;
|
47
|
-
let hex = c.hex();
|
48
|
-
if (isTransparent) {
|
49
|
-
hex = `${hex}${colorAlphaToHex(rgbInfo.alpha * 256)}`;
|
50
|
-
}
|
51
|
-
return {
|
52
|
-
original: color,
|
53
|
-
format: getColorFormat(color),
|
54
|
-
rgbInfo,
|
55
|
-
rgbString,
|
56
|
-
hslInfo,
|
57
|
-
hslString,
|
58
|
-
hex,
|
59
|
-
isTransparent,
|
60
|
-
luminosity: c.luminosity(),
|
61
|
-
isDark: c.isDark(),
|
62
|
-
isLight: c.isLight(),
|
63
|
-
color: c
|
64
|
-
};
|
65
|
-
} catch (e) {
|
66
|
-
throw new Error(`Could not analyze the color "${color}" - ${e.message}`);
|
67
|
-
}
|
68
|
-
}
|
69
|
-
__name(analyzeColorBase, "analyzeColorBase");
|
70
|
-
var analyzeColor = loMemo(analyzeColorBase);
|
71
|
-
function isColor(color) {
|
72
|
-
try {
|
73
|
-
analyzeColor(color);
|
74
|
-
return true;
|
75
|
-
} catch (e) {
|
76
|
-
return false;
|
77
|
-
}
|
78
|
-
}
|
79
|
-
__name(isColor, "isColor");
|
80
|
-
function assertColor(color) {
|
81
|
-
analyzeColor(color);
|
82
|
-
}
|
83
|
-
__name(assertColor, "assertColor");
|
84
|
-
function getColorRgbInfo(color) {
|
85
|
-
return analyzeColor(color).rgbInfo;
|
86
|
-
}
|
87
|
-
__name(getColorRgbInfo, "getColorRgbInfo");
|
88
|
-
function getColorHslInfo(color) {
|
89
|
-
return analyzeColor(color).hslInfo;
|
90
|
-
}
|
91
|
-
__name(getColorHslInfo, "getColorHslInfo");
|
92
|
-
function hasOpacity(color) {
|
93
|
-
return !analyzeColor(color).isTransparent;
|
94
|
-
}
|
95
|
-
__name(hasOpacity, "hasOpacity");
|
96
|
-
function getColorLuminosity(color) {
|
97
|
-
return analyzeColor(color).luminosity;
|
98
|
-
}
|
99
|
-
__name(getColorLuminosity, "getColorLuminosity");
|
100
|
-
function isDarkColor(color) {
|
101
|
-
return analyzeColor(color).isDark;
|
102
|
-
}
|
103
|
-
__name(isDarkColor, "isDarkColor");
|
104
|
-
function isLightColor(color) {
|
105
|
-
return analyzeColor(color).isLight;
|
106
|
-
}
|
107
|
-
__name(isLightColor, "isLightColor");
|
108
|
-
function convertColor({
|
109
|
-
color,
|
110
|
-
format
|
111
|
-
}) {
|
112
|
-
assertColor(color);
|
113
|
-
switch (format) {
|
114
|
-
case "rgb": {
|
115
|
-
const rgb = getColorRgbInfo(color);
|
116
|
-
return Color(rgb).string();
|
117
|
-
}
|
118
|
-
case "hsl": {
|
119
|
-
const hsl = getColorHslInfo(color);
|
120
|
-
return Color(hsl).string();
|
121
|
-
}
|
122
|
-
case "hex":
|
123
|
-
default:
|
124
|
-
return Color(color).hex();
|
125
|
-
}
|
126
|
-
}
|
127
|
-
__name(convertColor, "convertColor");
|
128
|
-
function convertTransparentColorToHex(color) {
|
129
|
-
return analyzeColor(color).hex;
|
130
|
-
}
|
131
|
-
__name(convertTransparentColorToHex, "convertTransparentColorToHex");
|
132
|
-
function isColorContrastEnough({
|
133
|
-
foregroundColor,
|
134
|
-
bgColor
|
135
|
-
}) {
|
136
|
-
const f = analyzeColor(foregroundColor);
|
137
|
-
const b = analyzeColor(bgColor);
|
138
|
-
const contrast = f.color.contrast(b.color);
|
139
|
-
return contrast >= 4.5;
|
140
|
-
}
|
141
|
-
__name(isColorContrastEnough, "isColorContrastEnough");
|
142
|
-
export {
|
143
|
-
analyzeColor,
|
144
|
-
assertColor,
|
145
|
-
convertColor,
|
146
|
-
convertTransparentColorToHex,
|
147
|
-
getColorHslInfo,
|
148
|
-
getColorLuminosity,
|
149
|
-
getColorRgbInfo,
|
150
|
-
hasOpacity,
|
151
|
-
isColor,
|
152
|
-
isColorContrastEnough,
|
153
|
-
isDarkColor,
|
154
|
-
isLightColor
|
155
|
-
};
|
4
|
+
var f=Object.defineProperty;var n=(r,t)=>f(r,"name",{value:t,configurable:!0});function p(r){return r<1?Number(r.toFixed(2)):Math.round(r)}n(p,"roundNumber");function c(r){return Object.entries(r).reduce((t,[s,o])=>{let a=s;return t[a]=p(o),t},{})}n(c,"roundObjectValuesNumber");function C(r){let t=Math.max(0,r);return t=Math.min(t,255),t=Math.ceil(t),"0123456789ABCDEF".charAt((t-t%16)/16)+"0123456789ABCDEF".charAt(t%16)}n(C,"colorAlphaToHex");function x(r){return r.startsWith("#")?"hex":r.startsWith("rgb")?"rgb":r.startsWith("hsl")?"hsl":null}n(x,"getColorFormat");function $(r){try{let t=l(r),s=t.rgb(),o=c(s.object()),a=typeof o.alpha=="number"&&o.alpha!==1,g=a?`rgba(${o.r}, ${o.g}, ${o.b}, ${o.alpha})`:`rgb(${o.r}, ${o.g}, ${o.b})`,b=t.hsl(),i=c(b.object()),h=a?`hsla(${i.h}, ${i.s}%, ${i.l}%, ${i.alpha})`:`hsl(${i.h}, ${i.s}%, ${i.l}%)`,u=t.hex();return a&&(u=`${u}${C(o.alpha*256)}`),{original:r,format:x(r),rgbInfo:o,rgbString:g,hslInfo:i,hslString:h,hex:u,isTransparent:a,luminosity:t.luminosity(),isDark:t.isDark(),isLight:t.isLight(),color:t}}catch(t){throw new Error(`Could not analyze the color "${r}" - ${t.message}`)}}n($,"analyzeColorBase");var e=m($);function F(r){try{return e(r),!0}catch{return !1}}n(F,"isColor");function y(r){e(r);}n(y,"assertColor");function d(r){return e(r).rgbInfo}n(d,"getColorRgbInfo");function I(r){return e(r).hslInfo}n(I,"getColorHslInfo");function R(r){return !e(r).isTransparent}n(R,"hasOpacity");function D(r){return e(r).luminosity}n(D,"getColorLuminosity");function H(r){return e(r).isDark}n(H,"isDarkColor");function w(r){return e(r).isLight}n(w,"isLightColor");function A({color:r,format:t}){switch(y(r),t){case"rgb":{let s=d(r);return l(s).string()}case"hsl":{let s=I(r);return l(s).string()}case"hex":default:return l(r).hex()}}n(A,"convertColor");function L(r){return e(r).hex}n(L,"convertTransparentColorToHex");function M({foregroundColor:r,bgColor:t}){let s=e(r),o=e(t);return s.color.contrast(o.color)>=4.5}n(M,"isColorContrastEnough");
|
5
|
+
|
6
|
+
export { e as analyzeColor, y as assertColor, A as convertColor, L as convertTransparentColorToHex, I as getColorHslInfo, D as getColorLuminosity, d as getColorRgbInfo, R as hasOpacity, F as isColor, M as isColorContrastEnough, H as isDarkColor, w as isLightColor };
|
7
|
+
//# sourceMappingURL=index.mjs.map
|
156
8
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import Color from 'color';\nimport loMemo from 'lodash/memoize.js';\n// need to leave this imported even though it doesn't look like it's being used for proper typing of `lodash/memoize`\n\nexport type ColorRgbInfo = {\n /** From 0 - 255 */\n r: number;\n /** From 0 - 255 */\n g: number;\n /** From 0 - 255 */\n b: number;\n /** From 0 - 1 */\n alpha?: number;\n};\n\nexport type ColorHslInfo = {\n /** From 0 - 255 */\n h: number;\n /** From 0 - 255 */\n s: number;\n /** From 0 - 255 */\n l: number;\n /** From 0 - 1 */\n alpha?: number;\n};\n\nexport type ColorFormat = 'hex' | 'hsl' | 'rgb';\n\n/**\n * If a number is less than 1, round it to a hundredth (i.e. 0.87), else round to nearest integer.\n */\nfunction roundNumber(value: number): number {\n return value < 1 ? Number(value.toFixed(2)) : Math.round(value);\n}\n\nfunction roundObjectValuesNumber<Obj extends Record<string, number>>(\n obj: Obj,\n): Record<keyof Obj, number> {\n return Object.entries(obj).reduce((cur, [key, value]) => {\n const k = key as keyof Obj;\n cur[k] = roundNumber(value);\n return cur;\n }, {} as Record<keyof Obj, number>);\n}\n\n/**\n * Convert a transparency to the 7th & 8th character for an 8 digit hex color\n * @link https://gist.github.com/lopspower/03fb1cc0ac9f32ef38f4\n */\nfunction colorAlphaToHex(value: number): string {\n let val = Math.max(0, value);\n val = Math.min(val, 255);\n val = Math.ceil(val);\n return (\n '0123456789ABCDEF'.charAt((val - (val % 16)) / 16) +\n '0123456789ABCDEF'.charAt(val % 16)\n );\n}\n\nfunction getColorFormat(color: string): ColorFormat | null {\n if (color.startsWith('#')) return 'hex';\n if (color.startsWith('rgb')) return 'rgb';\n if (color.startsWith('hsl')) return 'hsl';\n return null;\n}\n\nfunction analyzeColorBase(color: string) {\n try {\n const c = Color(color);\n const rgb = c.rgb();\n const rgbInfo = roundObjectValuesNumber(\n rgb.object() as unknown as ColorRgbInfo,\n );\n const isTransparent =\n typeof rgbInfo.alpha === 'number' && rgbInfo.alpha !== 1;\n const rgbString = isTransparent\n ? `rgba(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b}, ${rgbInfo.alpha})`\n : `rgb(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b})`;\n const hsl = c.hsl();\n const hslInfo = roundObjectValuesNumber(\n hsl.object() as unknown as ColorHslInfo,\n );\n const hslString = isTransparent\n ? `hsla(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%, ${hslInfo.alpha})`\n : `hsl(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%)`;\n let hex = c.hex();\n if (isTransparent) {\n // 6 => 8 character transparent hex\n hex = `${hex}${colorAlphaToHex(rgbInfo.alpha * 256)}`;\n }\n return {\n original: color,\n format: getColorFormat(color),\n rgbInfo,\n rgbString,\n hslInfo,\n hslString,\n hex,\n isTransparent,\n luminosity: c.luminosity(),\n isDark: c.isDark(),\n isLight: c.isLight(),\n color: c,\n };\n } catch (e) {\n throw new Error(`Could not analyze the color \"${color}\" - ${e.message}`);\n }\n}\n\nexport const analyzeColor = loMemo(analyzeColorBase);\n\n/**\n * Is a valid css color\n */\nexport function isColor(color: string): boolean {\n try {\n analyzeColor(color);\n return true;\n } catch (e) {\n return false;\n }\n}\n\nexport function assertColor(color: string): asserts color is string {\n analyzeColor(color);\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorRgbInfo(color: string): ColorRgbInfo {\n return analyzeColor(color).rgbInfo;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorHslInfo(color: string): ColorHslInfo {\n return analyzeColor(color).hslInfo;\n}\n\n/**\n * Has Opacity\n * Returns true if a color has opacity\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function hasOpacity(color: string): boolean {\n return !analyzeColor(color).isTransparent;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorLuminosity(color: string): number {\n return analyzeColor(color).luminosity;\n}\n\n/**\n * Color Contrast\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n * @param color - color value to check. supports any number of formats (hex, rgb, hsl, etc)\n * @returns {boolean} - true if the color is \"dark\" (ie. you should use a lighter color on top) and false if the color is light.\n */\nexport function isDarkColor(color: string): boolean {\n return analyzeColor(color).isDark;\n}\n\n/**\n * Color Contrast\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n * @param color - color value to check. supports any number of formats (hex, rgb, hsl, etc)\n * @returns {boolean} - true if the color is \"light\" (ie. you should use a darker color on top) and false if the color is dark.\n */\nexport function isLightColor(color: string): boolean {\n return analyzeColor(color).isLight;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function convertColor({\n color,\n format,\n}: {\n color: string;\n format: ColorFormat;\n}): string {\n assertColor(color);\n switch (format) {\n case 'rgb': {\n const rgb = getColorRgbInfo(color);\n return Color(rgb).string();\n }\n case 'hsl': {\n const hsl = getColorHslInfo(color);\n return Color(hsl).string();\n }\n case 'hex':\n default:\n return Color(color).hex();\n }\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function convertTransparentColorToHex(color: string): string {\n return analyzeColor(color).hex;\n}\n\nconst toPercent = (n: number): string => `${n * 100}%`;\n\n/**\n * Contrast checking.\n *\n * IMPORTANT: CANNOT USE ALPHA CHANNELS IN CONTRAST CALCULATIONS.\n * This can only compare 2 SOLID colors.\n */\nexport function isColorContrastEnough({\n foregroundColor,\n bgColor,\n}: {\n foregroundColor: string;\n bgColor: string;\n}): boolean {\n const f = analyzeColor(foregroundColor);\n const b = analyzeColor(bgColor);\n\n // https://www.w3.org/TR/WCAG20/#contrast-ratiodef\n // (L1 + 0.05) / (L2 + 0.05), where\n // L1 is the relative luminance of the lighter of the colors, and\n // L2 is the relative luminance of the darker of the colors.\n const contrast = f.color.contrast(b.color);\n\n // https://webaim.org/resources/contrastchecker/\n return contrast >= 4.5;\n}\n"],"mappings":";;;;AAAA,OAAO,WAAW;AAClB,OAAO,YAAY;AA8BnB,SAAS,YAAY,OAAuB;AAC1C,SAAO,QAAQ,IAAI,OAAO,MAAM,QAAQ,CAAC,CAAC,IAAI,KAAK,MAAM,KAAK;AAChE;AAFS;AAIT,SAAS,wBACP,KAC2B;AAC3B,SAAO,OAAO,QAAQ,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK,MAAM;AACvD,UAAM,IAAI;AACV,QAAI,CAAC,IAAI,YAAY,KAAK;AAC1B,WAAO;AAAA,EACT,GAAG,CAAC,CAA8B;AACpC;AARS;AAcT,SAAS,gBAAgB,OAAuB;AAC9C,MAAI,MAAM,KAAK,IAAI,GAAG,KAAK;AAC3B,QAAM,KAAK,IAAI,KAAK,GAAG;AACvB,QAAM,KAAK,KAAK,GAAG;AACnB,SACE,mBAAmB,QAAQ,MAAO,MAAM,MAA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pC;AAAA,EACA;AACF,GAGY;AACV,QAAM,IAAI,aAAa,eAAe;AACtC,QAAM,IAAI,aAAa,OAAO;AAM9B,QAAM,WAAW,EAAE,MAAM,SAAS,EAAE,KAAK;AAGzC,SAAO,YAAY;AACrB;AAlBgB;","names":[]}
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["roundNumber","value","__name","roundObjectValuesNumber","obj","cur","key","k","colorAlphaToHex","val","getColorFormat","color","analyzeColorBase","c","Color","rgb","rgbInfo","isTransparent","rgbString","hsl","hslInfo","hslString","hex","e","analyzeColor","loMemo","isColor","assertColor","getColorRgbInfo","getColorHslInfo","hasOpacity","getColorLuminosity","isDarkColor","isLightColor","convertColor","format","convertTransparentColorToHex","isColorContrastEnough","foregroundColor","bgColor","f","b"],"mappings":";;;+EA+BA,SAASA,CAAAA,CAAYC,CAAuB,CAAA,CAC1C,OAAOA,CAAAA,CAAQ,CAAI,CAAA,MAAA,CAAOA,CAAM,CAAA,OAAA,CAAQ,CAAC,CAAC,CAAI,CAAA,IAAA,CAAK,KAAMA,CAAAA,CAAK,CAChE,CAFSC,CAAAF,CAAAA,CAAAA,CAAA,aAIT,CAAA,CAAA,SAASG,CACPC,CAAAA,CAAAA,CAC2B,CAC3B,OAAO,MAAA,CAAO,OAAQA,CAAAA,CAAG,CAAE,CAAA,MAAA,CAAO,CAACC,CAAAA,CAAK,CAACC,CAAAA,CAAKL,CAAK,CAAA,GAAM,CACvD,IAAMM,CAAID,CAAAA,CAAAA,CACV,OAAAD,CAAAA,CAAIE,CAAC,CAAA,CAAIP,CAAYC,CAAAA,CAAK,CACnBI,CAAAA,CACT,CAAG,CAAA,EAA+B,CACpC,CARSH,CAAAA,CAAAC,EAAA,yBAcT,CAAA,CAAA,SAASK,CAAgBP,CAAAA,CAAAA,CAAuB,CAC9C,IAAIQ,CAAM,CAAA,IAAA,CAAK,GAAI,CAAA,CAAA,CAAGR,CAAK,CAAA,CAC3B,OAAAQ,CAAAA,CAAM,IAAK,CA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CAAA,SAASC,CAAa,CAAA,CAC3B,KAAAvB,CAAAA,CAAAA,CACA,MAAAwB,CAAAA,CACF,CAGW,CAAA,CAET,OADAR,CAAAA,CAAYhB,CAAK,CAAA,CACTwB,CAAQ,EACd,IAAK,KAAA,CAAO,CACV,IAAMpB,CAAMa,CAAAA,CAAAA,CAAgBjB,CAAK,CAAA,CACjC,OAAOG,CAAAA,CAAMC,CAAG,CAAA,CAAE,MAAO,EAC3B,CACA,IAAK,KAAO,CAAA,CACV,IAAMI,CAAAA,CAAMU,CAAgBlB,CAAAA,CAAK,CACjC,CAAA,OAAOG,EAAMK,CAAG,CAAA,CAAE,MAAO,EAC3B,CACA,IAAK,KACL,CAAA,QACE,OAAOL,CAAAA,CAAMH,CAAK,CAAA,CAAE,GAAI,EAC5B,CACF,CArBgBT,CAAAgC,CAAAA,CAAAA,CAAA,cA2BT,CAAA,CAAA,SAASE,CAA6BzB,CAAAA,CAAAA,CAAuB,CAClE,OAAOa,CAAab,CAAAA,CAAK,CAAE,CAAA,GAC7B,CAFgBT,CAAAA,CAAAkC,CAAA,CAAA,8BAAA,CAAA,CAYT,SAASC,CAAAA,CAAsB,CACpC,eAAA,CAAAC,CACA,CAAA,OAAA,CAAAC,CACF,CAAA,CAGY,CACV,IAAMC,CAAIhB,CAAAA,CAAAA,CAAac,CAAe,CAAA,CAChCG,CAAIjB,CAAAA,CAAAA,CAAae,CAAO,CAAA,CAS9B,OAHiBC,CAAAA,CAAE,KAAM,CAAA,QAAA,CAASC,CAAE,CAAA,KAAK,CAGtB,EAAA,GACrB,CAlBgBvC,CAAAA,CAAAmC,CAAA,CAAA,uBAAA,CAAA","file":"index.mjs","sourcesContent":["import Color from 'color';\nimport loMemo from 'lodash/memoize.js';\n// need to leave this imported even though it doesn't look like it's being used for proper typing of `lodash/memoize`\n\nexport type ColorRgbInfo = {\n /** From 0 - 255 */\n r: number;\n /** From 0 - 255 */\n g: number;\n /** From 0 - 255 */\n b: number;\n /** From 0 - 1 */\n alpha?: number;\n};\n\nexport type ColorHslInfo = {\n /** From 0 - 255 */\n h: number;\n /** From 0 - 255 */\n s: number;\n /** From 0 - 255 */\n l: number;\n /** From 0 - 1 */\n alpha?: number;\n};\n\nexport type ColorFormat = 'hex' | 'hsl' | 'rgb';\n\n/**\n * If a number is less than 1, round it to a hundredth (i.e. 0.87), else round to nearest integer.\n */\nfunction roundNumber(value: number): number {\n return value < 1 ? Number(value.toFixed(2)) : Math.round(value);\n}\n\nfunction roundObjectValuesNumber<Obj extends Record<string, number>>(\n obj: Obj,\n): Record<keyof Obj, number> {\n return Object.entries(obj).reduce((cur, [key, value]) => {\n const k = key as keyof Obj;\n cur[k] = roundNumber(value);\n return cur;\n }, {} as Record<keyof Obj, number>);\n}\n\n/**\n * Convert a transparency to the 7th & 8th character for an 8 digit hex color\n * @link https://gist.github.com/lopspower/03fb1cc0ac9f32ef38f4\n */\nfunction colorAlphaToHex(value: number): string {\n let val = Math.max(0, value);\n val = Math.min(val, 255);\n val = Math.ceil(val);\n return (\n '0123456789ABCDEF'.charAt((val - (val % 16)) / 16) +\n '0123456789ABCDEF'.charAt(val % 16)\n );\n}\n\nfunction getColorFormat(color: string): ColorFormat | null {\n if (color.startsWith('#')) return 'hex';\n if (color.startsWith('rgb')) return 'rgb';\n if (color.startsWith('hsl')) return 'hsl';\n return null;\n}\n\nfunction analyzeColorBase(color: string) {\n try {\n const c = Color(color);\n const rgb = c.rgb();\n const rgbInfo = roundObjectValuesNumber(\n rgb.object() as unknown as ColorRgbInfo,\n );\n const isTransparent =\n typeof rgbInfo.alpha === 'number' && rgbInfo.alpha !== 1;\n const rgbString = isTransparent\n ? `rgba(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b}, ${rgbInfo.alpha})`\n : `rgb(${rgbInfo.r}, ${rgbInfo.g}, ${rgbInfo.b})`;\n const hsl = c.hsl();\n const hslInfo = roundObjectValuesNumber(\n hsl.object() as unknown as ColorHslInfo,\n );\n const hslString = isTransparent\n ? `hsla(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%, ${hslInfo.alpha})`\n : `hsl(${hslInfo.h}, ${hslInfo.s}%, ${hslInfo.l}%)`;\n let hex = c.hex();\n if (isTransparent) {\n // 6 => 8 character transparent hex\n hex = `${hex}${colorAlphaToHex(rgbInfo.alpha * 256)}`;\n }\n return {\n original: color,\n format: getColorFormat(color),\n rgbInfo,\n rgbString,\n hslInfo,\n hslString,\n hex,\n isTransparent,\n luminosity: c.luminosity(),\n isDark: c.isDark(),\n isLight: c.isLight(),\n color: c,\n };\n } catch (e) {\n throw new Error(`Could not analyze the color \"${color}\" - ${e.message}`);\n }\n}\n\nexport const analyzeColor = loMemo(analyzeColorBase);\n\n/**\n * Is a valid css color\n */\nexport function isColor(color: string): boolean {\n try {\n analyzeColor(color);\n return true;\n } catch (e) {\n return false;\n }\n}\n\nexport function assertColor(color: string): asserts color is string {\n analyzeColor(color);\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorRgbInfo(color: string): ColorRgbInfo {\n return analyzeColor(color).rgbInfo;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorHslInfo(color: string): ColorHslInfo {\n return analyzeColor(color).hslInfo;\n}\n\n/**\n * Has Opacity\n * Returns true if a color has opacity\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function hasOpacity(color: string): boolean {\n return !analyzeColor(color).isTransparent;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function getColorLuminosity(color: string): number {\n return analyzeColor(color).luminosity;\n}\n\n/**\n * Color Contrast\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n * @param color - color value to check. supports any number of formats (hex, rgb, hsl, etc)\n * @returns {boolean} - true if the color is \"dark\" (ie. you should use a lighter color on top) and false if the color is light.\n */\nexport function isDarkColor(color: string): boolean {\n return analyzeColor(color).isDark;\n}\n\n/**\n * Color Contrast\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n * @param color - color value to check. supports any number of formats (hex, rgb, hsl, etc)\n * @returns {boolean} - true if the color is \"light\" (ie. you should use a darker color on top) and false if the color is dark.\n */\nexport function isLightColor(color: string): boolean {\n return analyzeColor(color).isLight;\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function convertColor({\n color,\n format,\n}: {\n color: string;\n format: ColorFormat;\n}): string {\n assertColor(color);\n switch (format) {\n case 'rgb': {\n const rgb = getColorRgbInfo(color);\n return Color(rgb).string();\n }\n case 'hsl': {\n const hsl = getColorHslInfo(color);\n return Color(hsl).string();\n }\n case 'hex':\n default:\n return Color(color).hex();\n }\n}\n\n/**\n * @deprecated - use `analyzeColor`\n * @see analyzeColor\n */\nexport function convertTransparentColorToHex(color: string): string {\n return analyzeColor(color).hex;\n}\n\nconst toPercent = (n: number): string => `${n * 100}%`;\n\n/**\n * Contrast checking.\n *\n * IMPORTANT: CANNOT USE ALPHA CHANNELS IN CONTRAST CALCULATIONS.\n * This can only compare 2 SOLID colors.\n */\nexport function isColorContrastEnough({\n foregroundColor,\n bgColor,\n}: {\n foregroundColor: string;\n bgColor: string;\n}): boolean {\n const f = analyzeColor(foregroundColor);\n const b = analyzeColor(bgColor);\n\n // https://www.w3.org/TR/WCAG20/#contrast-ratiodef\n // (L1 + 0.05) / (L2 + 0.05), where\n // L1 is the relative luminance of the lighter of the colors, and\n // L2 is the relative luminance of the darker of the colors.\n const contrast = f.color.contrast(b.color);\n\n // https://webaim.org/resources/contrastchecker/\n return contrast >= 4.5;\n}\n"]}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@knapsack/color-utils",
|
3
3
|
"description": "",
|
4
|
-
"version": "4.69.15--canary.
|
4
|
+
"version": "4.69.15--canary.4771.9877e61.0",
|
5
5
|
"source": "src/index.ts",
|
6
6
|
"main": "dist/index.js",
|
7
7
|
"module": "dist/index.mjs",
|
@@ -29,9 +29,9 @@
|
|
29
29
|
"lodash": "^4.17.21"
|
30
30
|
},
|
31
31
|
"devDependencies": {
|
32
|
-
"@knapsack/eslint-config-starter": "4.69.15--canary.
|
33
|
-
"@knapsack/test-ava": "4.69.15--canary.
|
34
|
-
"@knapsack/typescript-config-starter": "4.69.15--canary.
|
32
|
+
"@knapsack/eslint-config-starter": "4.69.15--canary.4771.9877e61.0",
|
33
|
+
"@knapsack/test-ava": "4.69.15--canary.4771.9877e61.0",
|
34
|
+
"@knapsack/typescript-config-starter": "4.69.15--canary.4771.9877e61.0",
|
35
35
|
"@types/color": "^3.0.6",
|
36
36
|
"ava": "^6.1.3",
|
37
37
|
"eslint": "^8.57.0",
|
@@ -46,5 +46,5 @@
|
|
46
46
|
"directory": "libs/color-utils",
|
47
47
|
"type": "git"
|
48
48
|
},
|
49
|
-
"gitHead": "
|
49
|
+
"gitHead": "9877e612f2b63bc8dba31726acd0c019c1b57457"
|
50
50
|
}
|