Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
QR.cs
1using System;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using SkiaSharp;
17
19{
23 public class QR : HttpSynchronousResource, IHttpGetMethod, IDisposable
24 {
25 private static readonly QrEncoder encoder = new QrEncoder();
26
30 public QR()
31 : base("/QR")
32 {
33 QrEncode.CustomColorFunction += this.QrEncode_CustomColorFunction;
34 }
35
36 private void QrEncode_CustomColorFunction(object Sender, ColorFunctionEventArgs e)
37 {
38 e.Function ??= GetColorFunction(e.Text);
39 }
40
46 public static ColorFunction GetColorFunction(string Text)
47 {
48 return GetColorFunction(Text, SKColors.Black, SKColors.White,
49 null, null, null, null, null, out _);
50 }
51
66 public static ColorFunction GetColorFunction(string Text, SKColor Foreground,
67 SKColor Background, SKColor? IconColor, SKColor? MarkerColor,
68 SKColor? MarkerOuterColor, SKColor? AlignmentColor, SKColor? AlignmentOuterColor,
69 out bool DoubleEncodeAtSign)
70 {
71 string UriScheme = null;
72 IconInfo Info = null;
73
74 DoubleEncodeAtSign = false;
75
76 while (Info is null)
77 {
78 if (string.IsNullOrEmpty(Text))
79 return null;
80
81 int i = Text.IndexOf(':');
82 if (i < 0)
83 return null;
84
85 UriScheme = Text[..i].ToLower();
86
87 switch (UriScheme)
88 {
89 case "iotid":
90 Info = userInfo;
91 DoubleEncodeAtSign = true;
92 break;
93
94 case "obinfo":
95 Info = onboardingInfo;
96 break;
97
98 case "edaler":
99 Info = eDalerInfo;
100 DoubleEncodeAtSign = true;
101 break;
102
103 case "aes256":
104 Info = aes256Info;
105 break;
106
107 case "tagsign":
108 Info = signatureInfo;
109 break;
110
111 case "iotsc":
112 Info = contractInfo;
113 DoubleEncodeAtSign = true;
114 break;
115
116 case "iotdisco":
117 Info = thingsInfo;
118 DoubleEncodeAtSign = true;
119 break;
120
121 case "nfeat":
122 Info = tokenInfo;
123 DoubleEncodeAtSign = true;
124 break;
125
126 case "http":
127 case "https":
128 try
129 {
130 Uri Uri = new Uri(Text);
131
132 if (!Gateway.IsDomain(Uri.Host, true))
133 return null;
134
135 if (string.IsNullOrEmpty(Uri.Query))
136 return null;
137
138 i = Uri.Query.ToLower().IndexOf("scheme=");
139 if (i < 0)
140 return null;
141
142 Text = Uri.Query[(i + 7)..];
143 i = Text.IndexOf('&');
144 if (i >= 0)
145 Text = Text[..i];
146
147 Text += ":";
148 }
149 catch (Exception)
150 {
151 return null;
152 }
153
154 UriScheme = null;
155 continue;
156
157 default:
158 return null;
159 }
160 }
161
162 IconColor ??= Info.Color;
163 MarkerColor ??= IconColor;
164 MarkerOuterColor ??= Blend.BlendColors(MarkerColor.Value, Foreground, 0.5);
165 AlignmentColor ??= IconColor;
166 AlignmentOuterColor ??= Blend.BlendColors(AlignmentColor.Value, Foreground, 0.5);
167
168 StringBuilder sb = new StringBuilder();
169
170 sb.Append(UriScheme);
171 sb.Append('|');
172 sb.Append(Foreground.ToString());
173 sb.Append('|');
174 sb.Append(Background.ToString());
175 sb.Append('|');
176 sb.Append(IconColor.Value.ToString());
177 sb.Append('|');
178 sb.Append(MarkerColor.Value.ToString());
179 sb.Append('|');
180 sb.Append(MarkerOuterColor.Value.ToString());
181 sb.Append('|');
182 sb.Append(AlignmentColor.Value.ToString());
183 sb.Append('|');
184 sb.Append(AlignmentOuterColor.Value.ToString());
185
186 string Key = sb.ToString();
187 CustomColoring Result;
188
189 lock (colorings)
190 {
191 if (!colorings.TryGetValue(Key, out Result))
192 {
193 Result = new CustomColoring(Info, IconColor.Value, Background,
194 Foreground, Background, MarkerOuterColor.Value, MarkerColor.Value,
195 Background, AlignmentOuterColor.Value, AlignmentColor.Value,
196 Background);
197
198 colorings[Key] = Result;
199 }
200 }
201
202 return Result.ColorFunction;
203 }
204
205 public void Dispose()
206 {
207 QrEncode.CustomColorFunction -= this.QrEncode_CustomColorFunction;
208 }
209
210 public bool AllowsGET => true;
211 public override bool HandlesSubPaths => true;
212 public override bool UserSessions => false;
213
214 public async Task GET(HttpRequest Request, HttpResponse Response)
215 {
216 SKColor Foreground = SKColors.Black;
217 SKColor Background = SKColors.White;
218 SKColor? IconColor = null;
219 SKColor? MarkerColor = null;
220 SKColor? MarkerOuterColor = null;
221 SKColor? AlignmentColor = null;
222 SKColor? AlignmentOuterColor = null;
223 ThemeDefinition CurrentTheme = null;
224 int Quality = 1;
225 int Width = 400;
226 int Height = 400;
227 string s;
228
229 SetTransparentCorsHeaders(this, Request, Response);
230
231 if (!(Request.Header.QueryParameters is null))
232 {
233 foreach (KeyValuePair<string, string> P in Request.Header.QueryParameters)
234 {
235 s = P.Value;
236
237 switch (P.Key)
238 {
239 case "w":
240 if (!int.TryParse(s, out Width) || Width <= 0 || Width > 2048)
241 throw new BadRequestException("Invalid width.");
242 break;
243
244 case "h":
245 if (!int.TryParse(s, out Height) || Height <= 0 || Height > 2048)
246 throw new BadRequestException("Invalid height.");
247 break;
248
249 case "fg":
250 if (string.Compare(s, "Theme", true) == 0)
251 {
252 CurrentTheme ??= Theme.GetCurrentTheme(Request.Session);
253 Foreground = CurrentTheme.TextColor;
254 }
255 else if (!Graph.TryConvertToColor(s, out Foreground))
256 throw new BadRequestException("Invalid Foreground color.");
257 break;
258
259 case "bg":
260 if (string.Compare(s, "Theme", true) == 0)
261 {
262 CurrentTheme ??= Theme.GetCurrentTheme(Request.Session);
263 Background = CurrentTheme.BackgroundColor;
264 }
265 else if (!Graph.TryConvertToColor(s, out Background))
266 throw new BadRequestException("Invalid Background color.");
267 break;
268
269 case "c":
270 if (!Graph.TryConvertToColor(s, out SKColor Color))
271 throw new BadRequestException("Invalid Icon color.");
272
273 IconColor = Color;
274 break;
275
276 case "mc":
277 if (!Graph.TryConvertToColor(s, out Color))
278 throw new BadRequestException("Invalid Marker color.");
279
280 MarkerColor = Color;
281 break;
282
283 case "omc":
284 if (!Graph.TryConvertToColor(s, out Color))
285 throw new BadRequestException("Invalid Outer Marker color.");
286
287 MarkerOuterColor = Color;
288 break;
289
290 case "ac":
291 if (!Graph.TryConvertToColor(s, out Color))
292 throw new BadRequestException("Invalid Alignment color.");
293
294 AlignmentColor = Color;
295 break;
296
297 case "oac":
298 if (!Graph.TryConvertToColor(s, out Color))
299 throw new BadRequestException("Invalid Outer Alignment color.");
300
301 AlignmentOuterColor = Color;
302 break;
303
304 case "q":
305 if (!int.TryParse(s, out Quality) || Quality < 1)
306 throw new BadRequestException("Invalid Quality.");
307 break;
308 }
309 }
310 }
311
312 if (Width * Quality > 8192 || Height * Quality > 8192)
313 throw new BadRequestException("Too large.");
314
315 if (string.IsNullOrEmpty(Request.SubPath))
316 throw new BadRequestException("No text to encode.");
317
318 string Text = System.Web.HttpUtility.UrlDecode(Request.SubPath[1..]);
319
320 using SKImage Bitmap = CreateQRCode(Text, Width, Height, Quality,
321 Foreground, Background, IconColor, MarkerColor, MarkerOuterColor,
322 AlignmentColor, AlignmentOuterColor);
323
324 await Response.Return(Bitmap);
325 }
326
332 public static SKImage CreateQRCode(string Text)
333 {
334 return CreateQRCode(Text, 400, 400);
335 }
336
344 public static SKImage CreateQRCode(string Text, int Width, int Height)
345 {
346 return CreateQRCode(Text, Width, Height, 2, SKColors.White, SKColors.Black,
347 null, null, null, null, null);
348 }
349
363 public static SKImage CreateQRCode(string Text, int Width, int Height, int Quality,
364 SKColor Foreground, SKColor Background, SKColor? IconColor, SKColor? MarkerColor,
365 SKColor? MarkerOuterColor, SKColor? AlignmentColor, SKColor? AlignmentOuterColor)
366 {
367 QrMatrix M;
368 byte[] Rgba;
369 ColorFunction f = GetColorFunction(Text, Foreground, Background, IconColor,
370 MarkerColor, MarkerOuterColor, AlignmentColor, AlignmentOuterColor,
371 out bool DoubleEncodeAtSign);
372
373 if (DoubleEncodeAtSign)
374 Text = Text.Replace("@", "%40");
375
376 if (f is null)
377 {
378 M = encoder.GenerateMatrix(CorrectionLevel.L, Text);
379 Rgba = M.ToRGBA(Width, Height);
380 }
381 else
382 {
383 M = encoder.GenerateMatrix(CorrectionLevel.H, Text);
384 Rgba = M.ToRGBA(Width, Height, f, Quality);
385 }
386
387 using SKData Data = SKData.Create(new MemoryStream(Rgba));
388
389 return SKImage.FromPixels(new SKImageInfo(Width, Height, SKColorType.Rgba8888), Data, Width * 4);
390 }
391
392 private class IconInfo
393 {
394 public byte[] IconBits;
395 public SKColor Color;
396 public int Width;
397 public int Height;
398
399 public IconInfo(SKColor Color, int Width, int Height, string Path)
400 {
401 this.Color = Color;
402 this.Width = Width;
403 this.Height = Height;
404
405 if (string.IsNullOrEmpty(Path))
406 this.IconBits = null;
407 else
408 {
409 using SKBitmap Icon = SvgPath.SvgPathToBitmap(Path, 256, 256, SKColors.White, SKColors.Transparent,
410 0, 0, 256f / Width, 256f / Height);
411
412 this.IconBits = new byte[8192];
413
414 int x, y;
415 byte Bit = 1;
416 byte Bits = 0;
417 int Pos = 0;
418
419 for (y = 0; y < 256; y++)
420 {
421 for (x = 0; x < 256; x++)
422 {
423 if (Icon.GetPixel(x, y).Alpha > 127)
424 Bits |= Bit;
425
426 Bit <<= 1;
427 if (Bit == 0)
428 {
429 this.IconBits[Pos++] = Bits;
430 Bit = 1;
431 Bits = 0;
432 }
433 }
434 }
435 }
436 }
437 };
438
439 private static readonly IconInfo userInfo = new IconInfo(SKColors.Red, 256, 256, "M128 21.3335C69.1202 21.3335 21.3335 69.1202 21.3335 128C21.3335 186.88 69.1202 234.667 128 234.667C186.88 234.667 234.667 186.88 234.667 128C234.667 69.1202 186.88 21.3335 128 21.3335ZM128 53.3335C145.707 53.3335 160 67.6268 160 85.3335C160 103.04 145.707 117.333 128 117.333C110.293 117.333 96.0002 103.04 96.0002 85.3335C96.0002 67.6268 110.293 53.3335 128 53.3335ZM128 204.8C101.333 204.8 77.7602 191.147 64.0002 170.453C64.3202 149.227 106.667 137.6 128 137.6C149.227 137.6 191.68 149.227 192 170.453C178.24 191.147 154.667 204.8 128 204.8Z");
440 private static readonly IconInfo onboardingInfo = new IconInfo(SKColors.Green, 256, 256, "M181.523 136.57H76.6327C68.3914 136.57 61.6484 143.313 61.6484 151.554V181.523C61.6484 189.764 68.3914 196.507 76.6327 196.507H181.523C189.764 196.507 196.507 189.764 196.507 181.523V151.554C196.507 143.313 189.764 136.57 181.523 136.57ZM91.617 181.523C83.3756 181.523 76.6327 174.78 76.6327 166.538C76.6327 158.297 83.3756 151.554 91.617 151.554C99.8583 151.554 106.601 158.297 106.601 166.538C106.601 174.78 99.8583 181.523 91.617 181.523ZM181.523 61.6484H76.6327C68.3914 61.6484 61.6484 68.3914 61.6484 76.6327V106.601C61.6484 114.843 68.3914 121.586 76.6327 121.586H181.523C189.764 121.586 196.507 114.843 196.507 106.601V76.6327C196.507 68.3914 189.764 61.6484 181.523 61.6484ZM91.617 106.601C83.3756 106.601 76.6327 99.8583 76.6327 91.617C76.6327 83.3756 83.3756 76.6327 91.617 76.6327C99.8583 76.6327 106.601 83.3756 106.601 91.617C106.601 99.8583 99.8583 106.601 91.617 106.601Z");
441 private static readonly IconInfo eDalerInfo = new IconInfo(SKColors.Goldenrod, 67, 67, "M24.6263 16.4401C23.3663 16.4401 22.1063 16.4401 20.8463 16.4401C20.7206 16.44 20.5948 16.4401 20.4395 16.4401V51.4389H20.9223C24.8438 51.4389 28.7656 51.4428 32.6871 51.4358C33.5212 51.4344 34.3558 51.4085 35.189 51.3681C37.4651 51.2581 39.6652 50.7828 41.772 49.9128C45.1668 48.511 47.8677 46.2772 49.6567 43.0516C52.1445 38.5658 52.5325 33.7874 51.288 28.8799C50.035 23.9385 47.0312 20.3848 42.3972 18.2388C39.5603 16.9252 36.548 16.4476 33.4461 16.4435C31.2191 16.4405 28.9924 16.44 26.7654 16.44C26.0525 16.44 25.3391 16.44 24.6263 16.4401ZM29.0057 23.7511C29.0906 23.7458 29.1635 23.7367 29.2363 23.7376C29.766 23.7444 30.2963 23.7432 30.8267 23.742C32.0051 23.7392 33.1835 23.7365 34.3556 23.8212C38.7074 24.1359 42.1845 26.6075 43.0292 31.4776C43.4386 33.8372 43.3465 36.1684 42.4712 38.415C41.2914 41.4435 38.9755 43.1121 35.8589 43.7698C34.4877 44.0592 33.0964 44.0526 31.7052 44.0495C30.8097 44.0475 29.914 44.0495 28.989 44.0495V37.1036H40.4938V30.4922H29.0057V23.7514V23.7511Z");
442 private static readonly IconInfo aes256Info = new IconInfo(SKColors.SlateGray, 68, 67, "M44.2122 28.1067H42.5456V24.7733C42.5456 20.1733 38.8122 16.44 34.2122 16.44C29.6122 16.44 25.8789 20.1733 25.8789 24.7733V28.1067H24.2122C22.3789 28.1067 20.8789 29.6067 20.8789 31.44V48.1067C20.8789 49.94 22.3789 51.44 24.2122 51.44H44.2122C46.0456 51.44 47.5456 49.94 47.5456 48.1067V31.44C47.5456 29.6067 46.0456 28.1067 44.2122 28.1067ZM34.2122 43.1067C32.3789 43.1067 30.8789 41.6067 30.8789 39.7733C30.8789 37.94 32.3789 36.44 34.2122 36.44C36.0456 36.44 37.5456 37.94 37.5456 39.7733C37.5456 41.6067 36.0456 43.1067 34.2122 43.1067ZM29.2122 28.1067V24.7733C29.2122 22.0067 31.4456 19.7733 34.2122 19.7733C36.9789 19.7733 39.2122 22.0067 39.2122 24.7733V28.1067H29.2122Z");
443 private static readonly IconInfo signatureInfo = new IconInfo(SKColors.SlateGray, 67, 67, "M34.1818 29.2727C32.6909 25.0364 28.6545 22 23.9091 22C17.8909 22 13 26.8909 13 32.9091C13 38.9273 17.8909 43.8182 23.9091 43.8182C28.6545 43.8182 32.6909 40.7818 34.1818 36.5455H42.0909V43.8182H49.3636V36.5455H53V29.2727H34.1818ZM23.9091 36.5455C21.9091 36.5455 20.2727 34.9091 20.2727 32.9091C20.2727 30.9091 21.9091 29.2727 23.9091 29.2727C25.9091 29.2727 27.5455 30.9091 27.5455 32.9091C27.5455 34.9091 25.9091 36.5455 23.9091 36.5455Z");
444 private static readonly IconInfo contractInfo = new IconInfo(SKColors.RoyalBlue, 68, 67, "M34.3809 61.44C49.5687 61.44 61.8809 49.1278 61.8809 33.94C61.8809 18.7522 49.5687 6.44 34.3809 6.44C19.193 6.44 6.88086 18.7522 6.88086 33.94C6.88086 49.1278 19.193 61.44 34.3809 61.44ZM27.0428 16.44H41.0432L51.5435 26.94V47.94C51.5435 49.865 49.9684 51.44 48.0434 51.44H27.0253C25.1002 51.44 23.5427 49.865 23.5427 47.94L23.5502 35.89H22.0197V45.1152C22.0197 45.9724 21.5061 46.7396 20.7122 47.0699L19.661 47.5068C19.6621 47.5273 19.6649 47.5473 19.6678 47.5673C19.6714 47.5927 19.675 47.618 19.675 47.6442C19.675 49.032 18.5405 50.1608 17.1458 50.1608C15.7512 50.1608 14.6167 49.032 14.6167 47.6442C14.6167 46.2565 15.7512 45.1276 17.1458 45.1276C17.8867 45.1276 18.5474 45.452 19.0105 45.9584L20.0628 45.5212C20.2273 45.4526 20.3336 45.2935 20.3336 45.1149L20.3336 35.8897H18.7837C18.4346 36.8641 17.5074 37.5675 16.41 37.5675C15.0153 37.5675 13.8809 36.4386 13.8809 35.0509C13.8809 33.6631 15.0153 32.5343 16.41 32.5343C17.5077 32.5343 18.4346 33.2376 18.7837 34.212H20.3336L20.3336 24.9868C20.3336 24.8082 20.2267 24.6485 20.0618 24.58L19.0099 24.1427C18.5471 24.6491 17.8864 24.9732 17.1455 24.9732C15.7509 24.9732 14.6164 23.8443 14.6164 22.4566C14.6164 21.0688 15.7509 19.94 17.1455 19.94C18.5402 19.94 19.6747 21.0688 19.6747 22.4566C19.6747 22.4832 19.6711 22.5087 19.6675 22.5342L19.6675 22.5342C19.6646 22.5543 19.6618 22.5743 19.6607 22.5949L20.7119 23.0322C21.5064 23.3625 22.0194 24.13 22.0194 24.9869L22.0197 34.212H23.5513L23.5602 19.94C23.5602 18.015 25.1177 16.44 27.0428 16.44ZM30.5429 44.44H44.5433V40.94H30.5429V44.44ZM30.5429 37.44H44.5433V33.94H30.5429V37.44ZM39.2931 19.065V28.69H48.9184L39.2931 19.065Z");
445 private static readonly IconInfo thingsInfo = new IconInfo(SKColors.DarkOrchid, 67, 67, "M33.5 61C48.6878 61 61 48.6878 61 33.5C61 18.3122 48.6878 6 33.5 6C18.3122 6 6 18.3122 6 33.5C6 48.6878 18.3122 61 33.5 61ZM39.02 22.146V25.292C39.02 26.157 38.312 26.865 37.447 26.865H24.864C23.998 26.865 23.291 26.157 23.291 25.292V22.146H20.144V47.312H26.484C24.565 45.881 23.29 43.601 23.29 41.021V33.156H39.021V41.021C39.021 43.6 37.762 45.881 35.828 47.312H42.166V50.458H20.146C19.3124 50.4556 18.5135 50.1234 17.9241 49.5339C17.3346 48.9445 17.0024 48.1456 17 47.312V22.146C17.0024 21.3124 17.3346 20.5135 17.9241 19.9241C18.5135 19.3346 19.3124 19.0024 20.146 19H42.167V22.146H39.02ZM32.2683 31.1223C31.9733 31.4173 31.5732 31.583 31.156 31.583C30.7388 31.583 30.3387 31.4173 30.0437 31.1223C29.7487 30.8273 29.583 30.4272 29.583 30.01C29.583 29.5928 29.7487 29.1927 30.0437 28.8977C30.3387 28.6027 30.7388 28.437 31.156 28.437C31.5732 28.437 31.9733 28.6027 32.2683 28.8977C32.5633 29.1927 32.729 29.5928 32.729 30.01C32.729 30.4272 32.5633 30.8273 32.2683 31.1223ZM44.64 30.2C44.02 30.144 43.553 29.616 43.553 28.99C43.5517 28.8177 43.5867 28.6471 43.6558 28.4892C43.7249 28.3314 43.8265 28.1899 43.954 28.074C44.0815 27.9581 44.232 27.8704 44.3957 27.8167C44.5594 27.7629 44.7326 27.7443 44.904 27.762C51.284 28.406 56.355 33.478 56.994 39.852C57.067 40.576 56.49 41.202 55.766 41.202C55.14 41.202 54.611 40.723 54.55 40.097C54.028 34.872 49.865 30.709 44.64 30.2ZM43.553 40.324V38.52V38.519C43.553 37.942 44.118 37.512 44.659 37.672C45.221 37.8529 45.7318 38.165 46.1491 38.5825C46.5665 39 46.8784 39.511 47.059 40.073C47.237 40.631 46.801 41.19 46.224 41.19H44.419C43.94 41.19 43.553 40.804 43.553 40.324ZM44.597 35.142C43.995 35.044 43.553 34.54 43.553 33.939C43.553 33.19 44.198 32.594 44.929 32.711C46.7099 33.001 48.3546 33.8436 49.6305 35.1195C50.9064 36.3954 51.749 38.0401 52.039 39.821C52.161 40.546 51.56 41.196 50.823 41.196H50.811C50.209 41.196 49.712 40.748 49.607 40.152C49.3927 38.9013 48.7958 37.7478 47.8985 36.8505C47.0012 35.9532 45.8477 35.3563 44.597 35.142Z");
446 private static readonly IconInfo tokenInfo = new IconInfo(SKColors.LightSeaGreen, 67, 67, "M37.1248 33.9083C37.1248 36.5422 34.2731 38.1878 31.9928 36.8719C30.9346 36.261 30.2824 35.1302 30.2824 33.9083C30.2824 31.2752 33.1334 29.6288 35.414 30.9455C36.4722 31.5566 37.1248 32.6862 37.1248 33.9083ZM28.6626 29.0916L19.9302 24.275C23.216 22.4293 30.2019 18.5135 31.8584 17.6126C33.5152 16.7126 34.9195 17.298 35.5497 17.6126L47.5231 24.275L38.7459 29.0916C34.5676 25.2384 30.2824 27.4855 28.6626 29.0916ZM49.3239 40.9307C49.3087 41.4554 49.018 42.6585 47.9731 43.2705C46.929 43.8825 39.2256 48.208 35.505 50.2929V40.6153C37.5758 40.1197 41.4641 37.7434 40.4562 32.1974L49.3239 27.2457V40.9307ZM18.129 27.2457L26.9972 32.1974C25.9888 37.7434 29.8769 40.1197 31.9483 40.6153V50.2929C28.2279 48.208 20.5242 43.8825 19.4802 43.2705C18.4348 42.6585 18.1441 41.4554 18.129 40.9307V27.2457ZM6 33.4999C6 48.6881 18.3119 61 33.5001 61C48.6884 61 61 48.6881 61 33.4999C61 18.3116 48.6884 6 33.5001 6C18.3119 6 6 18.3116 6 33.4999Z");
447
448 private static readonly Dictionary<string, CustomColoring> colorings = new Dictionary<string, CustomColoring>();
449
450 private class CustomColoring
451 {
452 private readonly byte[] iconBits;
453 private readonly uint iconFg;
454 private readonly uint iconBg;
455 private readonly uint codeFg;
456 private readonly uint codeBg;
457 private readonly uint markerOuterFg;
458 private readonly uint markerInnerFg;
459 private readonly uint markerBg;
460 private readonly uint alignmentOuterFg;
461 private readonly uint alignmentInnerFg;
462 private readonly uint alignmentBg;
463 private readonly bool hasIcon;
464
465 public CustomColoring(IconInfo Icon, SKColor IconFg, SKColor IconBg,
466 SKColor CodeFg, SKColor CodeBg, SKColor MarkerOuterFg, SKColor MarkerInnerFg,
467 SKColor MarkerBg, SKColor AlignmentOuterFg, SKColor AlignmentInnerFg,
468 SKColor AlignmentBg)
469 {
470 this.iconBits = Icon.IconBits;
471 this.hasIcon = !(this.iconBits is null);
472 this.iconFg = ToUInt(IconFg);
473 this.iconBg = ToUInt(IconBg);
474 this.codeFg = ToUInt(CodeFg);
475 this.codeBg = ToUInt(CodeBg);
476 this.markerOuterFg = ToUInt(MarkerOuterFg);
477 this.markerInnerFg = ToUInt(MarkerInnerFg);
478 this.markerBg = ToUInt(MarkerBg);
479 this.alignmentOuterFg = ToUInt(AlignmentOuterFg);
480 this.alignmentInnerFg = ToUInt(AlignmentInnerFg);
481 this.alignmentBg = ToUInt(AlignmentBg);
482 }
483
484 private static uint ToUInt(SKColor Color)
485 {
486 uint Result = Color.Alpha;
487
488 Result <<= 8;
489 Result |= Color.Blue;
490 Result <<= 8;
491 Result |= Color.Green;
492 Result <<= 8;
493 Result |= Color.Red;
494
495 return Result;
496 }
497
498 public uint ColorFunction(float CodeX, float CodeY, float DotX, float DotY, DotType Type)
499 {
500 float dx, dy, d2;
501
502 if (this.hasIcon)
503 {
504 dx = CodeX - 0.5f;
505 dy = CodeY - 0.5f;
506 d2 = dx * dx + dy * dy;
507
508 if (d2 < 0.01)
509 {
510 int IconX = (int)(128 + 1024 * dx + 0.5f);
511 int IconY = (int)(128 + 1024 * dy + 0.5f);
512
513 if (IconX >= 0 && IconX < 256 && IconY >= 0 && IconY < 256)
514 {
515 IconY <<= 5;
516 IconY += IconX >> 3;
517
518 if ((this.iconBits[IconY] & (1 << (IconX & 7))) != 0)
519 return this.iconFg;
520 else
521 return this.iconBg;
522 }
523 }
524
525 if (d2 < 0.015)
526 return this.codeBg;
527 }
528
529 switch (Type)
530 {
531 case DotType.CodeBackground:
532 default:
533 return this.codeBg;
534
535 case DotType.CodeForeground:
536 dx = DotX - 0.5f;
537 dy = DotY - 0.5f;
538 d2 = dx * dx + dy * dy;
539
540 if (d2 <= 0.25f)
541 return this.codeFg;
542 else
543 return this.codeBg;
544
545 case DotType.FinderMarkerBackground:
546 return this.markerBg;
547
548 case DotType.FinderMarkerForegroundOuter:
549 return this.markerOuterFg;
550
551 case DotType.FinderMarkerForegroundInner:
552 return this.markerInnerFg;
553
554 case DotType.AlignmentMarkerBackground:
555 return this.alignmentBg;
556
557 case DotType.AlignmentMarkerForegroundOuter:
558 return this.alignmentOuterFg;
559
560 case DotType.AlignmentMarkerForegroundInner:
561 return this.alignmentInnerFg;
562 }
563 }
564 }
565
566 }
567}
Processes SVG paths.
Definition: SvgPath.cs:18
static SKBitmap SvgPathToBitmap(string SvgPath, int Width, int Height, SKColor Foreground, SKColor Background)
Parses an SVG Path and generates a bitmap image from it.
Definition: SvgPath.cs:84
Class used to compute a QR code matrix.
Definition: QrMatrix.cs:29
byte[] ToRGBA()
Converts the matrix to pixels, each pixel represented by 4 bytes in the order Red,...
Definition: QrMatrix.cs:867
QR Code encoder.
Definition: QrEncoder.cs:12
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static bool IsDomain(string DomainOrHost, bool IncludeAlternativeDomains)
If a domain or host name represents the gateway.
Definition: Gateway.cs:5174
static ThemeDefinition GetCurrentTheme(SessionVariables Variables)
Gets the current theme definition, based on the host information available in the session varaibles.
Definition: Theme.cs:71
Contains properties for a theme.
SKColor TextColor
Color for normal text.
SKColor BackgroundColor
Background Color for normal text.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
KeyValuePair< string, string >[] QueryParameters
All query parameters.
Represents an HTTP request.
Definition: HttpRequest.cs:22
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:182
SessionVariables Session
Contains session states, if the resource requires sessions, or null otherwise.
Definition: HttpRequest.cs:212
string SubPath
Sub-path. If a resource is found handling the request, this property contains the trailing sub-path o...
Definition: HttpRequest.cs:194
static void SetTransparentCorsHeaders(HttpResource Resource, HttpRequest Request, HttpResponse Response)
Sets CORS headers for a resource, allowing it to be embedded in other sites.
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Return(Exception ex)
Returns an error to the client.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
Event arguments for events determoning if custom color coding of a QR code is to be applied.
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
static SKColor BlendColors(SKColor c1, SKColor c2, double p)
Blends two colors using a blending factor.
Definition: Blend.cs:125
Returns a color value from a string.
Definition: Color.cs:14
Base class for graphs.
Definition: Graph.cs:88
static bool TryConvertToColor(object Object, out SKColor Color)
Tries to convert an object to a color.
Definition: Graph.cs:822
Web Service, generating QR-codes based on URI input.
Definition: QR.cs:24
static SKImage CreateQRCode(string Text, int Width, int Height)
Creates a QR-Code image.
Definition: QR.cs:344
static ColorFunction GetColorFunction(string Text)
Selects a color function for a qr-code, based on the URI-schema of the code.
Definition: QR.cs:46
static SKImage CreateQRCode(string Text, int Width, int Height, int Quality, SKColor Foreground, SKColor Background, SKColor? IconColor, SKColor? MarkerColor, SKColor? MarkerOuterColor, SKColor? AlignmentColor, SKColor? AlignmentOuterColor)
Creates a QR-Code image.
Definition: QR.cs:363
async Task GET(HttpRequest Request, HttpResponse Response)
Executes the GET method on the resource.
Definition: QR.cs:214
bool AllowsGET
If the GET method is allowed.
Definition: QR.cs:210
static ColorFunction GetColorFunction(string Text, SKColor Foreground, SKColor Background, SKColor? IconColor, SKColor? MarkerColor, SKColor? MarkerOuterColor, SKColor? AlignmentColor, SKColor? AlignmentOuterColor, out bool DoubleEncodeAtSign)
Selects a color function for a qr-code, based on the URI-schema of the code.
Definition: QR.cs:66
static SKImage CreateQRCode(string Text)
Creates a QR-Code image.
Definition: QR.cs:332
QR()
Web Service, generating QR-codes based on URI input.
Definition: QR.cs:30
GET Interface for HTTP resources.
Definition: ImplTypes.g.cs:58
delegate uint ColorFunction(float CodeX, float CodeY, float DotX, float DotY, DotType Type)
Delegate for QR-code color functions
CorrectionLevel
QR Code correction level.
Definition: Enumerations.cs:7
DotType
Type of dot in code.
Definition: Enumerations.cs:64