summary refs log tree commit diff stats
path: root/c/nucleotide-count/test/test_nucleotide_count.c
blob: 7691fb78ac7e50f89ade1c7d5aebdbc22bd06e3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "vendor/unity.h"
#include "../src/nucleotide_count.h"
#include <stdlib.h>
#include <string.h>

void setUp(void)
{
}

void tearDown(void)
{
}
#include <stdio.h>
static void test_strand_count(const char *dna_strand, const char *expected)
{
   char *actual_count = count(dna_strand);

   TEST_ASSERT_TRUE(strcmp(actual_count, expected) == 0);
   free(actual_count);
}

static void test_empty_strand(void)
{
   const char *dna_strand = "";
   const char *expected = "A:0 C:0 G:0 T:0";

   test_strand_count(dna_strand, expected);
}

static void test_repeated_nucleotide(void)
{
   const char *dna_strand = "GGGGGGG";
   const char *expected = "A:0 C:0 G:7 T:0";

   test_strand_count(dna_strand, expected);
}

static void test_multiple_nucleotides(void)
{
   const char *dna_strand =
       "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC";
   const char *expected = "A:20 C:12 G:17 T:21";

   test_strand_count(dna_strand, expected);
}

static void test_invalid_nucleotide(void)
{
   const char *dna_strand = "AGXXACT";
   const char *expected = "";

   test_strand_count(dna_strand, expected);
}

int main(void)
{
   UnityBegin("test/test_nucleotide_count.c");

   RUN_TEST(test_empty_strand);
   RUN_TEST(test_repeated_nucleotide);
   RUN_TEST(test_multiple_nucleotides);
   RUN_TEST(test_invalid_nucleotide);

   return UnityEnd();
}
e efficient display algorithms' href='/ahoang/chawan/commit/src/types/color.nim?id=559d2ca622e15c5781303940bc0c87e9aed1e5d1'>559d2ca6 ^
911c2dd1 ^

f11c9425 ^
940325ab ^
c1bcb8a4 ^
940325ab ^
cd1a3641 ^


940325ab ^
f11c9425 ^
940325ab ^
911c2dd1 ^
f11c9425 ^
29c5a501 ^
f11c9425 ^



29c5a501 ^

f11c9425 ^
940325ab ^
f11c9425 ^
e46f0a4c ^
29c5a501 ^








940e6516 ^
19068fd7 ^


























































































































































ca81bbce ^
911c2dd1 ^
c1bcb8a4 ^
77512303 ^
911c2dd1 ^
c1bcb8a4 ^
77512303 ^
911c2dd1 ^
c1bcb8a4 ^
77512303 ^
911c2dd1 ^
c1bcb8a4 ^
1769bc42 ^
911c2dd1 ^

1769bc42 ^
911c2dd1 ^

1769bc42 ^
911c2dd1 ^

1769bc42 ^
911c2dd1 ^

77512303 ^
8027e52c ^
911c2dd1 ^
8027e52c ^
45f58316 ^




8027e52c ^



911c2dd1 ^

8027e52c ^

91707a9b ^
8027e52c ^



























911c2dd1 ^

8027e52c ^
911c2dd1 ^

8027e52c ^
01ce4740 ^

911c2dd1 ^
01ce4740 ^

911c2dd1 ^
01ce4740 ^
9e0a2ace ^


911c2dd1 ^
8027e52c ^
911c2dd1 ^
8027e52c ^


b2514e94 ^
c1bcb8a4 ^



9e0a2ace ^
8027e52c ^


fb36a13c ^

77512303 ^
fb36a13c ^
c1bcb8a4 ^
77512303 ^
fb36a13c ^
c1bcb8a4 ^
77512303 ^
fb36a13c ^
c1bcb8a4 ^
77512303 ^

fb36a13c ^



c1bcb8a4 ^
fb36a13c ^




c1bcb8a4 ^
fb36a13c ^




c1bcb8a4 ^
fb36a13c ^

f981b996 ^


77512303 ^


c1bcb8a4 ^
f94439ca ^
911c2dd1 ^

