summary refs log tree commit diff stats
path: root/tests/tcolors.nim
blob: 9d1034405f1161649d01b356076b3106263de763 (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
import strutils

type
  TColor = distinct int32

proc rgb(r, g, b: range[0..255]): TColor = 
  result = TColor(r or g shl 8 or b shl 16)

proc `$`(c: TColor): string =
  result = "#" & toHex(int32(c), 6)

echo rgb(34, 55, 255)

when false:
  type
    TColor = distinct int32
    TColorComponent = distinct int8
  
  proc red(a: TColor): TColorComponent = 
    result = TColorComponent(int32(a) and 0xff'i32)
  
  proc green(a: TColor): TColorComponent = 
    result = TColorComponent(int32(a) shr 8'i32 and 0xff'i32)
  
  proc blue(a: TColor): TColorComponent = 
    result = TColorComponent(int32(a) shr 16'i32 and 0xff'i32)
  
  proc rgb(r, g, b: range[0..255]): TColor = 
    result = TColor(r or g shl 8 or b shl 8)
  
  proc `+!` (a, b: TColorComponent): TColorComponent =  
    ## saturated arithmetic:
    result = TColorComponent(min(ze(int8(a)) + ze(int8(b)), 255))
  
  proc `+` (a, b: TColor): TColor = 
    ## saturated arithmetic for colors makes sense, I think:
    return rgb(red(a) +! red(b), green(a) +! green(b), blue(a) +! blue(b))
  
  rgb(34, 55, 255)