-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmod.ts
105 lines (89 loc) · 3.03 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import BKDRHash from './lib/bkdr-hash.ts';
import { Sha256ToInt } from './lib/sha256.ts';
import { RGB2HEX, HSL2RGB } from './lib/colors.ts';
class ColorHash {
L: number[];
S: number[];
hueRanges: {max: number, min: number}[]
hash: (str: string) => number;
constructor(options: {
lightness?: number | number[],
saturation?: number | number[],
hue?: number | {max: number, min: number} | {max: number, min: number}[],
hash?: string | ((str: string) => number);
} = {}) {
const [L, S] = [options.lightness, options.saturation].map(function(param) {
param = param !== undefined ? param : [0.35, 0.5, 0.65]; // note that 3 is a prime
return Array.isArray(param) ? param.concat() : [param];
});
this.L = L;
this.S = S;
if (typeof options.hue === 'number') {
options.hue = {min: options.hue, max: options.hue};
}
if (typeof options.hue === 'object' && !Array.isArray(options.hue)) {
options.hue = [options.hue];
}
if (typeof options.hue === 'undefined') {
options.hue = [];
}
this.hueRanges = options.hue.map(function (range) {
return {
min: typeof range.min === 'undefined' ? 0 : range.min,
max: typeof range.max === 'undefined' ? 360: range.max
};
});
this.hash = Sha256ToInt; // Default hash function
if (typeof options.hash === 'function') {
this.hash = options.hash;
}
if (options.hash === 'bkdr') {
this.hash = BKDRHash;
}
}
/**
* Returns the hash in [h, s, l].
* Note that H ∈ [0, 360); S ∈ [0, 1]; L ∈ [0, 1];
*
* @param {String} str string to hash
* @returns {Array} [h, s, l]
*/
hsl(str: string): [number, number, number] {
var H, S, L;
var hash = this.hash(str);
var hueResolution = 727; // note that 727 is a prime
if (this.hueRanges.length) {
const range = this.hueRanges[hash % this.hueRanges.length];
H = ((hash / this.hueRanges.length) % hueResolution) * (range.max - range.min) / hueResolution + range.min;
} else {
H = hash % 359; // note that 359 is a prime
}
hash = Math.ceil(hash / 360);
S = this.S[hash % this.S.length];
hash = Math.ceil(hash / this.S.length);
L = this.L[hash % this.L.length];
return [H, S, L];
}
/**
* Returns the hash in [r, g, b].
* Note that R, G, B ∈ [0, 255]
*
* @param {String} str string to hash
* @returns {Array} [r, g, b]
*/
rgb(str: string) {
var hsl = this.hsl(str);
return HSL2RGB.apply(this, hsl);
}
/**
* Returns the hash in hex
*
* @param {String} str string to hash
* @returns {String} hex with #
*/
hex(str: string) {
var rgb = this.rgb(str);
return RGB2HEX(rgb);
}
}
export default ColorHash;