f94439ca ^
77512303 ^
7f66b5eb ^





911c2dd1 ^
fb36a13c ^
8027e52c ^

7f66b5eb ^



8027e52c ^
29c5a501 ^
f11c9425 ^
29c5a501 ^




c1bcb8a4 ^
29c5a501 ^
c1bcb8a4 ^

29c5a501 ^




f11c9425 ^
29c5a501 ^










c1bcb8a4 ^
29c5a501 ^
c1bcb8a4 ^
29c5a501 ^

412dafe1 ^


77512303 ^
f11c9425 ^
911c2dd1 ^
77512303 ^


911c2dd1 ^
652251c2 ^
911c2dd1 ^
652251c2 ^

a3bd24ae ^

b086e346 ^
652251c2 ^
911c2dd1 ^
652251c2 ^




911c2dd1 ^
652251c2 ^
a3bd24ae ^

652251c2 ^

911c2dd1 ^
652251c2 ^




911c2dd1 ^
652251c2 ^

911c2dd1 ^
652251c2 ^
911c2dd1 ^
652251c2 ^




ca81bbce ^
a62a7077 ^
7ab7f28f ^
9991bd33 ^
7ab7f28f ^







9991bd33 ^
7ab7f28f ^




d54e0258 ^


7ab7f28f ^





1e858c87 ^







9991bd33 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
                  
                 
 

                   
    
                             
 

                                                                           
                              
 






                                                        



                                 


                                                        
                  

                         
                     
                
             
 

                                        
                                          
                                            
 

                                                
 


                                                   
                                            
 

                                            

                                                

                                            
 

                                              
 
                                      
                
 


                                                 
                                           
                                                             
 
                                             
                                             
 



                                           

                                              
                                           
 
                                                
 








                                
 


























































































































































                                     
 
                              
                                
 
                              
                               
 
                              
                         
 
                              
                                
 

                                                
 

                                               
 

                                        
 

                                                
 
                                                        
                                           
                    




                        



                                                                             

                                         

                                      
                                      



























                                      

                                                    
 

                                                   
 

                                                                           
                                        

              
                       
                    


                                                              
                                                              
 
                                           


                       
                                   



                       
                                 


                          

                                                                       
 
                             
                                
 
                             
                               
 
                             
                         

                                                                                              



                                 
                                                       




                                 
                                                        




                                 
                                                        

                                     


                      


                                                                     
                                          
 

                                                              
                                   
 





                                               
                                        
                                                     

                                



                                      
 
                                                              
                                              




                                        
                                            
                         

                                          




                              
                                                










                                                                  
                                                             
                          
                                                        

                                           


                                                                      
                                         
                      
                    


                       
                                                   
             
                                                

            

                                                                 
                                                                
                                                    
                             




                                                                 
                             
       

                                                                 

                                                                
                             




                                                                 
                             

               
                                                    
                    
                                        




                                                                            
 
                                              
                    
                       







                                             
                        




                             


                             





                                         







                                                           
                    
import std/options
import std/tables

import utils/twtstr

type
  RGBColor* = distinct uint32

  # ARGB color. machine-dependent format, so that bit shifts and arithmetic
  # works. (Alpha is MSB, then come R, G, B.)
  ARGBColor* = distinct uint32

  # RGBA format; machine-independent, always big-endian.
  RGBAColorBE* {.packed.} = object
    r*: uint8
    g*: uint8
    b*: uint8
    a*: uint8

  ANSIColor* = distinct uint8

  EightBitColor* = distinct uint8

  # ctNone: default color (intentionally 0), n is unused
  # ctANSI: ANSI color, as selected by SGR 38/48
  # ctRGB: RGB color
  ColorTag* = enum
    ctNone, ctANSI, ctRGB

  CellColor* = object
    t*: ColorTag
    n: uint32

func rgba*(r, g, b, a: uint8): ARGBColor

func toRGBColor*(i: ARGBColor): RGBColor =
  return RGBColor(uint32(i) and 0xFFFFFFu32)

converter toARGBColor*(i: RGBColor): ARGBColor =
  return ARGBColor(uint32(i) or 0xFF000000u32)

