-
Notifications
You must be signed in to change notification settings - Fork 3
/
colorsmith.test.ts
69 lines (62 loc) · 1.83 KB
/
colorsmith.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
import { assertEquals, assertThrows } from "https://deno.land/[email protected]/testing/asserts.ts";
import { hexToRGB, rgbToHex, rgbToHSL, hslToRGB, ColorsmithError } from "./colorsmith.ts";
Deno.test({
name: "RGB to hex",
fn: () => {
assertEquals(rgbToHex({ r: 0.5, g: 0.5, b: 0.5 }), "#808080");
assertEquals(rgbToHex({ r: 0, g: 0, b: 0 }), "#000000");
assertEquals(rgbToHex({ r: 1, g: 1, b: 1 }), "#ffffff");
},
});
Deno.test({
name: "Hex to RGB",
fn: () => {
assertEquals(hexToRGB("#808080"), { r: 0.5, g: 0.5, b: 0.5 });
},
});
Deno.test({
name: "Hex to RGB, invalid",
fn: () => {
assertThrows<ColorsmithError>(() => hexToRGB("black"));
assertThrows<ColorsmithError>(() => hexToRGB("#000"));
assertThrows<ColorsmithError>(() => hexToRGB("######"));
assertThrows<ColorsmithError>(() => hexToRGB("aabbcc"));
assertThrows<ColorsmithError>(() => hexToRGB("#gggggg"));
},
});
Deno.test({
name: "Round-trip hex through RGB",
fn: () => {
for (const color of [
"#000000",
"#ffffff",
"#654321",
"#123456",
]) {
assertEquals(rgbToHex(hexToRGB(color)), color);
}
},
});
Deno.test({
name: "RGB to HSL",
fn: () => {
assertEquals(rgbToHSL({ r: 1, g: 0, b: 0}), { h: 0, s: 1, l: 0.5});
assertEquals(rgbToHSL({ r: 0, g: 1, b: 1}), { h: 180, s: 1, l: 0.5});
},
});
Deno.test({
name: "Round-trip hex through RGB and HSL",
fn: () => {
const colors = [
"#123456",
"#654321",
"#808080",
"#baf00d",
"#999999",
"#f1f2f3",
];
for (const color of colors) {
assertEquals(rgbToHex(hslToRGB(rgbToHSL(hexToRGB(color)))), color);
}
},
});