@autoguru/overdrive 4.9.2 → 4.9.3

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.
@@ -1,2 +1,28 @@
1
- export declare const shadedColour: (colour: string, intensity: number | string | null, direction: 'forward' | 'backward', isDarkTheme?: boolean, transparency?: number | string | null) => string;
1
+ interface ShadedColourProps {
2
+ colour: string;
3
+ intensity: number | string | null;
4
+ direction: 'forward' | 'backward';
5
+ isDarkTheme: boolean;
6
+ transparency?: number | string | null;
7
+ }
8
+ export declare const shadedColour: ({ colour, intensity, direction, isDarkTheme, transparency, }: ShadedColourProps) => string;
9
+ declare type RGBNumbers = {
10
+ r: number;
11
+ g: number;
12
+ b: number;
13
+ } | null;
14
+ export declare const hexToRGB: (hex: string) => RGBNumbers;
15
+ export declare const rgbStrToRGB: (rgb: string) => RGBNumbers;
16
+ export declare const getRGBValues: (hexOrRGB: string) => RGBNumbers;
17
+ export declare const getColourLuminance: (rgb: RGBNumbers) => number;
18
+ export declare const getContrastRatio: (colour1: string, colour2: string) => number;
19
+ declare type AccessibilityLevel = 'AA' | 'AAA';
20
+ declare type AccessibilityTextSize = 'SMALL' | 'LARGE';
21
+ export declare const passesAccessibilityContrast: ({ colour1, colour2, level, textSize, }: {
22
+ colour1: string;
23
+ colour2: string;
24
+ level: AccessibilityLevel;
25
+ textSize: AccessibilityTextSize;
26
+ }) => boolean;
27
+ export {};
2
28
  //# sourceMappingURL=helpers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../lib/themes/helpers.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,YAAY,WAChB,MAAM,aACH,MAAM,GAAG,MAAM,GAAG,IAAI,aACtB,SAAS,GAAG,UAAU,wCAEnB,MAAM,GAAG,MAAM,GAAG,IAAI,KAClC,MAiBF,CAAC"}
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../lib/themes/helpers.ts"],"names":[],"mappings":"AAEA,UAAU,iBAAiB;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IAClC,SAAS,EAAE,SAAS,GAAG,UAAU,CAAC;IAClC,WAAW,EAAE,OAAO,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CACtC;AAED,eAAO,MAAM,YAAY,iEAMtB,iBAAiB,KAAG,MAiBtB,CAAC;AAEF,aAAK,UAAU,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC;AAE7D,eAAO,MAAM,QAAQ,QAAS,MAAM,KAAG,UAYtC,CAAC;AAEF,eAAO,MAAM,WAAW,QAAS,MAAM,KAAG,UAOzC,CAAC;AAEF,eAAO,MAAM,YAAY,aAAc,MAAM,KAAG,UACwB,CAAC;AAEzE,eAAO,MAAM,kBAAkB,QAAS,UAAU,WAQjD,CAAC;AAEF,eAAO,MAAM,gBAAgB,YAAa,MAAM,WAAW,MAAM,KAAG,MAMnE,CAAC;AAEF,aAAK,kBAAkB,GAAG,IAAI,GAAG,KAAK,CAAC;AACvC,aAAK,qBAAqB,GAAG,OAAO,GAAG,OAAO,CAAC;AAE/C,eAAO,MAAM,2BAA2B;aAM9B,MAAM;aACN,MAAM;WACR,kBAAkB;cACf,qBAAqB;MAC5B,OAQH,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import { colord } from 'colord';
2
- export const shadedColour = (colour, intensity, direction, isDarkTheme = false, transparency = 0) => {
2
+ export const shadedColour = ({ colour, intensity, direction, isDarkTheme, transparency = 0, }) => {
3
3
  const intensityValue = typeof intensity === 'string' ? Number(intensity) : intensity;
4
4
  const transparencyValue = typeof transparency === 'string' ? Number(transparency) : transparency;
5
5
  return colord(colour)[(!isDarkTheme && direction === 'backward') ||
@@ -9,3 +9,50 @@ export const shadedColour = (colour, intensity, direction, isDarkTheme = false,
9
9
  .alpha(typeof transparencyValue === 'number' ? 1 - transparencyValue : 1)
10
10
  .toHex();
11
11
  };
12
+ export const hexToRGB = (hex) => {
13
+ const shorthandRegex = /^#?([\da-f])([\da-f])([\da-f])$/i;
14
+ hex = hex.replace(shorthandRegex, (_, r, g, b) => r + r + g + g + b + b);
15
+ const result = /^#?([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i.exec(hex);
16
+ return result
17
+ ? {
18
+ r: Number.parseInt(result[1], 16),
19
+ g: Number.parseInt(result[2], 16),
20
+ b: Number.parseInt(result[3], 16),
21
+ }
22
+ : null;
23
+ };
24
+ export const rgbStrToRGB = (rgb) => {
25
+ const components = rgb.replace(/[^\d,]/g, '').split(',');
26
+ return {
27
+ r: Number.parseInt(components[0]),
28
+ g: Number.parseInt(components[1]),
29
+ b: Number.parseInt(components[2]),
30
+ };
31
+ };
32
+ export const getRGBValues = (hexOrRGB) => hexOrRGB.startsWith('rgb') ? rgbStrToRGB(hexOrRGB) : hexToRGB(hexOrRGB);
33
+ export const getColourLuminance = (rgb) => {
34
+ if (!rgb)
35
+ return 0;
36
+ const { r, g, b } = rgb;
37
+ const a = [r, g, b].map((v) => {
38
+ v /= 255;
39
+ return v <= 0.039_28 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
40
+ });
41
+ return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
42
+ };
43
+ export const getContrastRatio = (colour1, colour2) => {
44
+ const color1luminance = getColourLuminance(getRGBValues(colour1));
45
+ const color2luminance = getColourLuminance(getRGBValues(colour2));
46
+ return color1luminance > color2luminance
47
+ ? (color2luminance + 0.05) / (color1luminance + 0.05)
48
+ : (color1luminance + 0.05) / (color2luminance + 0.05);
49
+ };
50
+ export const passesAccessibilityContrast = ({ colour1, colour2, level, textSize, }) => {
51
+ const contrastRatio = getContrastRatio(colour1, colour2);
52
+ if (textSize === 'LARGE') {
53
+ return level === 'AAA'
54
+ ? contrastRatio < 1 / 4.5
55
+ : contrastRatio < 1 / 3;
56
+ }
57
+ return level === 'AAA' ? contrastRatio < 1 / 7 : contrastRatio < 1 / 4.5;
58
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autoguru/overdrive",
3
- "version": "4.9.2",
3
+ "version": "4.9.3",
4
4
  "description": "Overdrive is a product component library, and design system for AutoGuru.",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/index.js",