MCPcopy
hub / github.com/omgovich/colord / minifyPlugin

Function minifyPlugin

src/plugins/minify.ts:23–110  ·  view source on GitHub ↗
(ColordClass)

Source from the content-addressed store, hash-verified

21 * A plugin adding a color minification utilities.
22 */
23const minifyPlugin: Plugin = (ColordClass): void => {
24 // Finds the shortest hex representation
25 const minifyHex = (instance: Colord): string => {
26 const hex = instance.toHex();
27 const [, r1, r2, g1, g2, b1, b2, a1, a2] = hex.split("");
28
29 // Check if the string can be shorten
30 if (r1 === r2 && g1 === g2 && b1 === b2) {
31 if (instance.alpha() === 1) {
32 // Express as 3 digit hexadecimal string if the color doesn't have an alpha channel
33 return "#" + r1 + g1 + b1;
34 } else if (a1 === a2) {
35 // Format 4 digit hex
36 return "#" + r1 + g1 + b1 + a1;
37 }
38 }
39
40 return hex;
41 };
42
43 // Returns the shortest string in array
44 const findShortestString = (variants: string[]): string => {
45 let shortest = variants[0];
46
47 for (let index = 1; index < variants.length; index++) {
48 if (variants[index].length < shortest.length) shortest = variants[index];
49 }
50
51 return shortest;
52 };
53
54 // Removes leading zero before floating point if necessary
55 const shortenNumber = (number: number): string | number => {
56 if (number > 0 && number < 1) return number.toString().replace("0.", ".");
57 return number;
58 };
59
60 // Define new public method
61 ColordClass.prototype.minify = function (options: MinificationOptions = {}) {
62 const rgb = this.toRgb();
63 const r = shortenNumber(rgb.r);
64 const g = shortenNumber(rgb.g);
65 const b = shortenNumber(rgb.b);
66
67 const hsl = this.toHsl();
68 const h = shortenNumber(hsl.h);
69 const s = shortenNumber(hsl.s);
70 const l = shortenNumber(hsl.l);
71
72 const a = shortenNumber(this.alpha());
73
74 const defaults: MinificationOptions = {
75 hex: true,
76 rgb: true,
77 hsl: true,
78 };
79
80 const settings: MinificationOptions = Object.assign(defaults, options);

Callers

nothing calls this directly

Calls 7

shortenNumberFunction · 0.85
minifyHexFunction · 0.85
findShortestStringFunction · 0.85
toRgbMethod · 0.80
toHslMethod · 0.80
alphaMethod · 0.80
toNameMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…