color-bits 0.1.0 → 1.0.1

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/build/core.js ADDED
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.OFFSET_A = exports.OFFSET_B = exports.OFFSET_G = exports.OFFSET_R = void 0;
27
+ exports.newColor = newColor;
28
+ exports.from = from;
29
+ exports.toNumber = toNumber;
30
+ exports.getRed = getRed;
31
+ exports.getGreen = getGreen;
32
+ exports.getBlue = getBlue;
33
+ exports.getAlpha = getAlpha;
34
+ exports.setRed = setRed;
35
+ exports.setGreen = setGreen;
36
+ exports.setBlue = setBlue;
37
+ exports.setAlpha = setAlpha;
38
+ const bit = __importStar(require("./bit"));
39
+ const { cast, get, set } = bit;
40
+ exports.OFFSET_R = 24;
41
+ exports.OFFSET_G = 16;
42
+ exports.OFFSET_B = 8;
43
+ exports.OFFSET_A = 0;
44
+ /**
45
+ * Creates a new color from the given RGBA components.
46
+ * Every component should be in the [0, 255] range.
47
+ */
48
+ function newColor(r, g, b, a) {
49
+ return ((r << exports.OFFSET_R) +
50
+ (g << exports.OFFSET_G) +
51
+ (b << exports.OFFSET_B) +
52
+ (a << exports.OFFSET_A));
53
+ }
54
+ /**
55
+ * Creates a new color from the given number value, e.g. 0x599eff.
56
+ */
57
+ function from(color) {
58
+ return newColor(get(color, exports.OFFSET_R), get(color, exports.OFFSET_G), get(color, exports.OFFSET_B), get(color, exports.OFFSET_A));
59
+ }
60
+ /**
61
+ * Turns the color into its equivalent number representation.
62
+ * This is essentially a cast from int32 to uint32.
63
+ */
64
+ function toNumber(color) {
65
+ return cast(color);
66
+ }
67
+ function getRed(c) { return get(c, exports.OFFSET_R); }
68
+ function getGreen(c) { return get(c, exports.OFFSET_G); }
69
+ function getBlue(c) { return get(c, exports.OFFSET_B); }
70
+ function getAlpha(c) { return get(c, exports.OFFSET_A); }
71
+ function setRed(c, value) { return set(c, exports.OFFSET_R, value); }
72
+ function setGreen(c, value) { return set(c, exports.OFFSET_G, value); }
73
+ function setBlue(c, value) { return set(c, exports.OFFSET_B, value); }
74
+ function setAlpha(c, value) { return set(c, exports.OFFSET_A, value); }
75
+ //# sourceMappingURL=core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAeA,4BAOC;AAKD,oBAOC;AAMD,4BAEC;AAED,wBAA6D;AAC7D,4BAA+D;AAC/D,0BAA8D;AAC9D,4BAA+D;AAE/D,wBAAmF;AACnF,4BAAqF;AACrF,0BAAoF;AACpF,4BAAqF;AApDrF,2CAA4B;AAE5B,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,GAAG,CAAA;AAIjB,QAAA,QAAQ,GAAG,EAAE,CAAC;AACd,QAAA,QAAQ,GA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C,CAAC,CAAC,CAAC;AACnF,SAAgB,QAAQ,CAAC,CAAQ,EAAE,KAAa,IAAI,OAAO,GAAG,CAAC,CAAC,EAAE,gBAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;AACrF,SAAgB,OAAO,CAAC,CAAQ,EAAE,KAAa,IAAI,OAAO,GAAG,CAAC,CAAC,EAAE,gBAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;AACpF,SAAgB,QAAQ,CAAC,CAAQ,EAAE,KAAa,IAAI,OAAO,GAAG,CAAC,CAAC,EAAE,gBAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,24 @@
1
+ import type { Color } from './core';
2
+ /** Format to a #RRGGBBAA string */
3
+ export declare function format(color: Color): string;
4
+ export declare function formatRGBA(color: Color): string;
5
+ export declare function toRGBA(color: Color): {
6
+ r: number;
7
+ g: number;
8
+ b: number;
9
+ a: number;
10
+ };
11
+ export declare function formatHSLA(color: Color): string;
12
+ export declare function toHSLA(color: Color): {
13
+ h: number;
14
+ s: number;
15
+ l: number;
16
+ a: number;
17
+ };
18
+ export declare function formatHWBA(color: Color): string;
19
+ export declare function toHWBA(color: Color): {
20
+ h: number;
21
+ w: number;
22
+ b: number;
23
+ a: number;
24
+ };
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.format = format;
27
+ exports.formatRGBA = formatRGBA;
28
+ exports.toRGBA = toRGBA;
29
+ exports.formatHSLA = formatHSLA;
30
+ exports.toHSLA = toHSLA;
31
+ exports.formatHWBA = formatHWBA;
32
+ exports.toHWBA = toHWBA;
33
+ const core = __importStar(require("./core"));
34
+ const { getRed, getGreen, getBlue, getAlpha } = core;
35
+ // Return buffer, avoid allocations
36
+ const buffer = [0, 0, 0];
37
+ /**
38
+ * Map 8-bits value to its hexadecimal representation
39
+ * ['00', '01', '02', ..., 'fe', 'ff']
40
+ */
41
+ const FORMAT_HEX = Array.from({ length: 256 })
42
+ .map((_, byte) => byte.toString(16).padStart(2, '0'));
43
+ /** Format to a #RRGGBBAA string */
44
+ function format(color) {
45
+ return ('#' +
46
+ FORMAT_HEX[getRed(color)] +
47
+ FORMAT_HEX[getGreen(color)] +
48
+ FORMAT_HEX[getBlue(color)] +
49
+ FORMAT_HEX[getAlpha(color)]);
50
+ }
51
+ function formatRGBA(color) {
52
+ return `rgba(${getRed(color)} ${getGreen(color)} ${getBlue(color)} / ${getAlpha(color) / 255})`;
53
+ }
54
+ function toRGBA(color) {
55
+ return {
56
+ r: getRed(color),
57
+ g: getGreen(color),
58
+ b: getBlue(color),
59
+ a: getAlpha(color),
60
+ };
61
+ }
62
+ function formatHSLA(color) {
63
+ rgbToHSL(getRed(color), getGreen(color), getBlue(color));
64
+ const h = buffer[0];
65
+ const s = buffer[1];
66
+ const l = buffer[2];
67
+ const a = getAlpha(color) / 255;
68
+ return `hsla(${h} ${s}% ${l}% / ${a})`;
69
+ }
70
+ function toHSLA(color) {
71
+ rgbToHSL(getRed(color), getGreen(color), getBlue(color));
72
+ const h = buffer[0];
73
+ const s = buffer[1];
74
+ const l = buffer[2];
75
+ const a = getAlpha(color) / 255;
76
+ return { h, s, l, a };
77
+ }
78
+ function formatHWBA(color) {
79
+ rgbToHWB(getRed(color), getGreen(color), getBlue(color));
80
+ const h = buffer[0];
81
+ const w = buffer[1];
82
+ const b = buffer[2];
83
+ const a = getAlpha(color) / 255;
84
+ return `hsla(${h} ${w}% ${b}% / ${a})`;
85
+ }
86
+ function toHWBA(color) {
87
+ rgbToHWB(getRed(color), getGreen(color), getBlue(color));
88
+ const h = buffer[0];
89
+ const w = buffer[1];
90
+ const b = buffer[2];
91
+ const a = getAlpha(color) / 255;
92
+ return { h, w, b, a };
93
+ }
94
+ // Conversion functions
95
+ // https://www.30secondsofcode.org/js/s/rgb-hex-hsl-hsb-color-format-conversion/
96
+ function rgbToHSL(r, g, b) {
97
+ r /= 255;
98
+ g /= 255;
99
+ b /= 255;
100
+ const l = Math.max(r, g, b);
101
+ const s = l - Math.min(r, g, b);
102
+ const h = s
103
+ ? l === r
104
+ ? (g - b) / s
105
+ : l === g
106
+ ? 2 + (b - r) / s
107
+ : 4 + (r - g) / s
108
+ : 0;
109
+ buffer[0] = 60 * h < 0 ? 60 * h + 360 : 60 * h;
110
+ buffer[1] = 100 * (s ? (l <= 0.5 ? s / (2 * l - s) : s / (2 - (2 * l - s))) : 0);
111
+ buffer[2] = (100 * (2 * l - s)) / 2;
112
+ }
113
+ // https://stackoverflow.com/a/29463581/3112706
114
+ function rgbToHWB(r, g, b) {
115
+ r /= 255;
116
+ g /= 255;
117
+ b /= 255;
118
+ const w = Math.min(r, g, b);
119
+ const v = Math.max(r, g, b);
120
+ const black = 1 - v;
121
+ if (v === w) {
122
+ buffer[0] = 0;
123
+ buffer[1] = w;
124
+ buffer[2] = black;
125
+ return;
126
+ }
127
+ let f = r === w ? g - b : (g === w ? b - r : r - g);
128
+ let i = r === w ? 3 : (g === w ? 5 : 1);
129
+ buffer[0] = (i - f / (v - w)) / 6;
130
+ buffer[1] = w;
131
+ buffer[2] = black;
132
+ }
133
+ //# sourceMappingURL=format.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"format.js","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAiBA,wBAQC;AAED,gCAEC;AAED,wBAOC;AAED,gCAYC;AAED,wBAYC;AAED,gCAYC;AAED,wBAYC;AA7FD,6CAA8B;AAE9B,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAA;AAEpD,mCAAmC;AACnC,MAAM,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;AAExB;;;GAGG;AACH,MAAM,UAAU,GACd,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;KACxB,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAA;AAEzD,mCAAmC;AACnC,SAAgB,MAAM,CAAC,KAAY;IACjC,OAAO,CACL,GAAG;QACH,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACzB,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAC3B,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC1B,UAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAC5B,CAAA;AACH,CAAC;AAED,SAAgB,UAAU,CAAC,KAAY;IACrC,OAAO,QAAQ,MAAM,CAAC,KAAK,CAAC,IAAI,QAAQ,CAAC,KAAK,CAAC,IAAI,OAAO,CAAC,KAAK,CAAC,MAAM,QAAQ,CAAC,KAAK,CAAC,GAAG,GA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AAE,CAAC,EAAE,CAAC,CAAC,CAAA;IAC3B,MAAM,KAAK,GAAG,CAAC,GAAG,CAAC,CAAA;IAEnB,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACZ,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;QACb,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;QACb,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,CAAA;QACjB,OAAM;IACR,CAAC;IAED,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACpD,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAExC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;IACjC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;IACb,MAAM,CAAC,CAAC,CAAC,GAAG,KAAK,CAAA;AACnB,CAAC"}
@@ -0,0 +1,33 @@
1
+ import type { Color } from './core';
2
+ /**
3
+ * Modifies color alpha channel.
4
+ * @param color - Color
5
+ * @param value - Value in the range [0, 1]
6
+ */
7
+ export declare function alpha(color: Color, value: number): Color;
8
+ /**
9
+ * Darkens a color.
10
+ * @param color - Color
11
+ * @param coefficient - Multiplier in the range [0, 1]
12
+ */
13
+ export declare function darken(color: Color, coefficient: number): Color;
14
+ /**
15
+ * Lighten a color.
16
+ * @param color - Color
17
+ * @param coefficient - Multiplier in the range [0, 1]
18
+ */
19
+ export declare function lighten(color: Color, coefficient: number): Color;
20
+ /**
21
+ * Blend (aka mix) two colors together.
22
+ * @param background The background color
23
+ * @param overlay The overlay color that is affected by @opacity
24
+ * @param opacity Opacity (alpha) for @overlay
25
+ * @param [gamma=1.0] Gamma correction coefficient. `1.0` to match browser behavior, `2.2` for gamma-corrected blending.
26
+ */
27
+ export declare function blend(background: Color, overlay: Color, opacity: number, gamma?: number): number;
28
+ /**
29
+ * The relative brightness of any point in a color space, normalized to 0 for
30
+ * darkest black and 1 for lightest white.
31
+ * @returns The relative brightness of the color in the range 0 - 1, with 3 digits precision
32
+ */
33
+ export declare function getLuminance(color: Color): number;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.alpha = alpha;
27
+ exports.darken = darken;
28
+ exports.lighten = lighten;
29
+ exports.blend = blend;
30
+ exports.getLuminance = getLuminance;
31
+ const core = __importStar(require("./core"));
32
+ const { getRed, getGreen, getBlue, getAlpha, setAlpha, newColor } = core;
33
+ /**
34
+ * Modifies color alpha channel.
35
+ * @param color - Color
36
+ * @param value - Value in the range [0, 1]
37
+ */
38
+ function alpha(color, value) {
39
+ return setAlpha(color, Math.round(value * 255));
40
+ }
41
+ /**
42
+ * Darkens a color.
43
+ * @param color - Color
44
+ * @param coefficient - Multiplier in the range [0, 1]
45
+ */
46
+ function darken(color, coefficient) {
47
+ const r = getRed(color);
48
+ const g = getGreen(color);
49
+ const b = getBlue(color);
50
+ const a = getAlpha(color);
51
+ const factor = 1 - coefficient;
52
+ return newColor(r * factor, g * factor, b * factor, a);
53
+ }
54
+ /**
55
+ * Lighten a color.
56
+ * @param color - Color
57
+ * @param coefficient - Multiplier in the range [0, 1]
58
+ */
59
+ function lighten(color, coefficient) {
60
+ const r = getRed(color);
61
+ const g = getGreen(color);
62
+ const b = getBlue(color);
63
+ const a = getAlpha(color);
64
+ return newColor(r + (255 - r) * coefficient, g + (255 - g) * coefficient, b + (255 - b) * coefficient, a);
65
+ }
66
+ /**
67
+ * Blend (aka mix) two colors together.
68
+ * @param background The background color
69
+ * @param overlay The overlay color that is affected by @opacity
70
+ * @param opacity Opacity (alpha) for @overlay
71
+ * @param [gamma=1.0] Gamma correction coefficient. `1.0` to match browser behavior, `2.2` for gamma-corrected blending.
72
+ */
73
+ function blend(background, overlay, opacity, gamma = 1.0) {
74
+ const blendChannel = (b, o) => Math.round((b ** (1 / gamma) * (1 - opacity) + o ** (1 / gamma) * opacity) ** gamma);
75
+ const r = blendChannel(getRed(background), getRed(overlay));
76
+ const g = blendChannel(getGreen(background), getGreen(overlay));
77
+ const b = blendChannel(getBlue(background), getBlue(overlay));
78
+ return newColor(r, g, b, 255);
79
+ }
80
+ /**
81
+ * The relative brightness of any point in a color space, normalized to 0 for
82
+ * darkest black and 1 for lightest white.
83
+ * @returns The relative brightness of the color in the range 0 - 1, with 3 digits precision
84
+ */
85
+ function getLuminance(color) {
86
+ const r = getRed(color) / 255;
87
+ const g = getGreen(color) / 255;
88
+ const b = getBlue(color) / 255;
89
+ const apply = (v) => v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
90
+ const r1 = apply(r);
91
+ const g1 = apply(g);
92
+ const b1 = apply(b);
93
+ return Math.round((0.2126 * r1 + 0.7152 * g1 + 0.0722 * b1) * 1000) / 1000;
94
+ }
95
+ //# sourceMappingURL=functions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"functions.js","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAUA,sBAEC;AAOD,wBAcC;AAOD,0BAYC;AASD,sBASC;AAOD,oCAYC;AAxFD,6CAA8B;AAE9B,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAA;AAExE;;;;GAIG;AACH,SAAgB,KAAK,CAAC,KAAY,EAAE,KAAa;IAC/C,OAAO,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC,CAAA;AACjD,CAAC;AAED;;;;GAIG;AACH,SAAgB,MAAM,CAAC,KAAY,EAAE,WAAmB;IACtD,MAAM,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAA;IACvB,MAAM,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IACzB,MAAM,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,CAAA;IACxB,MAAM,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IAEzB,MAAM,MAAM,GAAG,CAAC,GAAG,WAAW,CAAA;IAE9B,OAAO,QAAQ,CACb,CAAC,GAAG,MAAM,EACV,CAAC,GAAG,MAAM,EACV,CAAC,GAAG,MAAM,EACV,CAAC,CACF,CAAA;AACH,CAAC;AAED;;;;GAIG;AACH,SAAgB,OAAO,CAAC,KAAY,EAAE,W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AED;;;;GAIG;AACH,SAAgB,YAAY,CAAC,KAAY;IACvC,MAAM,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,GAAG,CAAA;IAC7B,MAAM,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,GAAG,CAAA;IAC/B,MAAM,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,GAAG,CAAA;IAE9B,MAAM,KAAK,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC,IAAI,GAAG,CAAA;IAEpF,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;IACnB,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;IACnB,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;IAEnB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,GAAG,EAAE,GAAG,MAAM,GAAG,EAAE,GAAG,MAAM,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAA;AAC5E,CAAC"}
@@ -1,4 +1,4 @@
1
1
  export * from './core';
2
2
  export * from './parse';
3
+ export * from './format';
3
4
  export * from './functions';
4
- export * from './transform';
package/build/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./core"), exports);
18
+ __exportStar(require("./parse"), exports);
19
+ __exportStar(require("./format"), exports);
20
+ __exportStar(require("./functions"), exports);
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,0CAAwB;AACxB,2CAAyB;AACzB,8CAA4B"}
@@ -0,0 +1,17 @@
1
+ import { Color } from './core';
2
+ /**
3
+ * Parse CSS color
4
+ * @param color CSS color string: #xxx, #xxxxxx, #xxxxxxxx, rgb(), rgba(), hsl(), hsla(), color()
5
+ */
6
+ export declare function parse(color: string): Color;
7
+ /**
8
+ * Parse hexadecimal CSS color
9
+ * @param color Hex color string: #xxx, #xxxxxx, #xxxxxxxx
10
+ */
11
+ export declare function parseHex(color: string): Color;
12
+ /**
13
+ * Parse CSS color
14
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
15
+ * @param color CSS color string: rgb(), rgba(), hsl(), hsla(), color()
16
+ */
17
+ export declare function parseColor(color: string): Color;