MCPcopy Create free account
hub / github.com/bernaferrari/FigmaToCode / rgbToCssColor

Function rgbToCssColor

packages/backend/src/common/color.ts:32–59  ·  view source on GitHub ↗
(color: RGB | RGBA, alpha: number = 1)

Source from the content-addressed store, hash-verified

30 * @returns A CSS color string
31 */
32export const rgbToCssColor = (color: RGB | RGBA, alpha: number = 1): string => {
33 // Special cases for common colors
34 if (color.r === 1 && color.g === 1 && color.b === 1 && alpha === 1) {
35 return "white";
36 }
37
38 if (color.r === 0 && color.g === 0 && color.b === 0 && alpha === 1) {
39 return "black";
40 }
41
42 // Return hex when possible (no transparency)
43 if (alpha === 1) {
44 const r = Math.round(color.r * 255);
45 const g = Math.round(color.g * 255);
46 const b = Math.round(color.b * 255);
47
48 const toHex = (num: number): string => num.toString(16).padStart(2, "0");
49 return `#${toHex(r)}${toHex(g)}${toHex(b)}`.toUpperCase();
50 }
51
52 // Use rgba for transparent colors
53 const r = numberToFixedString(color.r * 255);
54 const g = numberToFixedString(color.g * 255);
55 const b = numberToFixedString(color.b * 255);
56 const a = numberToFixedString(alpha);
57
58 return `rgba(${r}, ${g}, ${b}, ${a})`;
59};
60
61// ---- Gradient Transformation ----
62export const gradientAngle = (fill: GradientPaint): number => {

Callers

nothing calls this directly

Calls 2

numberToFixedStringFunction · 0.90
toHexFunction · 0.70

Tested by

no test coverage detected