@kastov/uuid-color 0.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/.prettierrc +21 -0
- package/README.md +71 -0
- package/lib/index.d.ts +64 -0
- package/lib/index.js +105 -0
- package/package.json +35 -0
package/.prettierrc
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"bracketSpacing": true,
|
|
3
|
+
"tabWidth": 4,
|
|
4
|
+
"printWidth": 100,
|
|
5
|
+
"singleQuote": true,
|
|
6
|
+
"trailingComma": "all",
|
|
7
|
+
"overrides": [
|
|
8
|
+
{
|
|
9
|
+
"files": ["*.js", "*.jsx", "*.ts", "*.tsx"],
|
|
10
|
+
"options": {
|
|
11
|
+
"parser": "typescript"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"files": ["*.md", "*.json", "*.yaml", "*.yml"],
|
|
16
|
+
"options": {
|
|
17
|
+
"tabWidth": 2
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
}
|
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
**uuid-color package on npm: https://www.npmjs.com/package/uuid-color**
|
|
2
|
+
|
|
3
|
+
A lightweight package to deterministically generate unique and uniformly sampled colors from UUIDs.
|
|
4
|
+
|
|
5
|
+
### How it works
|
|
6
|
+
|
|
7
|
+
The RGB color space is represented by hexadecimal numbers in the 0x000000 to 0xffffff range. We use a simple modulo hash function (`% (0xffffff + 0x000001)` i.e. `% 0x100000`) to produce numbers in that range from the decimal representation of the UUID (a base 10 39-digit integer). We then extract the red, green, and blue components from the resulting hash (which represents a specific color in hexadecimal notation) using bit masks.
|
|
8
|
+
|
|
9
|
+
### Limitations
|
|
10
|
+
|
|
11
|
+
Since the color space is only 256^3 whereas the UUID space is much larger ([approximtely 5.3 x 10^36 possible UUIDs](https://www.uuidtools.com/what-is-uuid#overview)), it is impossible to create a complete bijection between the spaces, and so collisions in the generated color space can occur. This package does however guarantee that the color space is maximally utilized, assuming a uniform distribution of the input UUIDs within the UUID space.
|
|
12
|
+
|
|
13
|
+
# Installation:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm i uuid-color
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
# Usage
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { v4 as uuidv4 } from "uuid";
|
|
23
|
+
import { colorFromUuid } from "uuid-color";
|
|
24
|
+
|
|
25
|
+
const myUuid = uuidv4();
|
|
26
|
+
|
|
27
|
+
// returns a hex color code as a string "#rrggbb"
|
|
28
|
+
const hexColor = colorFromUuid(myUuid);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
[Live demo](https://codesandbox.io/s/uuid-color-usage-o0e4o)
|
|
32
|
+
|
|
33
|
+
# Reference - v0.1.0
|
|
34
|
+
|
|
35
|
+
## Table of contents
|
|
36
|
+
|
|
37
|
+
### Interfaces
|
|
38
|
+
|
|
39
|
+
- [Options](docs/dist/interfaces/Options.md)
|
|
40
|
+
|
|
41
|
+
### Functions
|
|
42
|
+
|
|
43
|
+
- [colorFromUuid](docs/dist/README.md#colorfromuuid)
|
|
44
|
+
|
|
45
|
+
## Functions
|
|
46
|
+
|
|
47
|
+
### colorFromUuid
|
|
48
|
+
|
|
49
|
+
▸ **colorFromUuid**(`uuid`, `options?`): `string`
|
|
50
|
+
|
|
51
|
+
Returns the generated color associated with the given uuid.
|
|
52
|
+
|
|
53
|
+
**`throws`** [Error](https://developer.mozilla.org/en-US/docs/web/javascript/reference/global_objects/error)
|
|
54
|
+
This exception is thrown if the input uuid string is not a valid UUID.
|
|
55
|
+
|
|
56
|
+
#### Parameters
|
|
57
|
+
|
|
58
|
+
| Name | Type | Description |
|
|
59
|
+
| :------ | :------ | :------ |
|
|
60
|
+
| `uuid` | `string` | The uuid for which to generate a color |
|
|
61
|
+
| `options` | [`Options`](docs/dist/interfaces/Options.md) | An optional object to configure the color generation, and attach callbacks that directly receive the generated color code or components in various formats |
|
|
62
|
+
|
|
63
|
+
#### Returns
|
|
64
|
+
|
|
65
|
+
`string`
|
|
66
|
+
|
|
67
|
+
The generated color as a CSS `<color>` notation string
|
|
68
|
+
|
|
69
|
+
#### Defined in
|
|
70
|
+
|
|
71
|
+
[index.ts:81](https://github.com/loucadufault/uuid-color/blob/1d2a5c0/src/index.ts#L81)
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
type SupportedColorNotations = 'hex' | 'rgb' | 'hsl';
|
|
2
|
+
type Receivers = {
|
|
3
|
+
hex?: (hexString: string) => void;
|
|
4
|
+
rgb?: (red: number, green: number, blue: number, alpha?: number) => void;
|
|
5
|
+
hsl?: (hue: number, saturation: number, lightness: number, alpha?: number) => void;
|
|
6
|
+
};
|
|
7
|
+
export interface Options {
|
|
8
|
+
/**
|
|
9
|
+
* Determines whether to skip rounding the generated color components.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* Only applies when the specified format is `"hsl"`, as this is the only output format that involves a lossy conversion (from RGB model components).
|
|
13
|
+
*
|
|
14
|
+
* @defaultValue `false`
|
|
15
|
+
*/
|
|
16
|
+
raw?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Determines the alpha of the generated color as a number between 0 and 1. Passed through to the final generated color without any involvement in the generation process.
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* Specifying any value (even if it is equal to or greater than 1) will cause the function to return the generated color as a notation string including the alpha component (see the alpha variants in {@link Options.format | Options.format}), and cause the alpha to be included in the parameter(s) of the call to the supplied {@link Receivers} value(s).
|
|
22
|
+
* Values are not validated, but are clamped to between 0 and 1.
|
|
23
|
+
*
|
|
24
|
+
* @defaultValue `1`
|
|
25
|
+
*/
|
|
26
|
+
alpha?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Determines the output format of the generated color.
|
|
29
|
+
*
|
|
30
|
+
* hex: #rrggbb[aa]
|
|
31
|
+
* rgb: rgb[a](R, G, B[, A])
|
|
32
|
+
* hsl: hsl[a](H, S, L[, A])
|
|
33
|
+
* @see { @link https://developer.mozilla.org/en-US/docs/Web/CSS/color_value | \<color\> }
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* Colors are returned as strings in the CSS <color> data type hexadecimal or comma-separated functional notation corresponding to the specified format.
|
|
37
|
+
*
|
|
38
|
+
* @defaultValue `"hex"`
|
|
39
|
+
*/
|
|
40
|
+
format?: SupportedColorNotations;
|
|
41
|
+
/**
|
|
42
|
+
* Attach callbacks to {@link SupportedColorNotations | supported color notations} as keys that will be called with the corresponding generated color components or code. Eliminates the need to parse the returned string if further manipulation is desired.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* Does not affect the return value of {@link colorFromUuid | the `colorFromUuid` function}.
|
|
46
|
+
*
|
|
47
|
+
* @see {@link Receivers | the Receivers type}
|
|
48
|
+
*/
|
|
49
|
+
receivers?: Receivers;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns the generated color associated with the given uuid.
|
|
53
|
+
*
|
|
54
|
+
* @param uuid - The uuid for which to generate a color
|
|
55
|
+
* @param options - An optional object to configure the color generation, and attach callbacks that directly receive the generated color code or components in various formats
|
|
56
|
+
* @returns The generated color as a CSS `<color>` notation string
|
|
57
|
+
*
|
|
58
|
+
* @throws {@link https://developer.mozilla.org/en-US/docs/web/javascript/reference/global_objects/error | Error}
|
|
59
|
+
* This exception is thrown if the input uuid string is not a valid UUID.
|
|
60
|
+
*
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
export declare function colorFromUuid(uuid: string, options?: Options): string;
|
|
64
|
+
export {};
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.colorFromUuid = colorFromUuid;
|
|
13
|
+
var convert = require("color-convert");
|
|
14
|
+
var DEFAULT_COLOR_FORMAT = 'hex';
|
|
15
|
+
var DEFAULT_IS_RAW = false;
|
|
16
|
+
function uuidToBigInt(uuid) {
|
|
17
|
+
return BigInt('0x' + uuid.replace(/-/g, ''));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns the generated color associated with the given uuid.
|
|
21
|
+
*
|
|
22
|
+
* @param uuid - The uuid for which to generate a color
|
|
23
|
+
* @param options - An optional object to configure the color generation, and attach callbacks that directly receive the generated color code or components in various formats
|
|
24
|
+
* @returns The generated color as a CSS `<color>` notation string
|
|
25
|
+
*
|
|
26
|
+
* @throws {@link https://developer.mozilla.org/en-US/docs/web/javascript/reference/global_objects/error | Error}
|
|
27
|
+
* This exception is thrown if the input uuid string is not a valid UUID.
|
|
28
|
+
*
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
function colorFromUuid(uuid, options) {
|
|
32
|
+
if (options === void 0) { options = {}; }
|
|
33
|
+
var encodedUuid = uuidToBigInt(uuid);
|
|
34
|
+
var colorCode = Number(encodedUuid % BigInt(0x1000000));
|
|
35
|
+
var red = colorCode >> 16;
|
|
36
|
+
var green = (colorCode >> 8) & 0xff;
|
|
37
|
+
var blue = colorCode & 0xff;
|
|
38
|
+
var receivers = {};
|
|
39
|
+
if (options.hasOwnProperty('receivers')) {
|
|
40
|
+
['rgb', 'hsl', 'hex'].forEach(function (format) {
|
|
41
|
+
if (options.receivers.hasOwnProperty(format)) {
|
|
42
|
+
receivers[format] = options.receivers[format]; // link to callbacks
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
var isRaw = DEFAULT_IS_RAW;
|
|
47
|
+
if (options.hasOwnProperty('raw')) {
|
|
48
|
+
isRaw = options.raw;
|
|
49
|
+
}
|
|
50
|
+
var alpha;
|
|
51
|
+
if (options.hasOwnProperty('alpha')) {
|
|
52
|
+
alpha = Math.min(Math.max(options.alpha, 0), 1); // clamp to [0; 1]
|
|
53
|
+
}
|
|
54
|
+
if ('rgb' in receivers) {
|
|
55
|
+
if (alpha === undefined) {
|
|
56
|
+
receivers.rgb(red, green, blue);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
receivers.rgb(red, green, blue, alpha);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if ('hsl' in receivers) {
|
|
63
|
+
var hsl = isRaw
|
|
64
|
+
? convert.default.rgb.hsl.raw(red, green, blue)
|
|
65
|
+
: convert.default.rgb.hsl(red, green, blue);
|
|
66
|
+
if (alpha === undefined) {
|
|
67
|
+
receivers.hsl.apply(receivers, hsl);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
receivers.hsl.apply(receivers, __spreadArray(__spreadArray([], hsl, true), [alpha], false));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if ('hex' in receivers) {
|
|
74
|
+
var hexColorCode = convert.default.rgb.hex(red, green, blue).toLowerCase();
|
|
75
|
+
if (alpha === undefined) {
|
|
76
|
+
receivers.hex(hexColorCode);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
var hexAlphaCode = Math.floor(alpha * 255).toString(16);
|
|
80
|
+
receivers.hex(hexColorCode + hexAlphaCode);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
var format = DEFAULT_COLOR_FORMAT;
|
|
84
|
+
if (options.hasOwnProperty('format')) {
|
|
85
|
+
format = options.format;
|
|
86
|
+
}
|
|
87
|
+
switch (format) {
|
|
88
|
+
case 'rgb':
|
|
89
|
+
return alpha === undefined
|
|
90
|
+
? "rgb(".concat(red, ", ").concat(green, ", ").concat(blue, ")")
|
|
91
|
+
: "rgb(".concat(red, ", ").concat(green, ", ").concat(blue, ", ").concat(alpha, ")");
|
|
92
|
+
case 'hsl':
|
|
93
|
+
var hsl = isRaw
|
|
94
|
+
? convert.default.rgb.hsl.raw(red, green, blue)
|
|
95
|
+
: convert.default.rgb.hsl(red, green, blue);
|
|
96
|
+
return alpha === undefined
|
|
97
|
+
? "hsl(".concat(hsl[0], ", ").concat(hsl[1], "%, ").concat(hsl[2], "%)")
|
|
98
|
+
: "hsl(".concat(hsl[0], ", ").concat(hsl[1], "%, ").concat(hsl[2], "%, ").concat(alpha, ")");
|
|
99
|
+
default: // don't error
|
|
100
|
+
case 'hex':
|
|
101
|
+
var hexColorCode = convert.default.rgb.hex(red, green, blue).toLowerCase();
|
|
102
|
+
var hexAlphaCode = Math.floor(alpha * 255).toString(16);
|
|
103
|
+
return alpha === undefined ? "#".concat(hexColorCode) : "#".concat(hexColorCode).concat(hexAlphaCode);
|
|
104
|
+
}
|
|
105
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kastov/uuid-color",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A lightweight package to generate unique and uniformly sampled colors from UUIDs.",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"prepare": "rm -rf lib && NODE_ENV=production tsc"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/kastov/uuid-color.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"uuid",
|
|
16
|
+
"color",
|
|
17
|
+
"colour",
|
|
18
|
+
"unique",
|
|
19
|
+
"hash",
|
|
20
|
+
"uuid-v4"
|
|
21
|
+
],
|
|
22
|
+
"author": "Louca Dufault",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/kastov/uuid-color/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/kastov/uuid-color#readme",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"typescript": "^5.9.3"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"color-convert": "^3.1.3"
|
|
33
|
+
},
|
|
34
|
+
"sideEffects": false
|
|
35
|
+
}
|