@knapsack/color-utils 4.69.0--canary.4475.32644f6.0 → 4.69.0--canary.4487.690a0ba.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -66,12 +66,9 @@ function colorAlphaToHex(value) {
66
66
  }
67
67
  __name(colorAlphaToHex, "colorAlphaToHex");
68
68
  function getColorFormat(color) {
69
- if (color.startsWith("#"))
70
- return "hex";
71
- if (color.startsWith("rgb"))
72
- return "rgb";
73
- if (color.startsWith("hsl"))
74
- return "hsl";
69
+ if (color.startsWith("#")) return "hex";
70
+ if (color.startsWith("rgb")) return "rgb";
71
+ if (color.startsWith("hsl")) return "hsl";
75
72
  return null;
76
73
  }
77
74
  __name(getColorFormat, "getColorFormat");
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,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,MAAO,EAAE,IACjD,mBAAmB,OAAO,MAAM,EAAE;AAEtC;AARS;AAUT,SAAS,eAAe,OAAmC;AACzD,MAAI,MAAM,WA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C,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"],"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,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,MAAO,EAAE,IACjD,mBAAmB,OAAO,MAAM,EAAE;AAEtC;AARS;AAUT,SAAS,eAAe,OAAmC;AACzD,MAAI,MAAM,WAAW,GAAG,EAAG,QAAO;AAClC,MAAI,MAAM,WAAW,KAAK,EAAG,QAAO;AACpC,MAAI,MAAM,WAAW,KAAK,EAAG,QAAO;AACpC,SAAO;AACT;AALS;AAOT,SAAS,iBAAiB,OAAe;AACvC,MAAI;AACF,UAAM,QAAI,aAAAA,SAAM,KAAK;AACrB,UAAM,MAAM,EAAE,IAAI;AAClB,UAAM,UAAU;AAAA,MACd,IAAI,OAAO;AAAA,IACb;AACA,UAAM,gBACJ,OAAO,QAAQ,UAAU,YAAY,QAAQ,UAAU;AACzD,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,KAAK,EAAE;AAC7B;AAFgB;AAUT,SAAS,WAAW,OAAwB;AACjD,SAAO,CAAC,aAAa,KAAK,EAAE;AAC9B;AAFgB;AAQT,SAAS,mBAAmB,OAAuB;AACxD,SAAO,aAAa,KAAK,EAAE;AAC7B;AAFgB;AAWT,SAAS,YAAY,OAAwB;AAClD,SAAO,aAAa,KAAK,EAAE;AAC7B;AAFgB;AAWT,SAAS,aAAa,OAAwB;AACnD,SAAO,aAAa,KAAK,EAAE;AAC7B;AAFgB;AAQT,SAAS,aAAa;AAAA,EAC3B;AAAA,EACA;AACF,GAGW;AACT,cAAY,KAAK;AACjB,UAAQ,QAAQ;AAAA,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"]}
package/dist/index.mjs CHANGED
@@ -24,12 +24,9 @@ function colorAlphaToHex(value) {
24
24
  }
25
25
  __name(colorAlphaToHex, "colorAlphaToHex");
26
26
  function getColorFormat(color) {
27
- if (color.startsWith("#"))
28
- return "hex";
29
- if (color.startsWith("rgb"))
30
- return "rgb";
31
- if (color.startsWith("hsl"))
32
- return "hsl";
27
+ if (color.startsWith("#")) return "hex";
28
+ if (color.startsWith("rgb")) return "rgb";
29
+ if (color.startsWith("hsl")) return "hsl";
33
30
  return null;
34
31
  }
35
32
  __name(getColorFormat, "getColorFormat");
@@ -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,MAAO,EAAE,IACjD,mBAAmB,OAAO,MAAM,EAAE;AAEtC;AARS;AAUT,SAAS,eAAe,OAAmC;AACzD,MAAI,MAAM,WAAW,GAAG;AAAG,WAAO;AAClC,MAAI,MAAM,WAAW,KAAK;AAAG,WAAO;AACpC,MAAI,MAAM,WAAW,KAAK;AAAG,WAAO;AACpC,SAAO;AACT;AALS;AAOT,SAAS,iBAAiB,OAAe;AACvC,MAAI;AACF,UAAM,IAAI,MAAM,KAAK;AACrB,UAAM,MAAM,EAAE,IAAI;AAClB,UAAM,UAAU;AAAA,MACd,IAAI,OAAO;AAAA,IACb;AACA,UAAM,gBACJ,OAAO,QAAQ,UAAU,YAAY,QAAQ,UAAU;AACzD,UAAM,YAAY,gBACd,QAAQ,QAAQ,CAAC,KAAK,QAAQ,CAAC,KAAK,QAAQ,CAAC,KAAK,QAAQ,KAAK,MAC/D,OAA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mBAAmB,OAAuB;AACxD,SAAO,aAAa,KAAK,EAAE;AAC7B;AAFgB;AAWT,SAAS,YAAY,OAAwB;AAClD,SAAO,aAAa,KAAK,EAAE;AAC7B;AAFgB;AAWT,SAAS,aAAa,OAAwB;AACnD,SAAO,aAAa,KAAK,EAAE;AAC7B;AAFgB;AAQT,SAAS,aAAa;AAAA,EAC3B;AAAA,EACA;AACF,GAGW;AACT,cAAY,KAAK;AACjB,UAAQ,QAAQ;AAAA,IACd,KAAK,OAAO;AACV,YAAM,MAAM,gBAAgB,KAAK;AACjC,aAAO,MAAM,GAAG,EAAE,OAAO;AAAA,IAC3B;AAAA,IACA,KAAK,OAAO;AACV,YAAM,MAAM,gBAAgB,KAAK;AACjC,aAAO,MAAM,GAAG,EAAE,OAAO;AAAA,IAC3B;AAAA,IACA,KAAK;AAAA,IACL;AACE,aAAO,MAAM,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":[]}
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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A,IACL;AACE,aAAO,MAAM,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":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@knapsack/color-utils",
3
3
  "description": "",
4
- "version": "4.69.0--canary.4475.32644f6.0",
4
+ "version": "4.69.0--canary.4487.690a0ba.0",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -29,13 +29,13 @@
29
29
  "lodash": "^4.17.21"
30
30
  },
31
31
  "devDependencies": {
32
- "@knapsack/eslint-config-starter": "4.69.0--canary.4475.32644f6.0",
33
- "@knapsack/test-ava": "4.69.0--canary.4475.32644f6.0",
34
- "@knapsack/typescript-config-starter": "4.69.0--canary.4475.32644f6.0",
32
+ "@knapsack/eslint-config-starter": "4.69.0--canary.4487.690a0ba.0",
33
+ "@knapsack/test-ava": "4.69.0--canary.4487.690a0ba.0",
34
+ "@knapsack/typescript-config-starter": "4.69.0--canary.4487.690a0ba.0",
35
35
  "@types/color": "^3.0.6",
36
36
  "ava": "^6.1.3",
37
37
  "eslint": "^8.57.0",
38
- "tsup": "^8.0.2"
38
+ "tsup": "^8.2.4"
39
39
  },
40
40
  "license": "GPL-2.0-or-later",
41
41
  "publishConfig": {
@@ -46,5 +46,5 @@
46
46
  "directory": "libs/color-utils",
47
47
  "type": "git"
48
48
  },
49
- "gitHead": "32644f6a1a5511e4014e0088633262487e3df856"
49
+ "gitHead": "690a0ba96c24b040e36d6e29da8b2ac3a8bcdb44"
50
50
  }