diff options
author | rumpf_a@web.de <> | 2010-01-10 17:53:33 +0100 |
---|---|---|
committer | rumpf_a@web.de <> | 2010-01-10 17:53:33 +0100 |
commit | 55e900bba2be06cf91789e749d9fc31f017a0dd0 (patch) | |
tree | 6e223c8a7c3f70767b036d698de7ed00a2f80ea3 /tests | |
parent | a4ba67dd2eb335cb9c41a6a267b774a4830ea529 (diff) | |
download | Nim-55e900bba2be06cf91789e749d9fc31f017a0dd0.tar.gz |
devel of web frontend
Diffstat (limited to 'tests')
-rw-r--r-- | tests/tcolors.nim | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/tcolors.nim b/tests/tcolors.nim new file mode 100644 index 000000000..9d1034405 --- /dev/null +++ b/tests/tcolors.nim @@ -0,0 +1,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) |