Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RandomLinearRGB.cs
1using System;
2using SkiaSharp;
7
9{
19 {
25 {
26 }
27
32 : base(new ScriptNode[] { N, BandSize }, argumentTypes2Scalar, Start, Length, Expression)
33 {
34 }
35
40 : base(new ScriptNode[] { N, BandSize, Seed }, argumentTypes3Scalar, Start, Length, Expression)
41 {
42 }
43
51 {
52 int i = 0;
53 int c = Arguments.Length;
54 int N;
55 int BandSize;
56 int Seed;
57
58 if (i < c)
59 N = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
60 else
61 {
62 N = 1024;
63 Variables.ConsoleOut?.WriteLine("N = " + N.ToString(), Variables);
64 }
65
66 if (i < c)
67 BandSize = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
68 else
69 {
70 BandSize = 16;
71 Variables.ConsoleOut?.WriteLine("BandSize = " + BandSize.ToString(), Variables);
72 }
73
74 if (i < c)
75 Seed = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
76 else
77 {
78 lock (gen)
79 {
80 Seed = gen.Next();
81 }
82
83 Variables.ConsoleOut?.WriteLine("Seed = " + Seed.ToString(), Variables);
84 }
85
86 return new ObjectVector(CreatePalette(N, BandSize, Seed, this));
87 }
88
92 public override string[] DefaultArgumentNames
93 {
94 get
95 {
96 return new string[] { "N", "BandSize", "Seed" };
97 }
98 }
99
103 public static SKColor[] CreatePalette(int N, int BandSize, ScriptNode Node)
104 {
105 return CreatePalette(N, BandSize, null, Node);
106 }
107
111 public static SKColor[] CreatePalette(int N, int BandSize, int? Seed, ScriptNode Node)
112 {
113 if (N <= 0)
114 throw new ScriptRuntimeException("N in RandomLinearRGB(N[,BandSize]) has to be positive.", Node);
115
116 if (BandSize <= 0)
117 throw new ScriptRuntimeException("BandSize in RandomLinearRGB(N[,BandSize]) has to be positive.", Node);
118
119 SKColor[] Result = new SKColor[N];
120 int R1, G1, B1;
121 int R2, G2, B2;
122 int R, G, B;
123 int i, j, c, d;
124 int BandSize2 = BandSize / 2;
125 Random Generator;
126
127 if (Seed.HasValue)
128 Generator = new Random(Seed.Value);
129 else
130 Generator = gen;
131
132 lock (Generator)
133 {
134 R2 = Generator.Next(256);
135 G2 = Generator.Next(256);
136 B2 = Generator.Next(256);
137
138 i = 0;
139 while (i < N)
140 {
141 R1 = R2;
142 G1 = G2;
143 B1 = B2;
144
145 R2 = Generator.Next(256);
146 G2 = Generator.Next(256);
147 B2 = Generator.Next(256);
148
149 c = BandSize;
150 j = N - i;
151 if (c > j)
152 c = j;
153
154 d = N - i;
155 if (d > c)
156 d = c;
157
158 for (j = 0; j < d; j++)
159 {
160 R = ((R2 * j) + (R1 * (BandSize - j)) + BandSize2) / BandSize;
161 G = ((G2 * j) + (G1 * (BandSize - j)) + BandSize2) / BandSize;
162 B = ((B2 * j) + (B1 * (BandSize - j)) + BandSize2) / BandSize;
163
164 if (R > 255)
165 R = 255;
166
167 if (G > 255)
168 G = 255;
169
170 if (B > 255)
171 B = 255;
172
173 Result[i++] = new SKColor((byte)R, (byte)G, (byte)B);
174 }
175 }
176 }
177
178 return Result;
179 }
180
181 private static readonly Random gen = new Random();
182
186 public override string FunctionName => nameof(RandomLinearRGB);
187 }
188}
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
Calculates a palette of random color bands.
override string[] DefaultArgumentNames
Default Argument names
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
RandomLinearRGB(ScriptNode N, ScriptNode BandSize, int Start, int Length, Expression Expression)
TODO
static SKColor[] CreatePalette(int N, int BandSize, ScriptNode Node)
TODO
static SKColor[] CreatePalette(int N, int BandSize, int? Seed, ScriptNode Node)
TODO
RandomLinearRGB(ScriptNode N, ScriptNode BandSize, ScriptNode Seed, int Start, int Length, Expression Expression)
TODO
RandomLinearRGB(ScriptNode N, int Start, int Length, Expression Expression)
TODO
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes2Scalar
Two scalar parameters.
static readonly ArgumentType[] argumentTypes3Scalar
Three scalar parameters.
static readonly ArgumentType[] argumentTypes1Scalar
One scalar parameter.
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
TextWriter ConsoleOut
Console out interface. Can be used by functions and script to output data to the console.
Definition: Variables.cs:219
Basic interface for all types of elements.
Definition: IElement.cs:20