Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Blend.cs
1using System;
2using System.Collections.Generic;
3using System.Runtime.InteropServices;
4using SkiaSharp;
8
10{
15 {
27 {
28 }
29
33 public override string[] DefaultArgumentNames
34 {
35 get
36 {
37 return new string[] { "c1", "c2", "p" };
38 }
39 }
40
44 public override string FunctionName
45 {
46 get
47 {
48 return "Blend";
49 }
50 }
51
59 {
60 object x1 = Arguments[0].AssociatedObjectValue;
61 object x2 = Arguments[1].AssociatedObjectValue;
62 double p = Expression.ToDouble(Arguments[2].AssociatedObjectValue);
63 PixelInformation Img1, Img2;
64 SKColor c1, c2;
65
66 if (x1 is SKColor C1)
67 {
68 c1 = C1;
69 Img1 = null;
70 }
71 else if (x1 is Graph G1)
72 {
73 Img1 = G1.CreatePixels(Variables);
74 c1 = SKColor.Empty;
75 }
76 else if (x1 is SKImage I1)
77 {
78 Img1 = PixelInformation.FromImage(I1);
79 c1 = SKColor.Empty;
80 }
81 else
82 {
83 Img1 = null;
84 c1 = Graph.ToColor(x1);
85 }
86
87 if (x2 is SKColor C2)
88 {
89 c2 = C2;
90 Img2 = null;
91 }
92 else if (x2 is Graph G2)
93 {
94 Img2 = G2.CreatePixels(Variables);
95 c2 = SKColor.Empty;
96 }
97 else if (x2 is SKImage I2)
98 {
99 Img2 = PixelInformation.FromImage(I2);
100 c2 = SKColor.Empty;
101 }
102 else
103 {
104 Img2 = null;
105 c2 = Graph.ToColor(x2);
106 }
107
108 if (Img1 is null && Img2 is null)
109 return new ObjectValue(BlendColors(c1, c2, p));
110 else if (Img1 is null)
111 return new GraphBitmap(Variables, BlendColors(Img2, c1, 1 - p));
112 else if (Img2 is null)
113 return new GraphBitmap(Variables, BlendColors(Img1, c2, p));
114 else
115 return new GraphBitmap(Variables, BlendColors(Img1, Img2, p));
116 }
117
125 public static SKColor BlendColors(SKColor c1, SKColor c2, double p)
126 {
127 int R = (int)(c1.Red * (1 - p) + c2.Red * p + 0.5);
128 int G = (int)(c1.Green * (1 - p) + c2.Green * p + 0.5);
129 int B = (int)(c1.Blue * (1 - p) + c2.Blue * p + 0.5);
130 int A = (int)(c1.Alpha * (1 - p) + c2.Alpha * p + 0.5);
131
132 if (R < 0)
133 R = 0;
134 else if (R > 255)
135 R = 255;
136
137 if (G < 0)
138 G = 0;
139 else if (G > 255)
140 G = 255;
141
142 if (B < 0)
143 B = 0;
144 else if (B > 255)
145 B = 255;
146
147 if (A < 0)
148 A = 0;
149 else if (A > 255)
150 A = 255;
151
152 return new SKColor((byte)R, (byte)G, (byte)B, (byte)A);
153 }
154
162 public static PixelInformation BlendColors(PixelInformation Pixels, SKColor Color, double p)
163 {
164 PixelInformationRaw Raw = Pixels.GetRaw();
165 byte[] Bin = (byte[])Raw.Binary.Clone();
166 int i, j, c = Raw.Binary.Length;
167 byte R = Color.Red;
168 byte G = Color.Green;
169 byte B = Color.Blue;
170 byte A = Color.Alpha;
171
172 for (i = 0; i < c; i++)
173 {
174 j = (int)(Bin[i] * (1 - p) + B * p + 0.5);
175 if (j < 0)
176 Bin[i] = 0;
177 else if (j > 255)
178 Bin[i] = 255;
179 else
180 Bin[i] = (byte)j;
181
182 j = (int)(Bin[++i] * (1 - p) + G * p + 0.5);
183 if (j < 0)
184 Bin[i] = 0;
185 else if (j > 255)
186 Bin[i] = 255;
187 else
188 Bin[i] = (byte)j;
189
190 j = (int)(Bin[++i] * (1 - p) + R * p + 0.5);
191 if (j < 0)
192 Bin[i] = 0;
193 else if (j > 255)
194 Bin[i] = 255;
195 else
196 Bin[i] = (byte)j;
197
198 j = (int)(Bin[++i] * (1 - p) + A * p + 0.5);
199 if (j < 0)
200 Bin[i] = 0;
201 else if (j > 255)
202 Bin[i] = 255;
203 else
204 Bin[i] = (byte)j;
205 }
206
207 return new PixelInformationRaw(Raw.ColorType, Bin, Raw.Width, Raw.Height, Raw.BytesPerRow);
208 }
209
217 public static PixelInformation BlendColors(PixelInformation Image1, PixelInformation Image2, double p)
218 {
219 if (Image1.Width != Image2.Width || Image1.Height != Image2.Height)
220 throw new ArgumentException("Images not of the same size.", nameof(Image2));
221
222 PixelInformationRaw Raw1 = Image1.GetRaw();
223 PixelInformationRaw Raw2 = Image2.GetRaw();
224 byte[] Bin1 = (byte[])Raw1.Binary.Clone();
225 byte[] Bin2 = Raw2.Binary;
226 int i, j, c = Bin1.Length;
227
228 if (Bin2.Length != c)
229 throw new ArgumentException("Images not of the same size.", nameof(Image2));
230
231 for (i = 0; i < c; i++)
232 {
233 j = (int)(Bin1[i] * (1 - p) + Bin2[i] * p + 0.5);
234 if (j < 0)
235 Bin1[i] = 0;
236 else if (j > 255)
237 Bin1[i] = 255;
238 else
239 Bin1[i] = (byte)j;
240 }
241
242 return new PixelInformationRaw(Raw1.ColorType, Bin1, Raw1.Width, Raw1.Height, Raw1.BytesPerRow);
243 }
244
245 }
246}
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
Blends colors c1 and c2 together using a blending factor 0<=p<=1. Any or both of c1 and c2 can be an ...
Definition: Blend.cs:15
Blend(ScriptNode c1, ScriptNode c2, ScriptNode p, int Start, int Length, Expression Expression)
Blends colors c1 and c2 together using a blending factor 0<=p<=1. Any or both of c1 and c2 can be an ...
Definition: Blend.cs:25
static SKColor BlendColors(SKColor c1, SKColor c2, double p)
Blends two colors using a blending factor.
Definition: Blend.cs:125
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: Blend.cs:58
override string FunctionName
Name of the function
Definition: Blend.cs:45
override string[] DefaultArgumentNames
Default Argument names
Definition: Blend.cs:34
static PixelInformation BlendColors(PixelInformation Pixels, SKColor Color, double p)
Blends an image with a fixed color using a blending factor.
Definition: Blend.cs:162
static PixelInformation BlendColors(PixelInformation Image1, PixelInformation Image2, double p)
Blends two images of the same size using a blending factor.
Definition: Blend.cs:217
Returns a color value from a string.
Definition: Color.cs:14
Handles bitmap-based graphs.
Definition: GraphBitmap.cs:13
Base class for graphs.
Definition: Graph.cs:79
static SKColor ToColor(object Object)
Converts an object to a color.
Definition: Graph.cs:828
Contains pixel information
abstract PixelInformationRaw GetRaw()
Gets raw pixel data.
byte[] Binary
Binary representation of pixels
static PixelInformation FromImage(SKImage Image)
Gets the pixel information from an SKImage.
Contains pixel information in a raw unencoded format.
SKColorType ColorType
How pixels are represented
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes3Scalar
Three scalar parameters.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20