-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathconversionTables.ts
More file actions
242 lines (207 loc) · 6.85 KB
/
conversionTables.ts
File metadata and controls
242 lines (207 loc) · 6.85 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { nearestColorFrom } from "../nearest-color/nearestColor";
import { numberToFixedString } from "../common/numToAutoFixed";
import { localTailwindSettings } from "./tailwindMain";
import { config } from "./tailwindConfig";
import { rgbTo6hex } from "../common/color";
export const nearestValue = (goal: number, array: Array<number>): number => {
return array.reduce((prev, curr) => {
return Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev;
});
};
// New function to get nearest value only if it's within acceptable threshold
export const nearestValueWithThreshold = (
goal: number,
array: Array<number>,
thresholdPercent: number = 15,
): number | null => {
const nearest = nearestValue(goal, array);
const diff = Math.abs(nearest - goal);
const percentDiff = (diff / goal) * 100;
if (percentDiff <= thresholdPercent) {
return nearest;
}
return null;
};
export const exactValue = (
goal: number,
array: Array<number>,
): number | null => {
for (let i = 0; i < array.length; i++) {
const diff = Math.abs(goal - array[i]);
if (diff <= 0.05) {
return array[i];
}
}
return null;
};
/**
* convert pixel values to Tailwind attributes.
* by default, Tailwind uses rem, while Figma uses px.
* Therefore, a conversion is necessary. Rem = Pixel / baseFontSize
* Then, find in the corresponding table the closest value.
*/
const pxToRemToTailwind = (
value: number,
conversionMap: Record<number, string>,
): string => {
const keys = Object.keys(conversionMap).map((d) => +d);
// Use the configured base font size or fall back to default 16px
const baseFontSize = localTailwindSettings.baseFontSize || 16;
const remValue = value / baseFontSize;
const convertedValue = exactValue(remValue, keys);
if (convertedValue) {
return conversionMap[convertedValue];
} else if (localTailwindSettings.roundTailwindValues) {
// Only round if the nearest value is within acceptable threshold
const thresholdValue = nearestValueWithThreshold(remValue, keys, 15);
if (thresholdValue !== null) {
return conversionMap[thresholdValue];
}
}
return `[${numberToFixedString(value)}px]`;
};
const pxToTailwind = (
value: number,
conversionMap: Record<number, string>,
): string => {
const keys = Object.keys(conversionMap).map((d) => +d);
const convertedValue = exactValue(value, keys);
if (convertedValue) {
return conversionMap[convertedValue];
} else if (localTailwindSettings.roundTailwindValues) {
// Only round if the nearest value is within acceptable threshold
const thresholdValue = nearestValueWithThreshold(value, keys, 15);
if (thresholdValue !== null) {
return conversionMap[thresholdValue];
}
}
return `[${numberToFixedString(value)}px]`;
};
export const pxToLetterSpacing = (value: number): string => {
return pxToRemToTailwind(value, config.letterSpacing);
};
export const pxToLineHeight = (value: number): string => {
return pxToRemToTailwind(value, config.lineHeight);
};
export const pxToFontSize = (value: number): string => {
return pxToRemToTailwind(value, config.fontSize);
};
export const pxToBorderRadius = (value: number): string => {
const conversionMap = localTailwindSettings.useTailwind4
? config.borderRadiusV4
: config.borderRadius;
return pxToRemToTailwind(value, conversionMap);
};
export const pxToBorderWidth = (value: number): string | null => {
return pxToTailwind(value, config.border);
};
export const pxToOutline = (value: number): string | null => {
return pxToTailwind(value, config.outline);
};
export const pxToBlur = (value: number): string | null => {
const conversionMap = localTailwindSettings.useTailwind4
? config.blurV4
: config.blur;
return pxToTailwind(value, conversionMap);
};
export const pxToLayoutSize = (value: number): string => {
// Scale the input value according to the base font size ratio
const baseFontSize = localTailwindSettings.baseFontSize || 16;
// If baseFontSize is different than 16, we need to adjust the pixel value
// For example, with baseFontSize=14, 7px should match with the key for 8px (w-2)
const scaledValue = (value * 16) / baseFontSize;
// Use pxToTailwind directly with the scaled value, since the keys in config.layoutSize
// are likely in pixels based on a 16px base font size
const result = pxToTailwind(scaledValue, config.layoutSize);
return result !== null ? result : `[${numberToFixedString(value)}px]`;
};
export const nearestOpacity = (nodeOpacity: number): number => {
return nearestValue(nodeOpacity * 100, config.opacity);
};
export const nearestColor = nearestColorFrom(Object.keys(config.color));
/**
* @return Tailwind color name and Hex value with leading #
*/
export const nearestColorFromRgb = (color: RGB) => {
// figma uses r,g,b in [0, 1], while nearestColor uses it in [0, 255]
const colorMultiplied = {
r: color.r * 255,
g: color.g * 255,
b: color.b * 255,
};
const value = nearestColor(colorMultiplied);
const name = config.color[value];
return { name, value };
};
export const variableToColorName = async (id: string) => {
return (
(await figma.variables.getVariableByIdAsync(id))?.name
.replaceAll("/", "-")
.replaceAll(" ", "-") || id.toLowerCase().replaceAll(":", "-")
);
};
/**
* Get color information based on given color and user settings
*
* Returns type, name, hex and meta values
*/
export function getColorInfo(fill: SolidPaint | ColorStop) {
// variables
let colorName: string;
let colorType: "arbitrary" | "tailwind" | "variable";
let hex: string = "#" + rgbTo6hex(fill.color);
let meta: string = "";
// variable
if ((fill as any).variableColorName) {
// Use pre-computed variable name if available
colorName = (fill as any).variableColorName; // || variableToColorName(fill.boundVariables.color);
colorType = "variable";
meta = "custom";
return {
colorType,
colorName,
hex,
meta,
};
}
// Check for pure black/white first
if (fill.color.r === 0 && fill.color.g === 0 && fill.color.b === 0) {
return {
colorType: "tailwind",
colorName: "black",
hex: "#000000",
meta: "",
};
} else if (fill.color.r === 1 && fill.color.g === 1 && fill.color.b === 1) {
return {
colorType: "tailwind",
colorName: "white",
hex: "#ffffff",
meta: "",
};
} else {
// get tailwind color as comparison
const { name, value } = nearestColorFromRgb(fill.color);
// round color
if (localTailwindSettings.roundTailwindColors || hex === value) {
colorName = name;
colorType = "tailwind";
if (hex !== value) {
meta = "rounded";
}
// must come last, as previous comparison
hex = value;
}
// exact color
else {
colorName = `[${hex}]`;
colorType = "arbitrary";
}
}
return {
colorType,
colorName,
hex,
meta,
};
}