Source Edit

This module implements color handling for Nim, namely color mixing and parsing the CSS color names.

Imports

strutils, algorithm

Types

  1. Color = distinct int

A color stored as RGB, e.g. 0xff00cc. Source Edit

Consts

  1. colAliceBlue = 15792383

Source Edit

  1. colAntiqueWhite = 16444375

Source Edit

  1. colAqua = 65535

Source Edit

  1. colAquamarine = 8388564

Source Edit

  1. colAzure = 15794175

Source Edit

  1. colBeige = 16119260

Source Edit

  1. colBisque = 16770244

Source Edit

  1. colBlack = 0

Source Edit

  1. colBlanchedAlmond = 16772045

Source Edit

  1. colBlue = 255

Source Edit

  1. colBlueViolet = 9055202

Source Edit

  1. colBrown = 10824234

Source Edit

  1. colBurlyWood = 14596231

Source Edit

  1. colCadetBlue = 6266528

Source Edit

  1. colChartreuse = 8388352

Source Edit

  1. colChocolate = 13789470

Source Edit

  1. colCoral = 16744272

Source Edit

  1. colCornflowerBlue = 6591981

Source Edit

  1. colCornsilk = 16775388

Source Edit

  1. colCrimson = 14423100

Source Edit

  1. colCyan = 65535

Source Edit

  1. colDarkBlue = 139

Source Edit

  1. colDarkCyan = 35723

Source Edit

  1. colDarkGoldenRod = 12092939

Source Edit

  1. colDarkGray = 11119017

Source Edit

  1. colDarkGreen = 25600

Source Edit

  1. colDarkGrey = 11119017

Source Edit

  1. colDarkKhaki = 12433259

Source Edit

  1. colDarkMagenta = 9109643

Source Edit

  1. colDarkOliveGreen = 5597999

Source Edit

  1. colDarkorange = 16747520

Source Edit

  1. colDarkOrchid = 10040012

Source Edit

  1. colDarkRed = 9109504

Source Edit

  1. colDarkSalmon = 15308410

Source Edit

  1. colDarkSeaGreen = 9419919

Source Edit

  1. colDarkSlateBlue = 4734347

Source Edit

  1. colDarkSlateGray = 3100495

Source Edit

  1. colDarkSlateGrey = 3100495

Source Edit

  1. colDarkTurquoise = 52945

Source Edit

  1. colDarkViolet = 9699539

Source Edit

  1. colDeepPink = 16716947

Source Edit

  1. colDeepSkyBlue = 49151

Source Edit

  1. colDimGray = 6908265

Source Edit

  1. colDimGrey = 6908265

Source Edit

  1. colDodgerBlue = 2003199

Source Edit

  1. colFireBrick = 11674146

Source Edit

  1. colFloralWhite = 16775920

Source Edit

  1. colForestGreen = 2263842

Source Edit

  1. colFuchsia = 16711935

Source Edit

  1. colGainsboro = 14474460

Source Edit

  1. colGhostWhite = 16316671

Source Edit

  1. colGold = 16766720

Source Edit

  1. colGoldenRod = 14329120

Source Edit

  1. colGray = 8421504

Source Edit

  1. colGreen = 32768

Source Edit

  1. colGreenYellow = 11403055

Source Edit

  1. colGrey = 8421504

Source Edit

  1. colHoneyDew = 15794160

Source Edit

  1. colHotPink = 16738740

Source Edit

  1. colIndianRed = 13458524

Source Edit

  1. colIndigo = 4915330

Source Edit

  1. colIvory = 16777200

Source Edit

  1. colKhaki = 15787660

Source Edit

  1. colLavender = 15132410

Source Edit

  1. colLavenderBlush = 16773365

Source Edit

  1. colLawnGreen = 8190976

Source Edit

  1. colLemonChiffon = 16775885

Source Edit

  1. colLightBlue = 11393254

Source Edit

  1. colLightCoral = 15761536

Source Edit

  1. colLightCyan = 14745599

Source Edit

  1. colLightGoldenRodYellow = 16448210

Source Edit

  1. colLightGray = 13882323

Source Edit

  1. colLightGreen = 9498256

Source Edit

  1. colLightGrey = 13882323

Source Edit

  1. colLightPink = 16758465

Source Edit

  1. colLightSalmon = 16752762

Source Edit

  1. colLightSeaGreen = 2142890

Source Edit

  1. colLightSkyBlue = 8900346

Source Edit

  1. colLightSlateGray = 7833753

Source Edit

  1. colLightSlateGrey = 7833753

Source Edit

  1. colLightSteelBlue = 11584734

Source Edit

  1. colLightYellow = 16777184

Source Edit

  1. colLime = 65280

Source Edit

  1. colLimeGreen = 3329330

Source Edit

  1. colLinen = 16445670

Source Edit

  1. colMagenta = 16711935

Source Edit

  1. colMaroon = 8388608

Source Edit

  1. colMediumAquaMarine = 6737322

Source Edit

  1. colMediumBlue = 205

Source Edit

  1. colMediumOrchid = 12211667

Source Edit

  1. colMediumPurple = 9662683

Source Edit

  1. colMediumSeaGreen = 3978097

Source Edit

  1. colMediumSlateBlue = 8087790

Source Edit

  1. colMediumSpringGreen = 64154

Source Edit

  1. colMediumTurquoise = 4772300

Source Edit

  1. colMediumVioletRed = 13047173

Source Edit

  1. colMidnightBlue = 1644912

Source Edit

  1. colMintCream = 16121850

Source Edit

  1. colMistyRose = 16770273

Source Edit

  1. colMoccasin = 16770229

Source Edit

  1. colNavajoWhite = 16768685

