This module implements color handling for Nim, namely color mixing and parsing the CSS color names.
Procs
proc extractRGB(a: Color): tuple[r, g, b: range[0 .. 255]] {....raises: [], tags: [], forbids: [].}
-
Extracts the red/green/blue components of the color a.
Example:
var a = Color(0xff_00_ff) b = Color(0x00_ff_cc) type Col = range[0..255] # assert extractRGB(a) == (r: 255.Col, g: 0.Col, b: 255.Col) # assert extractRGB(b) == (r: 0.Col, g: 255.Col, b: 204.Col) echo extractRGB(a) echo typeof(extractRGB(a)) echo extractRGB(b) echo typeof(extractRGB(b))
Source Edit proc intensity(a: Color; f: float): Color {....raises: [], tags: [], forbids: [].}
-
Returns a with intensity f. f should be a float from 0.0 (completely dark) to 1.0 (full color intensity).
Example:
var a = Color(0xff_00_ff) b = Color(0x00_42_cc) assert a.intensity(0.5) == Color(0x80_00_80) assert b.intensity(0.5) == Color(0x00_21_66)
Source Edit proc parseColor(name: string): Color {....raises: [ValueError], tags: [], forbids: [].}
-
Parses name to a color value.
If no valid color could be parsed ValueError is raised. Case insensitive.
Example:
var a = "silver" b = "#0179fc" c = "#zzmmtt" assert parseColor(a) == Color(0xc0_c0_c0) assert parseColor(b) == Color(0x01_79_fc) doAssertRaises(ValueError): discard parseColor(c)
Source Edit
Templates
template mix(a, b: Color; fn: untyped): untyped
-
Uses fn to mix the colors a and b.
fn is invoked for each component R, G, and B. If fn's result is not in the range[0..255], it will be saturated to be so.
Example:
var a = Color(0x0a2814) b = Color(0x050a03) proc myMix(x, y: int): int = 2 * x - 3 * y assert mix(a, b, myMix) == Color(0x05_32_1f)
Source Edit