-
Notifications
You must be signed in to change notification settings - Fork 39
/
color-hash.test.ts
77 lines (68 loc) · 2.6 KB
/
color-hash.test.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
import {assertEquals, assert} from 'https://deno.land/[email protected]/testing/asserts.ts';
import {generate as generateUUID} from 'https://deno.land/[email protected]/uuid/v4.ts';
import {HSL2RGB, RGB2HEX} from './lib/colors.ts';
import ColorHash from './mod.ts';
Deno.test("ColorHash#Hue: should return the hash color based on default hue", () => {
const hash = new ColorHash();
for (let i = 0; i < 100; i++) {
const hue = hash.hsl(generateUUID())[0];
assertEquals(hue >= 0 && hue < 359, true); // hash % 359 means max 358
}
})
Deno.test("ColorHash#Hue: should return the hash color based on given hue value", () => {
const hash = new ColorHash({hue: 10});
for (let i = 0; i < 100; i++) {
const hue = hash.hsl(generateUUID())[0];
assertEquals(hue, 10);
}
})
Deno.test("ColorHash#Hue: should return the hash color based on given hue range", () => {
for (let min = 0; min < 361; min += 60) {
for (let max = min + 1; max < 361; max += 60) {
const hash = new ColorHash({hue: {min, max}});
for (let i = 0; i < 100; i++) {
const hue = hash.hsl(generateUUID())[0];
assertEquals(hue >= min && hue < max, true);
}
}
}
})
Deno.test("ColorHash#Hue: should work for multiple hue ranges", () => {
var ranges = [
{min: 30, max: 90},
{min: 180, max: 210},
{min: 270, max: 285}
];
const hash = new ColorHash({hue: ranges});
for (let i = 0; i < 100; i++) {
const hue = hash.hsl(generateUUID())[0];
assertEquals(ranges.some((range) => hue >= range.min && hue < range.max), true);
}
})
Deno.test("ColorHash#LS: should return color based on given lightness and saturation", () => {
const hash = new ColorHash({lightness: 0.5, saturation: 0.5});
const [h, s, l] = hash.hsl(generateUUID());
assertEquals(s, 0.5);
assertEquals(l, 0.5);
})
Deno.test("ColorHash should return the hash color based on given lightness array and saturation array", () => {
const hash = new ColorHash({
lightness: [0.9, 1],
saturation: [0.9, 1]
})
const [h, s, l] = hash.hsl(generateUUID());
assertEquals([0.9, 1].includes(s), true);
assertEquals([0.9, 1].includes(l), true);
})
Deno.test("Custom hash function", () => {
const customHash = function (str: string) {
var hash = 0;
for (let i = 0; i < str.length; i++) {
hash += str.charCodeAt(i);
}
return hash;
};
const hash = new ColorHash({hash: customHash});
const h = customHash('abc') % 359;
assertEquals(hash.hsl('abc')[0], h);
})