-
Notifications
You must be signed in to change notification settings - Fork 624
/
ColorPalette.cs
174 lines (155 loc) · 5.83 KB
/
ColorPalette.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using Autodesk.DesignScript.Runtime;
using CoreNodeModels.Properties;
using Dynamo.Graph;
using Dynamo.Graph.Nodes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ProtoCore.AST.AssociativeAST;
using DSColor = DSCore.Color;
namespace CoreNodeModels.Input
{
[NodeName("Color Palette")]
[NodeDescription("ColorPaletteDescription", typeof(Resources))]
[NodeCategory("Core.Color.Create")]
[NodeSearchTags("ColorUISearchTags", typeof(Resources))]
[IsDesignScriptCompatible]
[OutPortNames("Color")]
[OutPortTypes("DSCore.Color")]
[OutPortDescriptions("Selected Color.")]
public class ColorPalette : NodeModel
{
private DSColor dscolor = DSColor.ByARGB(255, 0, 0, 0);
[JsonProperty(PropertyName = "InputValue")]
public DSColor DsColor
{
get { return dscolor; }
set
{
dscolor = value;
OnNodeModified();
RaisePropertyChanged("DsColor");
}
}
//override ExtensionNode NodeType from NodeModel base class
public override string NodeType
{
get
{
return "ColorInputNode";
}
}
public override NodeInputData InputData
{
get
{
//this object which we'll convert to a json string matches the format the schema expects for colors
var colorObj = new { Red = Convert.ToInt32(DsColor.Red), Blue = Convert.ToInt32(DsColor.Blue), Green = Convert.ToInt32(DsColor.Green), Alpha = Convert.ToInt32(DsColor.Alpha) };
return new NodeInputData()
{
Id = this.GUID,
Name = this.Name,
Type = NodeInputTypes.colorInput,
Type2 = NodeInputTypes.colorInput,
Description = this.Description,
Value = JsonConvert.SerializeObject(colorObj),
};
}
}
/// <summary>
/// Node constructor.
/// </summary>
public ColorPalette()
{
RegisterAllPorts();
}
[JsonConstructor]
public ColorPalette(JObject InputValue, IEnumerable<PortModel> inPorts, IEnumerable<PortModel> outPorts) : base(inPorts, outPorts)
{
// RGBA to ARGB
try
{
this.DsColor = DSColor.ByARGB((int)InputValue["A"], (int)InputValue["R"], (int)InputValue["G"], (int)InputValue["B"]);
}
catch
{
this.DsColor = DSColor.ByARGB(255, 0, 0, 0);
}
}
protected override bool UpdateValueCore(UpdateValueParams updateValueParams)
{
string name = updateValueParams.PropertyName;
string value = updateValueParams.PropertyValue;
switch (name)
{
case "DsColor":
this.DsColor = this.DeserializeValue(value);
return true;
}
return base.UpdateValueCore(updateValueParams);
}
private DSColor DeserializeValue(string val)
{
try
{
//Splits the xml string and returns each of the ARGB values as a string array.
string[] argb = Regex.Split(val, @"\D+").Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
return DSColor.ByARGB(int.Parse(argb[3]), int.Parse(argb[0]), int.Parse(argb[1]), int.Parse(argb[2]));
}
catch
{
return DSColor.ByARGB(255, 0, 0, 0);
}
}
private string SerializeValue()
{
return DsColor.ToString();
}
/// <summary>
/// Store color value when graph is saved.
/// </summary>
/// <param name="element"></param>
/// <param name="context"></param>
protected override void SerializeCore(XmlElement element, SaveContext context)
{
base.SerializeCore(element, context);
XmlElement color = element.OwnerDocument.CreateElement("DsColor");
color.InnerText = SerializeValue();
element.AppendChild(color);
}
/// <summary>
/// Restore stored value and set "dscolor" to it.
/// </summary>
/// <param name="element"></param>
/// <param name="context"></param>
protected override void DeserializeCore(XmlElement element, SaveContext context)
{
base.DeserializeCore(element, context);
var colorNode = element.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "DsColor");
if (colorNode != null)
{
DsColor = DeserializeValue(colorNode.InnerText);
}
}
/// <summary>
/// AST Output.
/// </summary>
/// <param name="inputAstNodes"></param>
/// <returns></returns>
[IsVisibleInDynamoLibrary(false)]
public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
{
var av = AstFactory.BuildIntNode(Convert.ToInt32(DsColor.Alpha));
var ar = AstFactory.BuildIntNode(Convert.ToInt32(DsColor.Red));
var ag = AstFactory.BuildIntNode(Convert.ToInt32(DsColor.Green));
var ab = AstFactory.BuildIntNode(Convert.ToInt32(DsColor.Blue));
var colorNode = AstFactory.BuildFunctionCall(
new Func<int, int, int, int, DSColor>(DSColor.ByARGB), new List<AssociativeNode> { av, ar, ag, ab });
return new[] { AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), colorNode) };
}
}
}