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