converter toARGBColor*(c: RGBAColorBE): ARGBColor =
  return rgba(c.r, c.g, c.b, c.a)

func `==`*(a, b: ARGBColor): bool {.borrow.}

func `==`*(a, b: ANSIColor): bool {.borrow.}

func `==`*(a, b: EightBitColor): bool {.borrow.}

func rgbcolor*(color: CellColor): RGBColor =
  cast[RGBColor](color.n)

func argbcolor*(color: CellColor): ARGBColor =
  cast[ARGBColor](color.n)

func color*(color: CellColor): uint8 =
  uint8(color.n)

func eightbit*(color: CellColor): EightBitColor =
  EightBitColor(color.color)

func cellColor*(rgb: RGBColor): CellColor =
  return CellColor(t: ctRGB, n: uint32(rgb) or 0xFF000000u32)

func cellColor*(rgba: ARGBColor): CellColor =
  return CellColor(t: ctRGB, n: uint32(rgba))

#TODO bright ANSI colors (8..15)

func cellColor*(c: ANSIColor): CellColor =
  return CellColor(t: ctANSI, n: uint32(c))

func cellColor*(c: EightBitColor): CellColor =
  return CellColor(t: ctANSI, n: uint32(c))

const defaultColor* = CellColor(t: ctNone, n: 0)

const
  ANSI_BLACK* = ANSIColor(0u8)
  ANSI_RED* = ANSIColor(1u8)
  ANSI_GREEN* = ANSIColor(2u8)
  ANSI_YELLOW* = ANSIColor(3u8)
  ANSI_BLUE* = ANSIColor(4u8)
  ANSI_MAGENTA* = ANSIColor(5u8)
  ANSI_CYAN* = ANSIColor(6u8)
  ANSI_WHITE* = ANSIColor(7u8)

