-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsnapshot.cjs
More file actions
138 lines (122 loc) · 4.36 KB
/
Copy pathsnapshot.cjs
File metadata and controls
138 lines (122 loc) · 4.36 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const fs = require("fs");
const path = require("path");
const sass = require("sass");
const samples = [
"#1100ff",
"#0066aa",
"#cc33cc",
"#0022dd",
"#0066ee",
"#bbbb11",
"#5566dd",
"#55eeaa",
"#ee7700",
"#33bbaa",
"#44aa77",
"#dd6699",
"#99ee22",
"#ff22aa",
"#99aa11",
"#dd1122",
];
function hexToRgb(hex) {
return {
r: parseInt(hex.slice(1, 3), 16),
g: parseInt(hex.slice(3, 5), 16),
b: parseInt(hex.slice(5, 7), 16),
};
}
function sassInspect($map) {
const entries = [];
if ($map.contents) {
$map.contents.forEach((v, k) => {
const key = k instanceof sass.SassString ? k.text : k.toString();
entries.push(`'${key}': ${v}`);
});
}
return new sass.SassString("(" + entries.join(", ") + ")");
}
const debugOutput = {};
const result = sass.compileString(
`
@use 'conversions' as conv;
@use 'conversions/lch' as lch;
@use 'conversions/luv' as luv;
@use 'conversions/xyz' as xyz;
@use 'conversions/rgb' as rgb;
@function map-inspect($map) {
@return map-inspect-custom($map);
}
${samples
.map((color) => {
const rgb = hexToRgb(color);
const hex = color.slice(1);
return `
@debug "${color}-hsluv " + map-inspect(conv.rgb-hsluv(('r': ${rgb.r}, 'g': ${rgb.g}, 'b': ${rgb.b})));
@debug "${color}-hpluv " + map-inspect(conv.rgb-hpluv(('r': ${rgb.r}, 'g': ${rgb.g}, 'b': ${rgb.b})));
$hsluv${hex}: conv.rgb-hsluv(('r': ${rgb.r}, 'g': ${rgb.g}, 'b': ${rgb.b}));
$hpluv${hex}: conv.rgb-hpluv(('r': ${rgb.r}, 'g': ${rgb.g}, 'b': ${rgb.b}));
$lch-from-hsluv${hex}: lch.from-hsluv($hsluv${hex});
$lch-from-hpluv${hex}: lch.from-hpluv($hpluv${hex});
$luv${hex}: luv.from-lch($lch-from-hsluv${hex});
$xyz${hex}: xyz.from-luv($luv${hex});
$rgb-from-xyz${hex}: rgb.from-xyz($xyz${hex});
$xyz-back${hex}: rgb.to-xyz(('r': ${rgb.r}, 'g': ${rgb.g}, 'b': ${rgb.b}));
$luv-back${hex}: xyz.to-luv($xyz-back${hex});
$lch-back${hex}: luv.to-lch($luv-back${hex});
$hsluv-back${hex}: lch.to-hsluv($lch-back${hex});
$hpluv-back${hex}: lch.to-hpluv($lch-back${hex});
@debug "${color}-lch " + map-inspect($lch-from-hsluv${hex});
@debug "${color}-luv " + map-inspect($luv${hex});
@debug "${color}-xyz " + map-inspect($xyz${hex});
@debug "${color}-rgb " + map-inspect($rgb-from-xyz${hex});
@debug "${color}-xyz-back " + map-inspect($xyz-back${hex});
@debug "${color}-luv-back " + map-inspect($luv-back${hex});
@debug "${color}-lch-back " + map-inspect($lch-back${hex});
@debug "${color}-hsluv-back " + map-inspect($hsluv-back${hex});
@debug "${color}-hpluv-back " + map-inspect($hpluv-back${hex});
@debug "${color}-lch-from-hpluv " + map-inspect($lch-from-hpluv${hex});
`;
})
.join("\n")}
`,
{
loadPaths: [path.join(__dirname, "src")],
functions: {
"map-inspect-custom($map)": (args) => sassInspect(args[0]),
},
logger: {
debug: (msg) => {
const spaceIdx = msg.indexOf(" ");
const key = msg.slice(0, spaceIdx);
const val = msg.slice(spaceIdx + 1).trim();
debugOutput[key] = val;
},
warn: () => {},
},
}
);
const lines = ["$values: ("];
samples.forEach((color, i) => {
const last = i === samples.length - 1;
lines.push(` '${color}': (`);
lines.push(` 'hsluv': ${debugOutput[color + "-hsluv"]},`);
lines.push(` 'hpluv': ${debugOutput[color + "-hpluv"]},`);
lines.push(` 'lch': ${debugOutput[color + "-lch"]},`);
lines.push(` 'luv': ${debugOutput[color + "-luv"]},`);
lines.push(` 'xyz': ${debugOutput[color + "-xyz"]},`);
lines.push(` 'rgb': ${debugOutput[color + "-rgb"]},`);
lines.push(` 'xyz-back': ${debugOutput[color + "-xyz-back"]},`);
lines.push(` 'luv-back': ${debugOutput[color + "-luv-back"]},`);
lines.push(` 'lch-back': ${debugOutput[color + "-lch-back"]},`);
lines.push(` 'hsluv-back': ${debugOutput[color + "-hsluv-back"]},`);
lines.push(` 'hpluv-back': ${debugOutput[color + "-hpluv-back"]},`);
lines.push(` 'lch-from-hpluv': ${debugOutput[color + "-lch-from-hpluv"]}`);
lines.push(` )${last ? "" : ","}`);
});
lines.push(");\n");
const outputPath = path.join(__dirname, "test", "data", "_snapshot.scss");
fs.writeFileSync(outputPath, lines.join("\n"), "utf8");
console.log(
`Snapshot written to ${outputPath} (${Object.keys(debugOutput).length} values)`
);