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