const ColorsRGB* = block:
  const ColorsRGBMap = {
    "aliceblue": 0xf0f8ff,
    "antiquewhite": 0xfaebd7,
    "aqua": 0x00ffff,
    "aquamarine": 0x7fffd4,
    "azure": 0xf0ffff,
    "beige": 0xf5f5dc,
    "bisque": 0xffe4c4,
    "black": 0x000000,
    "blanchedalmond": 0xffebcd,
    "blue": 0x0000ff,
    "blueviolet": 0x8a2be2,
    "brown": 0xa52a2a,
    "burlywood": 0xdeb887,
    "cadetblue": 0x5f9ea0,
    "chartreuse": 0x7fff00,
    "chocolate": 0xd2691e,
    "coral": 0xff7f50,
    "cornflowerblue": 0x6495ed,
    "cornsilk": 0xfff8dc,
    "crimson": 0xdc143c,
    "cyan": 0x00ffff,
    "darkblue": 0x00008b,
    "darkcyan": 0x008b8b,
    "darkgoldenrod": 0xb8860b,
    "darkgray": 0xa9a9a9,
    "darkgreen": 0x006400,
    "darkgrey": 0xa9a9a9,
    "darkkhaki": 0xbdb76b,
    "darkmagenta": 0x8b008b,
    "darkolivegreen": 0x556b2f,
    "darkorange": 0xff8c00,
    "darkorchid": 0x9932cc,
    "darkred": 0x8b0000,
    "darksalmon": 0xe9967a,
    "darkseagreen": 0x8fbc8f,
    "darkslateblue": 0x483d8b,
    "darkslategray": 0x2f4f4f,
    "darkslategrey": 0x2f4f4f,
    "darkturquoise": 0x00ced1,
    "darkviolet": 0x9400d3,
    "deeppink": 0xff1493,
    "deepskyblue": 0x00bfff,
    "dimgray": 0x696969,
    "dimgrey": 0x696969,
    "dodgerblue": 0x1e90ff,
    "firebrick": 0xb22222,
    "floralwhite": 0xfffaf0,
    "forestgreen": 0x228b22,
    "fuchsia": 0xff00ff,
    "gainsboro": 0xdcdcdc,
    "ghostwhite": 0xf8f8ff,
    "gold": 0xffd700,
    "goldenrod": 0xdaa520,
    "gray": 0x808080,
    "green": 0x008000,
    "greenyellow": 0xadff2f,
    "grey": 0x808080,
    "honeydew": 0xf0fff0,
    "hotpink": 0xff69b4,
    "indianred": 0xcd5c5c,
    "indigo": 0x4b0082,
    "ivory": 0xfffff0,
    "khaki": 0xf0e68c,
    "lavender": 0xe6e6fa,
    "lavenderblush": 0xfff0f5,
    "lawngreen": 0x7cfc00,
    "lemonchiffon": 0xfffacd,
    "lightblue": 0xadd8e6,
    "lightcoral": 0xf08080,
    "lightcyan": 0xe0ffff,
    "lightgoldenrodyellow": 0xfafad2,
    "lightgray": 0xd3d3d3,
    "lightgreen": 0x90ee90,
    "lightgrey": 0xd3d3d3,
    "lightpink": 0xffb6c1,
    "lightsalmon": 0xffa07a,
    "lightseagreen": 0x20b2aa,
    "lightskyblue": 0x87cefa,
    "lightslategray": 0x778899,
    "lightslategrey": 0x778899,
    "lightsteelblue": 0xb0c4de,
    "lightyellow": 0xffffe0,
    "lime": 0x00ff00,
    "limegreen": 0x32cd32,
    "linen": 0xfaf0e6,
    "magenta": 0xff00ff,
    "maroon": 0x800000,
    "mediumaquamarine": 0x66cdaa,
    "mediumblue": 0x0000cd,
    "mediumorchid": 0xba55d3,
    "mediumpurple": 0x9370db,
    "mediumseagreen": 0x3cb371,
    "mediumslateblue": 0x7b68ee,
    "mediumspringgreen": 0x00fa9a,
    "mediumturquoise": 0x48d1cc,
    "mediumvioletred": 0xc71585,
    "midnightblue": 0x191970,
    "mintcream": 0xf5fffa,
    "mistyrose": 0xffe4e1,
    "moccasin": 0xffe4b5,
    "navajowhite": 0xffdead,
    "navy": 0x000080,
    "oldlace": 0xfdf5e6,
    "olive": 0x808000,
    "olivedrab": 0x6b8e23,
    "orange": 0xffa500,
    "orangered": 0xff4500,
    "orchid": 0xda70d6,
    "palegoldenrod": 0xeee8aa,
    "palegreen": 0x98fb98,
    "paleturquoise": 0xafeeee,
    "palevioletred": 0xdb7093,
    "papayawhip": 0xffefd5,
    "peachpuff": 0xffdab9,
    "peru": 0xcd853f,
    "pink": 0xffc0cb,
    "plum": 0xdda0dd,
    "powderblue": 0xb0e0e6,
    "purple": 0x800080,
    "red": 0xff0000,
    "rosybrown": 0xbc8f8f,
    "royalblue": 0x4169e1,
    "saddlebrown": 0x8b4513,
    "salmon": 0xfa8072,
    "sandybrown": 0xf4a460,
    "seagreen": 0x2e8b57,
    "seashell": 0xfff5ee,
    "sienna": 0xa0522d,
    "silver": 0xc0c0c0,
    "skyblue": 0x87ceeb,
    "slateblue": 0x6a5acd,
    "slategray": 0x708090,
    "slategrey": 0x708090,
    "snow": 0xfffafa,
    "springgreen": 0x00ff7f,
    "steelblue": 0x4682b4,
    "tan": 0xd2b48c,
    "teal": 0x008080,
    "thistle": 0xd8bfd8,
    "tomato": 0xff6347,
    "turquoise": 0x40e0d0,
    "violet": 0xee82ee,
    "wheat": 0xf5deb3,
    "white": 0xffffff,
    "whitesmoke": 0xf5f5f5,
    "yellow": 0xffff00,
    "yellowgreen": 0x9acd32,
    "rebeccapurple": 0x663399,
  }
  var res: Table[string, RGBColor]
  for it in ColorsRGBMap:
    res[it[0]] = RGBColor(it[1])
  res

