Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RandomLinearAnalogousHSL.cs
1using System;
2using SkiaSharp;
8
10{
25 {
31 {
32 }
33
38 : base(new ScriptNode[] { N, BandSize }, argumentTypes2Scalar, Start, Length, Expression)
39 {
40 }
41
46 : base(new ScriptNode[] { N, BandSize, Seed }, argumentTypes3Scalar, Start, Length, Expression)
47 {
48 }
49
57 {
58 int i = 0;
59 int c = Arguments.Length;
60 int N;
61 int BandSize;
62 int Seed;
63
64 if (i < c)
65 N = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
66 else
67 {
68 N = 1024;
69 Variables.ConsoleOut?.WriteLine("N = " + N.ToString(), Variables);
70 }
71
72 if (i < c)
73 BandSize = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
74 else
75 {
76 BandSize = 16;
77 Variables.ConsoleOut?.WriteLine("BandSize = " + BandSize.ToString(), Variables);
78 }
79
80 if (i < c)
81 Seed = (int)Expression.ToDouble(Arguments[i++].AssociatedObjectValue);
82 else
83 {
84 lock (gen)
85 {
86 Seed = gen.Next();
87 }
88
89 Variables.ConsoleOut?.WriteLine("Seed = " + Seed.ToString(), Variables);
90 }
91
92 return new ObjectVector(CreatePalette(N, BandSize, Seed, this));
93 }
94
98 public override string[] DefaultArgumentNames
99 {
100 get
101 {
102 return new string[] { "N", "BandSize", "Seed" };
103 }
104 }
105
109 public static SKColor[] CreatePalette(int N, int BandSize, ScriptNode Node)
110 {
111 return CreatePalette(N, BandSize, null, Node);
112 }
113
117 public static SKColor[] CreatePalette(int N, int BandSize, out int Seed, ScriptNode Node, Variables Variables)
118 {
119 lock (gen)
120 {
121 Seed = gen.Next();
122 }
123
124 Variables?.ConsoleOut?.WriteLine("Seed = " + Seed.ToString(), Variables);
125
126 return CreatePalette(N, BandSize, Seed, Node);
127 }
128
132 public static SKColor[] CreatePalette(int N, int BandSize, int? Seed, ScriptNode Node)
133 {
134 if (N <= 0)
135 throw new ScriptRuntimeException("N in RandomLinearAnalogousHSL(N[,BandSize]) has to be positive.", Node);
136
137 if (BandSize <= 0)
138 throw new ScriptRuntimeException("BandSize in RandomLinearAnalogousHSL(N[,BandSize]) has to be positive.", Node);
139
140 SKColor[] Result = new SKColor[N];
141 double H, S, L;
142 int R1, G1, B1;
143 int R2, G2, B2;
144 int R, G, B;
145 int i, j, c, d;
146 int BandSize2 = BandSize / 2;
147 Random Generator;
148
149 if (Seed.HasValue)
150 Generator = new Random(Seed.Value);
151 else
152 Generator = gen;
153
154 lock (Generator)
155 {
156 H = Generator.NextDouble() * 360;
157 S = Generator.NextDouble();
158 L = Generator.NextDouble();
159
160 SKColor cl = Graph.ToColorHSL(H, S, L);
161 R2 = cl.Red;
162 G2 = cl.Green;
163 B2 = cl.Blue;
164
165 i = 0;
166 while (i < N)
167 {
168 R1 = R2;
169 G1 = G2;
170 B1 = B2;
171
172 H += Generator.NextDouble() * 120 - 60;
173 S = Generator.NextDouble();
174 L = Generator.NextDouble();
175
176 cl = Graph.ToColorHSL(H, S, L);
177 R2 = cl.Red;
178 G2 = cl.Green;
179 B2 = cl.Blue;
180
181 c = BandSize;
182 j = N - i;
183 if (c > j)
184 c = j;
185
186 d = N - i;
187 if (d > c)
188 d = c;
189
190 for (j = 0; j < d; j++)
191 {
192 R = ((R2 * j) + (R1 * (BandSize - j)) + BandSize2) / BandSize;
193 G = ((G2 * j) + (G1 * (BandSize - j)) + BandSize2) / BandSize;
194 B = ((B2 * j) + (B1 * (BandSize - j)) + BandSize2) / BandSize;
195
196 if (R > 255)
197 R = 255;
198
199 if (G > 255)
200 G = 255;
201
202 if (B > 255)
203 B = 255;
204
205 Result[i++] = new SKColor((byte)R, (byte)G, (byte)B);
206 }
207 }
208 }
209
210 return Result;
211 }
212
213 private static readonly Random gen = new Random();
214
218 public override string FunctionName => nameof(RandomLinearAnalogousHSL);
219 }
220}
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
static SKColor[] CreatePalette(int N, int BandSize, out int Seed, ScriptNode Node, Variables Variables)
TODO
static SKColor[] CreatePalette(int N, int BandSize, int? Seed, ScriptNode Node)
TODO
static SKColor[] CreatePalette(int N, int BandSize, ScriptNode Node)
TODO
RandomLinearAnalogousHSL(ScriptNode N, int Start, int Length, Expression Expression)
TODO
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
RandomLinearAnalogousHSL(ScriptNode N, ScriptNode BandSize, int Start, int Length, Expression Expression)
TODO
RandomLinearAnalogousHSL(ScriptNode N, ScriptNode BandSize, ScriptNode Seed, int Start, int Length, Expression Expression)
TODO
Base class for graphs.
Definition: Graph.cs:79
static SKColor ToColorHSL(double H, double S, double L)
Creates a Color from its HSL representation.
Definition: Graph.cs:846
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