This module implements color handling for Nim, namely color mixing and parsing the CSS color names.
Imports
Types
Color = distinct int
A color stored as RGB, e.g. 0xff00cc. Source Edit
Consts
colAliceBlue = 15792383
colAntiqueWhite = 16444375
colAqua = 65535
colAquamarine = 8388564
colAzure = 15794175
colBeige = 16119260
colBisque = 16770244
colBlack = 0
colBlanchedAlmond = 16772045
colBlue = 255
colBlueViolet = 9055202
colBrown = 10824234
colBurlyWood = 14596231
colCadetBlue = 6266528
colChartreuse = 8388352
colChocolate = 13789470
colCoral = 16744272
colCornflowerBlue = 6591981
colCornsilk = 16775388
colCrimson = 14423100
colCyan = 65535
colDarkBlue = 139
colDarkCyan = 35723
colDarkGoldenRod = 12092939
colDarkGray = 11119017
colDarkGreen = 25600
colDarkGrey = 11119017
colDarkKhaki = 12433259
colDarkMagenta = 9109643
colDarkOliveGreen = 5597999
colDarkorange = 16747520
colDarkOrchid = 10040012
colDarkRed = 9109504
colDarkSalmon = 15308410
colDarkSeaGreen = 9419919
colDarkSlateBlue = 4734347
colDarkSlateGray = 3100495
colDarkSlateGrey = 3100495
colDarkTurquoise = 52945
colDarkViolet = 9699539
colDeepPink = 16716947
colDeepSkyBlue = 49151
colDimGray = 6908265
colDimGrey = 6908265
colDodgerBlue = 2003199
colFireBrick = 11674146
colFloralWhite = 16775920
colForestGreen = 2263842
colFuchsia = 16711935
colGainsboro = 14474460
colGhostWhite = 16316671
colGold = 16766720
colGoldenRod = 14329120
colGray = 8421504
colGreen = 32768
colGreenYellow = 11403055
colGrey = 8421504
colHoneyDew = 15794160
colHotPink = 16738740
colIndianRed = 13458524
colIndigo = 4915330
colIvory = 16777200
colKhaki = 15787660
colLavender = 15132410
colLavenderBlush = 16773365
colLawnGreen = 8190976
colLemonChiffon = 16775885
colLightBlue = 11393254
colLightCoral = 15761536
colLightCyan = 14745599
colLightGoldenRodYellow = 16448210
colLightGray = 13882323
colLightGreen = 9498256
colLightGrey = 13882323
colLightPink = 16758465
colLightSalmon = 16752762
colLightSeaGreen = 2142890
colLightSkyBlue = 8900346
colLightSlateGray = 7833753
colLightSlateGrey = 7833753
colLightSteelBlue = 11584734
colLightYellow = 16777184
colLime = 65280
colLimeGreen = 3329330
colLinen = 16445670
colMagenta = 16711935
colMaroon = 8388608
colMediumAquaMarine = 6737322
colMediumBlue = 205
colMediumOrchid = 12211667
colMediumPurple = 9662683
colMediumSeaGreen = 3978097
colMediumSlateBlue = 8087790
colMediumSpringGreen = 64154
colMediumTurquoise = 4772300
colMediumVioletRed = 13047173
colMidnightBlue = 1644912
colMintCream = 16121850
colMistyRose = 16770273
colMoccasin = 16770229
colNavajoWhite = 16768685
colNavy = 128
colOldLace = 16643558
colOlive = 8421376
colOliveDrab = 7048739
colOrange = 16753920
colOrangeRed = 16729344
colOrchid = 14315734
colPaleGoldenRod = 15657130
colPaleGreen = 10025880
colPaleTurquoise = 11529966
colPaleVioletRed = 14381203
colPapayaWhip = 16773077
colPeachPuff = 16767673
colPeru = 13468991
colPink = 16761035
colPlum = 14524637
colPowderBlue = 11591910
colPurple = 8388736
colRebeccaPurple = 6697881
colRed = 16711680
colRosyBrown = 12357519
colRoyalBlue = 4286945
colSaddleBrown = 9127187
colSalmon = 16416882
colSandyBrown = 16032864
colSeaGreen = 3050327
colSeaShell = 16774638
colSienna = 10506797
colSilver = 12632256
colSkyBlue = 8900331
colSlateBlue = 6970061
colSlateGray = 7372944
colSlateGrey = 7372944
colSnow = 16775930
colSpringGreen = 65407
colSteelBlue = 4620980
colTan = 13808780
colTeal = 32896
colThistle = 14204888
colTomato = 16737095
colTurquoise = 4251856
colViolet = 15631086
colWheat = 16113331
colWhite = 16777215
colWhiteSmoke = 16119285
colYellow = 16776960
colYellowGreen = 10145074
Procs
proc `$`(c: Color): string {....raises: [], tags: [], forbids: [].}
Converts a color into its textual representation.
Example:
assert $colFuchsia == "#FF00FF"
proc `+`(a, b: Color): Color {....raises: [], tags: [], forbids: [].}
Adds two colors.
This uses saturated arithmetic, so that each color component cannot overflow (255 is used as a maximum).
Example:
var
a = Color(0xaa_00_ff)
b = Color(0x11_cc_cc)
assert a + b == Color(0xbb_cc_ff)
proc `-`(a, b: Color): Color {....raises: [], tags: [], forbids: [].}
Subtracts two colors.
This uses saturated arithmetic, so that each color component cannot underflow (0 is used as a minimum).
Example:
var
a = Color(0xff_33_ff)
b = Color(0x11_ff_cc)
assert a - b == Color(0xee_00_33)
proc `==`(a, b: Color): bool {.borrow, ...raises: [], tags: [], forbids: [].}
Compares two colors.
var
a = Color(0xff_00_ff)
b = colFuchsia
c = Color(0x00_ff_cc)
assert a == b
assert not (a == c)
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))
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)
proc isColor(name: string): bool {....raises: [], tags: [], forbids: [].}
Returns true if name is a known color name or a hexadecimal color prefixed with #. Case insensitive.
Example:
var
a = "silver"
b = "#0179fc"
c = "#zzmmtt"
assert a.isColor
assert b.isColor
assert not c.isColor
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)
proc rgb(r, g, b: range[0 .. 255]): Color {....raises: [], tags: [], forbids: [].}
Constructs a color from RGB values.
Example:
assert rgb(0, 255, 128) == Color(0x00_ff_80)
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)