func r*(c: ARGBColor): uint8 =
  return uint8(uint32(c) shr 16)

func g*(c: ARGBColor): uint8 =
  return uint8(uint32(c) shr 8)

func b*(c: ARGBColor): uint8 =
  return uint8(uint32(c))

func a*(c: ARGBColor): uint8 =
  return uint8(uint32(c) shr 24)

proc `r=`*(c: var ARGBColor, r: uint8) =
  c = ARGBColor(uint32(c) or (uint32(r) shl 16))

proc `g=`*(c: var ARGBColor, g: uint8) =
  c = ARGBColor(uint32(c) or (uint32(g) shl 8))

proc `b=`*(c: var ARGBColor, b: uint8) =
  c = ARGBColor(uint32(c) or uint32(b))

proc `a=`*(c: var ARGBColor, a: uint8) =
  c = ARGBColor(uint32(c) or (uint32(a) shl 24))

# https://html.spec.whatwg.org/#serialisation-of-a-color
func serialize*(color: ARGBColor): string =
  if color.a == 255:
    var res = "#"
    res.pushHex(color.r)
    res.pushHex(color.g)
    res.pushHex(color.b)
    return res
  let a = float64(color.a) / 255
  return "rgba(" & $color.r & ", " & $color.g & ", " & $color.b & ", " & $a &
    ")"

func `$`*(argbcolor: ARGBColor): string =
  return argbcolor.serialize()

# https://arxiv.org/pdf/2202.02864.pdf
func fastmul*(c, ca: uint32): uint32 =
  let u = c or 0xFF000000u32
  var rb = u and 0x00FF00FFu32
  rb *= ca
  rb += 0x00800080
  rb += (rb shr 8) and 0x00FF00FFu32
  rb = rb and 0xFF00FF00u32
  var ga = (u shr 8) and 0x00FF00FFu32
  ga *= ca
  ga += 0x00800080
  ga += (ga shr 8) and 0x00FF00FFu32
  ga = ga and 0xFF00FF00u32
  return ga or (rb shr 8)

# fastmul, but preserves alpha
func fastmul1(c, ca: uint32): uint32 =
  let u = c
  var rb = u and 0x00FF00FFu32
  rb *= ca
  rb += 0x00800080
  rb += (rb shr 8) and 0x00FF00FFu32
  rb = rb and 0xFF00FF00u32
  var ga = (u shr 8) and 0x00FF00FFu32
  ga *= ca
  ga += 0x00800080
  ga += (ga shr 8) and 0x00FF00FFu32
  ga = ga and 0xFF00FF00u32
  return ga or (rb shr 8)

func fastmul1(c: ARGBColor; ca: uint32): ARGBColor =
  return ARGBColor(fastmul1(uint32(c), ca))

func premul(c: ARGBColor): ARGBColor =
  return ARGBColor(fastmul(uint32(c), uint32(c.a)))

# This is somewhat faster than floats or a lookup table, and is correct for
# all inputs.
proc straight(c: ARGBColor): ARGBColor =
  let a8 = c.a
  if a8 == 0:
    return ARGBColor(0)
  let a = uint32(a8)
  let r = ((uint32(c.r) * 0xFF00 div a + 0x80) shr 8) and 0xFF
  let g = ((uint32(c.g) * 0xFF00 div a + 0x80) shr 8) and 0xFF
  let b = ((uint32(c.b) * 0xFF00 div a + 0x80) shr 8) and 0xFF
  return ARGBColor((a shl 24) or (r shl 16) or (g shl 8) or b)

func blend*(c0, c1: ARGBColor): ARGBColor =
  let pc0 = c0.premul()
  let pc1 = c1.premul()
  let k = 255 - pc1.a
  let mc = fastmul1(pc0, uint32(k))
  let rr = pc1.r + mc.r
  let rg = pc1.g + mc.g
  let rb = pc1.b + mc.b
  let ra = pc1.a + mc.a
  let pres = rgba(rr, rg, rb, ra)
  let res = straight(pres)
  return res

