@instructure/ui-color-utils 10.5.1-snapshot-4 → 10.5.1-snapshot-7

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/CHANGELOG.md CHANGED
@@ -3,9 +3,12 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ## [10.5.1-snapshot-4](https://github.com/instructure/instructure-ui/compare/v10.5.0...v10.5.1-snapshot-4) (2024-11-14)
6
+ ## [10.5.1-snapshot-7](https://github.com/instructure/instructure-ui/compare/v10.5.0...v10.5.1-snapshot-7) (2024-11-18)
7
7
 
8
- **Note:** Version bump only for package @instructure/ui-color-utils
8
+
9
+ ### Features
10
+
11
+ * **ui-color-picker,ui-color-utils:** add callback for contrast validation information and export validation methods ([e756c7d](https://github.com/instructure/instructure-ui/commit/e756c7dde20158e82483a4541e916ee98a7a93ec))
9
12
 
10
13
 
11
14
 
@@ -0,0 +1,53 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { colorToRGB, colorToHex8 } from './conversions';
26
+ import { overlayColors } from './overlayColors';
27
+ import { contrast } from './contrast';
28
+
29
+ /**
30
+ * ---
31
+ * category: utilities
32
+ * ---
33
+ * Calculates two, not necesseraly opaque color's contrast on top of each other.
34
+ * The method assumes that the bottom color is on top of a white background (only important if it isn't opaque)
35
+ * @module contrastWithAlpha
36
+ * @param {String} color1
37
+ * @param {String} color2
38
+ * @param {Number} decimalPlaces
39
+ * @returns {Number} color contrast ratio
40
+ */
41
+ const contrastWithAlpha = (color1, color2) => {
42
+ const c1RGBA = colorToRGB(color1);
43
+ const c2RGBA = colorToRGB(color2);
44
+ const c1OnWhite = overlayColors({
45
+ r: 255,
46
+ g: 255,
47
+ b: 255,
48
+ a: 1
49
+ }, c1RGBA);
50
+ const c2OnC1OnWhite = overlayColors(c1OnWhite, c2RGBA);
51
+ return contrast(colorToHex8(c1OnWhite), colorToHex8(c2OnC1OnWhite), 2);
52
+ };
53
+ export { contrastWithAlpha };
package/es/index.js CHANGED
@@ -27,6 +27,9 @@ export { darken } from './darken';
27
27
  export { lighten } from './lighten';
28
28
  export { contrast } from './contrast';
29
29
  export { isValid } from './isValid';
30
+ export { overlayColors } from './overlayColors';
31
+ export { contrastWithAlpha } from './contrastWithAlpha';
32
+ export { validateContrast } from './validateContrast';
30
33
  export { color2hex, colorToHex8, colorToHsva, colorToHsla, colorToRGB } from './conversions';
31
34
  import { color2hex, colorToHex8, colorToHsva, colorToHsla, colorToRGB } from './conversions';
32
35
 
@@ -0,0 +1,55 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ /**
26
+ * @typedef {Object} RGBAResult
27
+ * @property {Number} r - Red component as a string
28
+ * @property {Number} g - Green component as a string
29
+ * @property {Number} b - Blue component as a string
30
+ * @property {Number} a - Alpha component as a string
31
+ */
32
+
33
+ /**
34
+ * ---
35
+ * category: utilities
36
+ * ---
37
+ * Place two RGBA colors on top of each other. The second one (c2) goes on top.
38
+ * The method calculates what color would be visible. If the second color (c2) is opaque, the result
39
+ * will be c2, if fully transparent, c1. If anything in between, it calculates the real color.
40
+ * Alpha is always set to 1 after the calculation
41
+ * @module overlayColors
42
+ * @param {RGBAType} c1
43
+ * @param {RGBAType} c2
44
+ * @returns {RGBAType} color as rgb string
45
+ */
46
+ const overlayColors = (c1, c2) => {
47
+ const alpha = 1 - (1 - c1.a) * (1 - c2.a);
48
+ return {
49
+ r: c2.r * c2.a / alpha + c1.r * c1.a * (1 - c2.a) / alpha,
50
+ g: c2.g * c2.a / alpha + c1.g * c1.a * (1 - c2.a) / alpha,
51
+ b: c2.b * c2.a / alpha + c1.b * c1.a * (1 - c2.a) / alpha,
52
+ a: 1
53
+ };
54
+ };
55
+ export { overlayColors };
@@ -0,0 +1,67 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ /**
26
+ * @typedef {Object} ValidatedContrasts
27
+ * @property {Boolean} isValidNormalText - Is the contrast high enough for normal text size?
28
+ * @property {Boolean} isValidLargeText - Is the contrast high enough for large text size?
29
+ * @property {Boolean} isValidGraphicsText - Is the contrast high enough for graphics text?
30
+ */
31
+
32
+ /**
33
+ * ---
34
+ * category: utilities
35
+ * ---
36
+ * Decides if the given contrast is sufficient for different text sizes and situations.
37
+ *
38
+ * According to WCAG 2.2
39
+ *
40
+ * AA level (https://www.w3.org/TR/WCAG22/#contrast-minimum)
41
+ *
42
+ * text: 4.5:1
43
+ *
44
+ * large text: 3:1
45
+ *
46
+ * non-text: 3:1 (https://www.w3.org/TR/WCAG22/#non-text-contrast)
47
+ *
48
+ *
49
+ * AAA level (https://www.w3.org/TR/WCAG22/#contrast-enhanced)
50
+ *
51
+ * text: 7:1
52
+ *
53
+ * large text: 4.5:1
54
+ *
55
+ * non-text: 3:1 (https://www.w3.org/TR/WCAG22/#non-text-contrast)
56
+ * @module validateContrast
57
+ * @param {Number} contrast
58
+ * @returns {ValidatedContrasts} validation object
59
+ */
60
+ const validateContrast = (contrast, validationLevel) => {
61
+ return {
62
+ isValidNormalText: contrast >= (validationLevel === 'AAA' ? 7 : 4.5),
63
+ isValidLargeText: contrast >= (validationLevel === 'AAA' ? 4.5 : 3),
64
+ isValidGraphicsText: contrast >= 3
65
+ };
66
+ };
67
+ export { validateContrast };
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.contrastWithAlpha = void 0;
7
+ var _conversions = require("./conversions");
8
+ var _overlayColors = require("./overlayColors");
9
+ var _contrast = require("./contrast");
10
+ /*
11
+ * The MIT License (MIT)
12
+ *
13
+ * Copyright (c) 2015 - present Instructure, Inc.
14
+ *
15
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ * of this software and associated documentation files (the "Software"), to deal
17
+ * in the Software without restriction, including without limitation the rights
18
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ * copies of the Software, and to permit persons to whom the Software is
20
+ * furnished to do so, subject to the following conditions:
21
+ *
22
+ * The above copyright notice and this permission notice shall be included in all
23
+ * copies or substantial portions of the Software.
24
+ *
25
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
+ * SOFTWARE.
32
+ */
33
+
34
+ /**
35
+ * ---
36
+ * category: utilities
37
+ * ---
38
+ * Calculates two, not necesseraly opaque color's contrast on top of each other.
39
+ * The method assumes that the bottom color is on top of a white background (only important if it isn't opaque)
40
+ * @module contrastWithAlpha
41
+ * @param {String} color1
42
+ * @param {String} color2
43
+ * @param {Number} decimalPlaces
44
+ * @returns {Number} color contrast ratio
45
+ */
46
+ const contrastWithAlpha = (color1, color2) => {
47
+ const c1RGBA = (0, _conversions.colorToRGB)(color1);
48
+ const c2RGBA = (0, _conversions.colorToRGB)(color2);
49
+ const c1OnWhite = (0, _overlayColors.overlayColors)({
50
+ r: 255,
51
+ g: 255,
52
+ b: 255,
53
+ a: 1
54
+ }, c1RGBA);
55
+ const c2OnC1OnWhite = (0, _overlayColors.overlayColors)(c1OnWhite, c2RGBA);
56
+ return (0, _contrast.contrast)((0, _conversions.colorToHex8)(c1OnWhite), (0, _conversions.colorToHex8)(c2OnC1OnWhite), 2);
57
+ };
58
+ exports.contrastWithAlpha = contrastWithAlpha;
package/lib/index.js CHANGED
@@ -45,6 +45,12 @@ Object.defineProperty(exports, "contrast", {
45
45
  return _contrast.contrast;
46
46
  }
47
47
  });
48
+ Object.defineProperty(exports, "contrastWithAlpha", {
49
+ enumerable: true,
50
+ get: function () {
51
+ return _contrastWithAlpha.contrastWithAlpha;
52
+ }
53
+ });
48
54
  Object.defineProperty(exports, "darken", {
49
55
  enumerable: true,
50
56
  get: function () {
@@ -64,11 +70,26 @@ Object.defineProperty(exports, "lighten", {
64
70
  return _lighten.lighten;
65
71
  }
66
72
  });
73
+ Object.defineProperty(exports, "overlayColors", {
74
+ enumerable: true,
75
+ get: function () {
76
+ return _overlayColors.overlayColors;
77
+ }
78
+ });
79
+ Object.defineProperty(exports, "validateContrast", {
80
+ enumerable: true,
81
+ get: function () {
82
+ return _validateContrast.validateContrast;
83
+ }
84
+ });
67
85
  var _alpha = require("./alpha");
68
86
  var _darken = require("./darken");
69
87
  var _lighten = require("./lighten");
70
88
  var _contrast = require("./contrast");
71
89
  var _isValid = require("./isValid");
90
+ var _overlayColors = require("./overlayColors");
91
+ var _contrastWithAlpha = require("./contrastWithAlpha");
92
+ var _validateContrast = require("./validateContrast");
72
93
  var _conversions = require("./conversions");
73
94
  /*
74
95
  * The MIT License (MIT)
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.overlayColors = void 0;
7
+ /*
8
+ * The MIT License (MIT)
9
+ *
10
+ * Copyright (c) 2015 - present Instructure, Inc.
11
+ *
12
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ * of this software and associated documentation files (the "Software"), to deal
14
+ * in the Software without restriction, including without limitation the rights
15
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ * copies of the Software, and to permit persons to whom the Software is
17
+ * furnished to do so, subject to the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be included in all
20
+ * copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ * SOFTWARE.
29
+ */
30
+
31
+ /**
32
+ * @typedef {Object} RGBAResult
33
+ * @property {Number} r - Red component as a string
34
+ * @property {Number} g - Green component as a string
35
+ * @property {Number} b - Blue component as a string
36
+ * @property {Number} a - Alpha component as a string
37
+ */
38
+
39
+ /**
40
+ * ---
41
+ * category: utilities
42
+ * ---
43
+ * Place two RGBA colors on top of each other. The second one (c2) goes on top.
44
+ * The method calculates what color would be visible. If the second color (c2) is opaque, the result
45
+ * will be c2, if fully transparent, c1. If anything in between, it calculates the real color.
46
+ * Alpha is always set to 1 after the calculation
47
+ * @module overlayColors
48
+ * @param {RGBAType} c1
49
+ * @param {RGBAType} c2
50
+ * @returns {RGBAType} color as rgb string
51
+ */
52
+ const overlayColors = (c1, c2) => {
53
+ const alpha = 1 - (1 - c1.a) * (1 - c2.a);
54
+ return {
55
+ r: c2.r * c2.a / alpha + c1.r * c1.a * (1 - c2.a) / alpha,
56
+ g: c2.g * c2.a / alpha + c1.g * c1.a * (1 - c2.a) / alpha,
57
+ b: c2.b * c2.a / alpha + c1.b * c1.a * (1 - c2.a) / alpha,
58
+ a: 1
59
+ };
60
+ };
61
+ exports.overlayColors = overlayColors;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.validateContrast = void 0;
7
+ /*
8
+ * The MIT License (MIT)
9
+ *
10
+ * Copyright (c) 2015 - present Instructure, Inc.
11
+ *
12
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ * of this software and associated documentation files (the "Software"), to deal
14
+ * in the Software without restriction, including without limitation the rights
15
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ * copies of the Software, and to permit persons to whom the Software is
17
+ * furnished to do so, subject to the following conditions:
18
+ *
19
+ * The above copyright notice and this permission notice shall be included in all
20
+ * copies or substantial portions of the Software.
21
+ *
22
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ * SOFTWARE.
29
+ */
30
+
31
+ /**
32
+ * @typedef {Object} ValidatedContrasts
33
+ * @property {Boolean} isValidNormalText - Is the contrast high enough for normal text size?
34
+ * @property {Boolean} isValidLargeText - Is the contrast high enough for large text size?
35
+ * @property {Boolean} isValidGraphicsText - Is the contrast high enough for graphics text?
36
+ */
37
+
38
+ /**
39
+ * ---
40
+ * category: utilities
41
+ * ---
42
+ * Decides if the given contrast is sufficient for different text sizes and situations.
43
+ *
44
+ * According to WCAG 2.2
45
+ *
46
+ * AA level (https://www.w3.org/TR/WCAG22/#contrast-minimum)
47
+ *
48
+ * text: 4.5:1
49
+ *
50
+ * large text: 3:1
51
+ *
52
+ * non-text: 3:1 (https://www.w3.org/TR/WCAG22/#non-text-contrast)
53
+ *
54
+ *
55
+ * AAA level (https://www.w3.org/TR/WCAG22/#contrast-enhanced)
56
+ *
57
+ * text: 7:1
58
+ *
59
+ * large text: 4.5:1
60
+ *
61
+ * non-text: 3:1 (https://www.w3.org/TR/WCAG22/#non-text-contrast)
62
+ * @module validateContrast
63
+ * @param {Number} contrast
64
+ * @returns {ValidatedContrasts} validation object
65
+ */
66
+ const validateContrast = (contrast, validationLevel) => {
67
+ return {
68
+ isValidNormalText: contrast >= (validationLevel === 'AAA' ? 7 : 4.5),
69
+ isValidLargeText: contrast >= (validationLevel === 'AAA' ? 4.5 : 3),
70
+ isValidGraphicsText: contrast >= 3
71
+ };
72
+ };
73
+ exports.validateContrast = validateContrast;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instructure/ui-color-utils",
3
- "version": "10.5.1-snapshot-4",
3
+ "version": "10.5.1-snapshot-7",
4
4
  "description": "A color utility library made by Instructure Inc.",
5
5
  "author": "Instructure, Inc. Engineering and Product Design",
6
6
  "module": "./es/index.js",
@@ -22,8 +22,8 @@
22
22
  },
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
- "@instructure/ui-babel-preset": "10.5.1-snapshot-4",
26
- "@instructure/ui-test-utils": "10.5.1-snapshot-4",
25
+ "@instructure/ui-babel-preset": "10.5.1-snapshot-7",
26
+ "@instructure/ui-test-utils": "10.5.1-snapshot-7",
27
27
  "@types/tinycolor2": "^1.4.6"
28
28
  },
29
29
  "dependencies": {
@@ -0,0 +1,50 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { colorToRGB, colorToHex8 } from './conversions'
26
+ import { overlayColors } from './overlayColors'
27
+ import { contrast } from './contrast'
28
+
29
+ /**
30
+ * ---
31
+ * category: utilities
32
+ * ---
33
+ * Calculates two, not necesseraly opaque color's contrast on top of each other.
34
+ * The method assumes that the bottom color is on top of a white background (only important if it isn't opaque)
35
+ * @module contrastWithAlpha
36
+ * @param {String} color1
37
+ * @param {String} color2
38
+ * @param {Number} decimalPlaces
39
+ * @returns {Number} color contrast ratio
40
+ */
41
+ const contrastWithAlpha = (color1: string, color2: string): number => {
42
+ const c1RGBA = colorToRGB(color1)
43
+ const c2RGBA = colorToRGB(color2)
44
+ const c1OnWhite = overlayColors({ r: 255, g: 255, b: 255, a: 1 }, c1RGBA)
45
+ const c2OnC1OnWhite = overlayColors(c1OnWhite, c2RGBA)
46
+
47
+ return contrast(colorToHex8(c1OnWhite), colorToHex8(c2OnC1OnWhite), 2)
48
+ }
49
+
50
+ export { contrastWithAlpha }
package/src/index.ts CHANGED
@@ -27,6 +27,9 @@ export { darken } from './darken'
27
27
  export { lighten } from './lighten'
28
28
  export { contrast } from './contrast'
29
29
  export { isValid } from './isValid'
30
+ export { overlayColors } from './overlayColors'
31
+ export { contrastWithAlpha } from './contrastWithAlpha'
32
+ export { validateContrast } from './validateContrast'
30
33
  export {
31
34
  color2hex,
32
35
  colorToHex8,
@@ -0,0 +1,58 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type { RGBAType } from './colorTypes'
26
+
27
+ /**
28
+ * @typedef {Object} RGBAResult
29
+ * @property {Number} r - Red component as a string
30
+ * @property {Number} g - Green component as a string
31
+ * @property {Number} b - Blue component as a string
32
+ * @property {Number} a - Alpha component as a string
33
+ */
34
+
35
+ /**
36
+ * ---
37
+ * category: utilities
38
+ * ---
39
+ * Place two RGBA colors on top of each other. The second one (c2) goes on top.
40
+ * The method calculates what color would be visible. If the second color (c2) is opaque, the result
41
+ * will be c2, if fully transparent, c1. If anything in between, it calculates the real color.
42
+ * Alpha is always set to 1 after the calculation
43
+ * @module overlayColors
44
+ * @param {RGBAType} c1
45
+ * @param {RGBAType} c2
46
+ * @returns {RGBAType} color as rgb string
47
+ */
48
+ const overlayColors = (c1: RGBAType, c2: RGBAType): RGBAType => {
49
+ const alpha = 1 - (1 - c1.a) * (1 - c2.a)
50
+ return {
51
+ r: (c2.r * c2.a) / alpha + (c1.r * c1.a * (1 - c2.a)) / alpha,
52
+ g: (c2.g * c2.a) / alpha + (c1.g * c1.a * (1 - c2.a)) / alpha,
53
+ b: (c2.b * c2.a) / alpha + (c1.b * c1.a * (1 - c2.a)) / alpha,
54
+ a: 1
55
+ }
56
+ }
57
+
58
+ export { overlayColors }
@@ -0,0 +1,77 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ interface ValidatedContrasts {
26
+ isValidNormalText: boolean
27
+ isValidLargeText: boolean
28
+ isValidGraphicsText: boolean
29
+ }
30
+
31
+ /**
32
+ * @typedef {Object} ValidatedContrasts
33
+ * @property {Boolean} isValidNormalText - Is the contrast high enough for normal text size?
34
+ * @property {Boolean} isValidLargeText - Is the contrast high enough for large text size?
35
+ * @property {Boolean} isValidGraphicsText - Is the contrast high enough for graphics text?
36
+ */
37
+
38
+ /**
39
+ * ---
40
+ * category: utilities
41
+ * ---
42
+ * Decides if the given contrast is sufficient for different text sizes and situations.
43
+ *
44
+ * According to WCAG 2.2
45
+ *
46
+ * AA level (https://www.w3.org/TR/WCAG22/#contrast-minimum)
47
+ *
48
+ * text: 4.5:1
49
+ *
50
+ * large text: 3:1
51
+ *
52
+ * non-text: 3:1 (https://www.w3.org/TR/WCAG22/#non-text-contrast)
53
+ *
54
+ *
55
+ * AAA level (https://www.w3.org/TR/WCAG22/#contrast-enhanced)
56
+ *
57
+ * text: 7:1
58
+ *
59
+ * large text: 4.5:1
60
+ *
61
+ * non-text: 3:1 (https://www.w3.org/TR/WCAG22/#non-text-contrast)
62
+ * @module validateContrast
63
+ * @param {Number} contrast
64
+ * @returns {ValidatedContrasts} validation object
65
+ */
66
+ const validateContrast = (
67
+ contrast: number,
68
+ validationLevel?: 'AA' | 'AAA'
69
+ ): ValidatedContrasts => {
70
+ return {
71
+ isValidNormalText: contrast >= (validationLevel === 'AAA' ? 7 : 4.5),
72
+ isValidLargeText: contrast >= (validationLevel === 'AAA' ? 4.5 : 3),
73
+ isValidGraphicsText: contrast >= 3
74
+ }
75
+ }
76
+
77
+ export { validateContrast }