Source Edit

  1. colNavy = 128

Source Edit

  1. colOldLace = 16643558

Source Edit

  1. colOlive = 8421376

Source Edit

  1. colOliveDrab = 7048739

Source Edit

  1. colOrange = 16753920

Source Edit

  1. colOrangeRed = 16729344

Source Edit

  1. colOrchid = 14315734

Source Edit

  1. colPaleGoldenRod = 15657130

Source Edit

  1. colPaleGreen = 10025880

Source Edit

  1. colPaleTurquoise = 11529966

Source Edit

  1. colPaleVioletRed = 14381203

Source Edit

  1. colPapayaWhip = 16773077

Source Edit

  1. colPeachPuff = 16767673

Source Edit

  1. colPeru = 13468991

Source Edit

  1. colPink = 16761035

Source Edit

  1. colPlum = 14524637

Source Edit

  1. colPowderBlue = 11591910

Source Edit

  1. colPurple = 8388736

Source Edit

  1. colRebeccaPurple = 6697881

Source Edit

  1. colRed = 16711680

Source Edit

  1. colRosyBrown = 12357519

Source Edit

  1. colRoyalBlue = 4286945

Source Edit

  1. colSaddleBrown = 9127187

Source Edit

  1. colSalmon = 16416882

Source Edit

  1. colSandyBrown = 16032864

Source Edit

  1. colSeaGreen = 3050327

Source Edit

  1. colSeaShell = 16774638

Source Edit

  1. colSienna = 10506797

Source Edit

  1. colSilver = 12632256

Source Edit

  1. colSkyBlue = 8900331

Source Edit

  1. colSlateBlue = 6970061

Source Edit

  1. colSlateGray = 7372944

Source Edit

  1. colSlateGrey = 7372944

Source Edit

  1. colSnow = 16775930

Source Edit

  1. colSpringGreen = 65407

Source Edit

  1. colSteelBlue = 4620980

Source Edit

  1. colTan = 13808780

Source Edit

  1. colTeal = 32896

Source Edit

  1. colThistle = 14204888

Source Edit

  1. colTomato = 16737095

Source Edit

  1. colTurquoise = 4251856

Source Edit

  1. colViolet = 15631086

Source Edit

  1. colWheat = 16113331

Source Edit

  1. colWhite = 16777215

Source Edit

  1. colWhiteSmoke = 16119285

Source Edit

  1. colYellow = 16776960

Source Edit

  1. colYellowGreen = 10145074

Source Edit

Procs

  1. proc `$`(c: Color): string {....raises: [], tags: [], forbids: [].}

Converts a color into its textual representation.

Example:

  1. assert $colFuchsia == "#FF00FF"

Source Edit

  1. 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:

  1. var
  2. a = Color(0xaa_00_ff)
  3. b = Color(0x11_cc_cc)
  4. assert a + b == Color(0xbb_cc_ff)

Source Edit

  1. 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:

  1. var
  2. a = Color(0xff_33_ff)
  3. b = Color(0x11_ff_cc)
  4. assert a - b == Color(0xee_00_33)

Source Edit

  1. proc `==`(a, b: Color): bool {.borrow, ...raises: [], tags: [], forbids: [].}

Compares two colors.

  1. var
  2. a = Color(0xff_00_ff)
  3. b = colFuchsia
  4. c = Color(0x00_ff_cc)
  5. assert a == b
  6. assert not (a == c)

Source Edit

  1. proc extractRGB(a: Color): tuple[r, g, b: range[0 .. 255]] {....raises: [],
  2. tags: [], forbids: [].}

Extracts the red/green/blue components of the color a.

Example:

  1. var
  2. a = Color(0xff_00_ff)
  3. b = Color(0x00_ff_cc)
  4. type
  5. Col = range[0..255]
  6. # assert extractRGB(a) == (r: 255.Col, g: 0.Col, b: 255.Col)
  7. # assert extractRGB(b) == (r: 0.Col, g: 255.Col, b: 204.Col)
  8. echo extractRGB(a)
  9. echo typeof(extractRGB(a))
  10. echo extractRGB(b)
  11. echo typeof(extractRGB(b))

Source Edit

  1. 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:

  1. var
  2. a = Color(0xff_00_ff)
  3. b = Color(0x00_42_cc)
  4. assert a.intensity(0.5) == Color(0x80_00_80)
  5. assert b.intensity(0.5) == Color(0x00_21_66)

Source Edit

  1. 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:

  1. var
  2. a = "silver"
  3. b = "#0179fc"
  4. c = "#zzmmtt"
  5. assert a.isColor
  6. assert b.isColor
  7. assert not c.isColor

Source Edit

  1. proc parseColor(name: string): Color {....raises: [ValueError], tags: [],
  2. forbids: [].}

Parses name to a color value.

If no valid color could be parsed ValueError is raised. Case insensitive.

Example:

  1. var
  2. a = "silver"
  3. b = "#0179fc"
  4. c = "#zzmmtt"
  5. assert parseColor(a) == Color(0xc0_c0_c0)
  6. assert parseColor(b) == Color(0x01_79_fc)
  7. doAssertRaises(ValueError): discard parseColor(c)

Source Edit

  1. proc rgb(r, g, b: range[0 .. 255]): Color {....raises: [], tags: [], forbids: [].}

Constructs a color from RGB values.

Example:

  1. assert rgb(0, 255, 128) == Color(0x00_ff_80)

Source Edit

Templates

  1. 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:

  1. var
  2. a = Color(0x0a2814)
  3. b = Color(0x050a03)
  4. proc myMix(x, y: int): int =
  5. 2 * x - 3 * y
  6. assert mix(a, b, myMix) == Color(0x05_32_1f)

Source Edit