func rgb*(r, g, b: uint8): RGBColor =
  return RGBColor((uint32(r) shl 16) or (uint32(g) shl 8) or uint32(b))

func r*(c: RGBColor): uint8 =
  return uint8(uint32(c) shr 16)

func g*(c: RGBColor): uint8 =
  return uint8(uint32(c) shr 8)

func b*(c: RGBColor): uint8 =
  return uint8(uint32(c))

# see https://learn.microsoft.com/en-us/previous-versions/windows/embedded/ms893078(v=msdn.10)
func Y*(c: RGBColor): uint8 =
  let rmul = uint16(c.r) * 66u16
  let gmul = uint16(c.g) * 129u16
  let bmul = uint16(c.b) * 25u16
  return uint8(((rmul + gmul + bmul + 128) shr 8) + 16)

func U*(c: RGBColor): uint8 =
  let rmul = uint16(c.r) * 38u16
  let gmul = uint16(c.g) * 74u16
  let bmul = uint16(c.b) * 112u16
  return uint8(((128 + bmul - rmul - gmul) shr 8) + 128)

func V*(c: RGBColor): uint8 =
  let rmul = uint16(c.r) * 112u16
  let gmul = uint16(c.g) * 94u16
  let bmul = uint16(c.b) * 18u16
  return uint8(((128 + rmul - gmul - bmul) shr 8) + 128)

func YUV*(Y, U, V: uint8): RGBColor =
  let C = int(Y) - 16
  let D = int(U) - 128
  let E = int(V) - 128
  let r = max(min((298 * C + 409 * E + 128) shr 8, 255), 0)
  let g = max(min((298 * C - 100 * D - 208 * E + 128) shr 8, 255), 0)
  let b = max(min((298 * C + 516 * D + 128) shr 8, 255), 0)
  return rgb(uint8(r), uint8(g), uint8(b))

func rgba*(r, g, b, a: uint8): ARGBColor =
  return ARGBColor((uint32(a) shl 24) or (uint32(r) shl 16) or
    (uint32(g) shl 8) or uint32(b))

func rgba_be*(r, g, b, a: uint8): RGBAColorBE =
  return RGBAColorBE(r: r, g: g, b: b, a: a)

func rgb_be*(r, g, b: uint8): RGBAColorBE =
  return RGBAColorBE(r: r, g: g, b: b, a: 0xFF)

func rgba*(r, g, b, a: int): ARGBColor =
  return rgba(uint8(r), uint8(g), uint8(b), uint8(a))

func gray*(n: uint8): RGBColor =
  return rgb(n, n, n)

func gray_be*(n: uint8): RGBAColorBE =
  return rgb_be(n, n, n)

# NOTE: this assumes n notin 0..15 (which would be ANSI 4-bit)
func toRGB*(param0: EightBitColor): RGBColor =
  doAssert uint8(param0) notin 0u8..15u8
  let u = uint8(param0)
  if u in 16u8..231u8:
    #16 + 36 * r + 6 * g + b
    let n = u - 16
    let r = uint8(int(n div 36) * 255 div 5)
    let m = int(n mod 36)
    let g = uint8(((m div 6) * 255) div 5)
    let b = uint8(((m mod 6) * 255) div 5)
    return rgb(r, g, b)
  else: # 232..255
    let n = (u - 232) * 10 + 8
    return gray(n)

func toEightBit*(rgb: RGBColor): EightBitColor =
  let r = int(rgb.r)
  let g = int(rgb.g)
  let b = int(rgb.b)
  # Idea from here: https://github.com/Qix-/color-convert/pull/75
  # This seems to work about as well as checking for
  # abs(U - 128) < 5 & abs(V - 128 < 5), but is definitely faster.
  if r shr 4 == g shr 4 and g shr 4 == b shr 4:
    if r < 8:
      return EightBitColor(16)
    if r > 248:
      return EightBitColor(231)
    return EightBitColor(uint8(((r - 8) * 24 div 247) + 232))
  #16 + 36 * r + 6 * g + b
  return EightBitColor(uint8(16 + 36 * (r * 5 div 255) +
    6 * (g * 5 div 255) + (b * 5 div 255)))

