forked from TrackMan/Unity.Package.FigmaToUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeExtensions.cs
68 lines (58 loc) · 1.98 KB
/
NodeExtensions.cs
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
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Figma
{
using global;
public static class NodeExtensions
{
#region Methods
public static void SetParentRecursively(this BaseNode node)
{
switch (node)
{
case DocumentNode document:
foreach (CanvasNode canvas in document.children)
{
canvas.parent = node;
SetParentRecursively(canvas);
}
break;
case ChildrenMixin { children: not null } children:
foreach (SceneNode child in children.children)
{
child.parent = node;
SetParentRecursively(child);
}
break;
}
}
public static string GetHash(this GradientPaint gradient)
{
using SHA1CryptoServiceProvider sha1 = new();
using MemoryStream stream = new();
using BinaryWriter writer = new(stream);
writer.Write((int)gradient.type);
foreach (ColorStop stop in gradient.gradientStops)
{
writer.Write(stop.position);
writer.Write(stop.color.r);
writer.Write(stop.color.g);
writer.Write(stop.color.b);
writer.Write(stop.color.a);
}
foreach (Vector position in gradient.gradientHandlePositions)
{
writer.Write(position.x);
writer.Write(position.y);
}
byte[] bytes = stream.ToArray();
byte[] hashBytes = sha1.ComputeHash(bytes);
StringBuilder hashBuilder = new();
foreach (byte @byte in hashBytes)
hashBuilder.Append(@byte.ToString("x2"));
return hashBuilder.ToString();
}
#endregion
}
}