template `$`*(rgbcolor: RGBColor): string =
  "rgb(" & $rgbcolor.r & ", " & $rgbcolor.g & ", " & $rgbcolor.b & ")"

template `$`*(color: CellColor): string =
  if color.t == ctRGB:
    $color.argbcolor
  else:
    "tcolor" & $color.n

func parseHexColor*(s: string): Option[ARGBColor] =
  for c in s:
    if hexValue(c) == -1: return none(ARGBColor)
  case s.len
  of 6:
    let c = 0xFF000000 or
            (hexValue(s[0]) shl 20) or (hexValue(s[1]) shl 16) or
            (hexValue(s[2]) shl 12) or (hexValue(s[3]) shl 8) or
            (hexValue(s[4]) shl 4) or hexValue(s[5])
    return some(ARGBColor(c))
  of 8:
    let c = (hexValue(s[6]) shl 28) or (hexValue(s[7]) shl 24) or
            (hexValue(s[0]) shl 20) or (hexValue(s[1]) shl 16) or
            (hexValue(s[2]) shl 12) or (hexValue(s[3]) shl 8) or
            (hexValue(s[4]) shl 4) or hexValue(s[5])
    return some(ARGBColor(c))
  of 3:
    let c = 0xFF000000 or
            (hexValue(s[0]) shl 20) or (hexValue(s[0]) shl 16) or
            (hexValue(s[1]) shl 12) or (hexValue(s[1]) shl 8) or
            (hexValue(s[2]) shl 4) or hexValue(s[2])
    return some(ARGBColor(c))
  of 4:
    let c = (hexValue(s[3]) shl 28) or (hexValue(s[3]) shl 24) or
            (hexValue(s[0]) shl 20) or (hexValue(s[0]) shl 16) or
            (hexValue(s[1]) shl 12) or (hexValue(s[1]) shl 8) or
            (hexValue(s[2]) shl 4) or hexValue(s[2])
    return some(ARGBColor(c))
  else: discard

func parseARGBColor*(s: string): Option[ARGBColor] =
  if s in ColorsRGB:
    return some(ARGBColor(ColorsRGB[s]))
  if (s.len == 3 or s.len == 4 or s.len == 6 or s.len == 8) and s[0] == '#':
    return parseHexColor(s[1..^1])
  if s.len > 2 and s[0] == '0' and s[1] == 'x':
    return parseHexColor(s[2..^1])
  return parseHexColor(s)

func parseLegacyColor0*(s: string): RGBColor =
  if s in ColorsRGB:
    return ColorsRGB[s]
  block hex:
    if s.len == 4:
      for c in s:
        if hexValue(c) == -1:
          break hex
      let c = (hexValue(s[0]) * 17 shl 16) or
        (hexValue(s[1]) * 17 shl 8) or
        (hexValue(s[2]) * 17)
      return RGBColor(c)
  # Seriously, what the hell.
  var s2 = if s[0] == '#':
    s.substr(1)
  else:
    s
  for i in 0 ..< s2.len:
    if hexValue(s2[i]) == -1:
      s2[i] = '0'
  while s2.len == 0 or s2.len mod 3 != 0:
    s2 &= '0'
  var l = s2.len div 3
  let c1 = s2[0..<min(l,2)]
  let c2 = s2[l..<min(l*2,l+2)]
  let c3 = s2[l*2..<min(l*3,l*2+2)]
  let c = if l == 1:
    (hexValue(c1[0]) shl 20) or (hexValue(c1[0]) shl 16) or
    (hexValue(c2[0]) shl 12) or (hexValue(c2[0]) shl 8) or
    (hexValue(c3[0]) shl 4) or hexValue(c3[0])
  else:
    (hexValue(c1[0]) shl 20) or (hexValue(c1[1]) shl 16) or
    (hexValue(c2[0]) shl 12) or (hexValue(c2[1]) shl 8) or
    (hexValue(c3[0]) shl 4) or hexValue(c3[1])
  return RGBColor(c)