colors.nim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. #
  2. # Nim's Runtime Library
  3. # (c) Copyright 2010 Andreas Rumpf
  4. #
  5. # See the file "copying.txt", included in this
  6. # distribution, for details about the copyright.
  7. #
  8. ## This module implements color handling for Nim,
  9. ## namely color mixing and parsing the CSS color names.
  10. import strutils
  11. from algorithm import binarySearch
  12. type
  13. Color* = distinct int ## A color stored as RGB, e.g. `0xff00cc`.
  14. proc `==`*(a, b: Color): bool {.borrow.}
  15. ## Compares two colors.
  16. ##
  17. ## .. code-block::
  18. ## var
  19. ## a = Color(0xff_00_ff)
  20. ## b = colFuchsia
  21. ## c = Color(0x00_ff_cc)
  22. ## assert a == b
  23. ## assert not (a == c)
  24. template extract(a: Color, r, g, b: untyped) =
  25. var r = a.int shr 16 and 0xff
  26. var g = a.int shr 8 and 0xff
  27. var b = a.int and 0xff
  28. template rawRGB(r, g, b: int): Color =
  29. Color(r shl 16 or g shl 8 or b)
  30. template colorOp(op): Color =
  31. extract(a, ar, ag, ab)
  32. extract(b, br, bg, bb)
  33. rawRGB(op(ar, br), op(ag, bg), op(ab, bb))
  34. proc satPlus(a, b: int): int {.inline.} =
  35. result = a +% b
  36. if result > 255: result = 255
  37. proc satMinus(a, b: int): int {.inline.} =
  38. result = a -% b
  39. if result < 0: result = 0
  40. proc `+`*(a, b: Color): Color =
  41. ## Adds two colors.
  42. ##
  43. ## This uses saturated arithmetic, so that each color
  44. ## component cannot overflow (255 is used as a maximum).
  45. ##
  46. runnableExamples:
  47. var
  48. a = Color(0xaa_00_ff)
  49. b = Color(0x11_cc_cc)
  50. assert a + b == Color(0xbb_cc_ff)
  51. colorOp(satPlus)
  52. proc `-`*(a, b: Color): Color =
  53. ## Subtracts two colors.
  54. ##
  55. ## This uses saturated arithmetic, so that each color
  56. ## component cannot underflow (0 is used as a minimum).
  57. ##
  58. runnableExamples:
  59. var
  60. a = Color(0xff_33_ff)
  61. b = Color(0x11_ff_cc)
  62. assert a - b == Color(0xee_00_33)
  63. colorOp(satMinus)
  64. proc extractRGB*(a: Color): tuple[r, g, b: range[0..255]] =
  65. ## Extracts the red/green/blue components of the color `a`.
  66. ##
  67. runnableExamples:
  68. var
  69. a = Color(0xff_00_ff)
  70. b = Color(0x00_ff_cc)
  71. type
  72. Col = range[0..255]
  73. # assert extractRGB(a) == (r: 255.Col, g: 0.Col, b: 255.Col)
  74. # assert extractRGB(b) == (r: 0.Col, g: 255.Col, b: 204.Col)
  75. echo extractRGB(a)
  76. echo typeof(extractRGB(a))
  77. echo extractRGB(b)
  78. echo typeof(extractRGB(b))
  79. result.r = a.int shr 16 and 0xff
  80. result.g = a.int shr 8 and 0xff
  81. result.b = a.int and 0xff
  82. proc intensity*(a: Color, f: float): Color =
  83. ## Returns `a` with intensity `f`. `f` should be a float from 0.0 (completely
  84. ## dark) to 1.0 (full color intensity).
  85. ##
  86. runnableExamples:
  87. var
  88. a = Color(0xff_00_ff)
  89. b = Color(0x00_42_cc)
  90. assert a.intensity(0.5) == Color(0x80_00_80)
  91. assert b.intensity(0.5) == Color(0x00_21_66)
  92. var r = toInt(toFloat(a.int shr 16 and 0xff) * f)
  93. var g = toInt(toFloat(a.int shr 8 and 0xff) * f)
  94. var b = toInt(toFloat(a.int and 0xff) * f)
  95. if r >% 255: r = 255
  96. if g >% 255: g = 255
  97. if b >% 255: b = 255
  98. result = rawRGB(r, g, b)
  99. template mix*(a, b: Color, fn: untyped): untyped =
  100. ## Uses `fn` to mix the colors `a` and `b`.
  101. ##
  102. ## `fn` is invoked for each component R, G, and B.
  103. ## If `fn`'s result is not in the `range[0..255]`,
  104. ## it will be saturated to be so.
  105. ##
  106. runnableExamples:
  107. var
  108. a = Color(0x0a2814)
  109. b = Color(0x050a03)
  110. proc myMix(x, y: int): int =
  111. 2 * x - 3 * y
  112. assert mix(a, b, myMix) == Color(0x05_32_1f)
  113. template `><` (x: untyped): untyped =
  114. # keep it in the range 0..255
  115. block:
  116. var y = x # eval only once
  117. if y >% 255:
  118. y = if y < 0: 0 else: 255
  119. y
  120. extract(a, ar, ag, ab)
  121. extract(b, br, bg, bb)
  122. rawRGB(><fn(ar, br), ><fn(ag, bg), ><fn(ab, bb))
  123. const
  124. colAliceBlue* = Color(0xF0F8FF)
  125. colAntiqueWhite* = Color(0xFAEBD7)
  126. colAqua* = Color(0x00FFFF)
  127. colAquamarine* = Color(0x7FFFD4)
  128. colAzure* = Color(0xF0FFFF)
  129. colBeige* = Color(0xF5F5DC)
  130. colBisque* = Color(0xFFE4C4)
  131. colBlack* = Color(0x000000)
  132. colBlanchedAlmond* = Color(0xFFEBCD)
  133. colBlue* = Color(0x0000FF)
  134. colBlueViolet* = Color(0x8A2BE2)
  135. colBrown* = Color(0xA52A2A)
  136. colBurlyWood* = Color(0xDEB887)
  137. colCadetBlue* = Color(0x5F9EA0)
  138. colChartreuse* = Color(0x7FFF00)
  139. colChocolate* = Color(0xD2691E)
  140. colCoral* = Color(0xFF7F50)
  141. colCornflowerBlue* = Color(0x6495ED)
  142. colCornsilk* = Color(0xFFF8DC)
  143. colCrimson* = Color(0xDC143C)
  144. colCyan* = Color(0x00FFFF)
  145. colDarkBlue* = Color(0x00008B)
  146. colDarkCyan* = Color(0x008B8B)
  147. colDarkGoldenRod* = Color(0xB8860B)
  148. colDarkGray* = Color(0xA9A9A9)
  149. colDarkGreen* = Color(0x006400)
  150. colDarkGrey* = Color(0xA9A9A9)
  151. colDarkKhaki* = Color(0xBDB76B)
  152. colDarkMagenta* = Color(0x8B008B)
  153. colDarkOliveGreen* = Color(0x556B2F)
  154. colDarkorange* = Color(0xFF8C00)
  155. colDarkOrchid* = Color(0x9932CC)
  156. colDarkRed* = Color(0x8B0000)
  157. colDarkSalmon* = Color(0xE9967A)
  158. colDarkSeaGreen* = Color(0x8FBC8F)
  159. colDarkSlateBlue* = Color(0x483D8B)
  160. colDarkSlateGray* = Color(0x2F4F4F)
  161. colDarkSlateGrey* = Color(0x2F4F4F)
  162. colDarkTurquoise* = Color(0x00CED1)
  163. colDarkViolet* = Color(0x9400D3)
  164. colDeepPink* = Color(0xFF1493)
  165. colDeepSkyBlue* = Color(0x00BFFF)
  166. colDimGray* = Color(0x696969)
  167. colDimGrey* = Color(0x696969)
  168. colDodgerBlue* = Color(0x1E90FF)
  169. colFireBrick* = Color(0xB22222)
  170. colFloralWhite* = Color(0xFFFAF0)
  171. colForestGreen* = Color(0x228B22)
  172. colFuchsia* = Color(0xFF00FF)
  173. colGainsboro* = Color(0xDCDCDC)
  174. colGhostWhite* = Color(0xF8F8FF)
  175. colGold* = Color(0xFFD700)
  176. colGoldenRod* = Color(0xDAA520)
  177. colGray* = Color(0x808080)
  178. colGreen* = Color(0x008000)
  179. colGrey* = Color(0x808080)
  180. colGreenYellow* = Color(0xADFF2F)
  181. colHoneyDew* = Color(0xF0FFF0)
  182. colHotPink* = Color(0xFF69B4)
  183. colIndianRed* = Color(0xCD5C5C)
  184. colIndigo* = Color(0x4B0082)
  185. colIvory* = Color(0xFFFFF0)
  186. colKhaki* = Color(0xF0E68C)
  187. colLavender* = Color(0xE6E6FA)
  188. colLavenderBlush* = Color(0xFFF0F5)
  189. colLawnGreen* = Color(0x7CFC00)
  190. colLemonChiffon* = Color(0xFFFACD)
  191. colLightBlue* = Color(0xADD8E6)
  192. colLightCoral* = Color(0xF08080)
  193. colLightCyan* = Color(0xE0FFFF)
  194. colLightGoldenRodYellow* = Color(0xFAFAD2)
  195. colLightGray* = Color(0xD3D3D3)
  196. colLightGreen* = Color(0x90EE90)
  197. colLightGrey* = Color(0xD3D3D3)
  198. colLightPink* = Color(0xFFB6C1)
  199. colLightSalmon* = Color(0xFFA07A)
  200. colLightSeaGreen* = Color(0x20B2AA)
  201. colLightSkyBlue* = Color(0x87CEFA)
  202. colLightSlateGray* = Color(0x778899)
  203. colLightSlateGrey* = Color(0x778899)
  204. colLightSteelBlue* = Color(0xB0C4DE)
  205. colLightYellow* = Color(0xFFFFE0)
  206. colLime* = Color(0x00FF00)
  207. colLimeGreen* = Color(0x32CD32)
  208. colLinen* = Color(0xFAF0E6)
  209. colMagenta* = Color(0xFF00FF)
  210. colMaroon* = Color(0x800000)
  211. colMediumAquaMarine* = Color(0x66CDAA)
  212. colMediumBlue* = Color(0x0000CD)
  213. colMediumOrchid* = Color(0xBA55D3)
  214. colMediumPurple* = Color(0x9370DB)
  215. colMediumSeaGreen* = Color(0x3CB371)
  216. colMediumSlateBlue* = Color(0x7B68EE)
  217. colMediumSpringGreen* = Color(0x00FA9A)
  218. colMediumTurquoise* = Color(0x48D1CC)
  219. colMediumVioletRed* = Color(0xC71585)
  220. colMidnightBlue* = Color(0x191970)
  221. colMintCream* = Color(0xF5FFFA)
  222. colMistyRose* = Color(0xFFE4E1)
  223. colMoccasin* = Color(0xFFE4B5)
  224. colNavajoWhite* = Color(0xFFDEAD)
  225. colNavy* = Color(0x000080)
  226. colOldLace* = Color(0xFDF5E6)
  227. colOlive* = Color(0x808000)
  228. colOliveDrab* = Color(0x6B8E23)
  229. colOrange* = Color(0xFFA500)
  230. colOrangeRed* = Color(0xFF4500)
  231. colOrchid* = Color(0xDA70D6)
  232. colPaleGoldenRod* = Color(0xEEE8AA)
  233. colPaleGreen* = Color(0x98FB98)
  234. colPaleTurquoise* = Color(0xAFEEEE)
  235. colPaleVioletRed* = Color(0xDB7093)
  236. colPapayaWhip* = Color(0xFFEFD5)
  237. colPeachPuff* = Color(0xFFDAB9)
  238. colPeru* = Color(0xCD853F)
  239. colPink* = Color(0xFFC0CB)
  240. colPlum* = Color(0xDDA0DD)
  241. colPowderBlue* = Color(0xB0E0E6)
  242. colPurple* = Color(0x800080)
  243. colRebeccaPurple* = Color(0x663399)
  244. colRed* = Color(0xFF0000)
  245. colRosyBrown* = Color(0xBC8F8F)
  246. colRoyalBlue* = Color(0x4169E1)
  247. colSaddleBrown* = Color(0x8B4513)
  248. colSalmon* = Color(0xFA8072)
  249. colSandyBrown* = Color(0xF4A460)
  250. colSeaGreen* = Color(0x2E8B57)
  251. colSeaShell* = Color(0xFFF5EE)
  252. colSienna* = Color(0xA0522D)
  253. colSilver* = Color(0xC0C0C0)
  254. colSkyBlue* = Color(0x87CEEB)
  255. colSlateBlue* = Color(0x6A5ACD)
  256. colSlateGray* = Color(0x708090)
  257. colSlateGrey* = Color(0x708090)
  258. colSnow* = Color(0xFFFAFA)
  259. colSpringGreen* = Color(0x00FF7F)
  260. colSteelBlue* = Color(0x4682B4)
  261. colTan* = Color(0xD2B48C)
  262. colTeal* = Color(0x008080)
  263. colThistle* = Color(0xD8BFD8)
  264. colTomato* = Color(0xFF6347)
  265. colTurquoise* = Color(0x40E0D0)
  266. colViolet* = Color(0xEE82EE)
  267. colWheat* = Color(0xF5DEB3)
  268. colWhite* = Color(0xFFFFFF)
  269. colWhiteSmoke* = Color(0xF5F5F5)
  270. colYellow* = Color(0xFFFF00)
  271. colYellowGreen* = Color(0x9ACD32)
  272. colorNames = {
  273. "aliceblue": colAliceBlue,
  274. "antiquewhite": colAntiqueWhite,
  275. "aqua": colAqua,
  276. "aquamarine": colAquamarine,
  277. "azure": colAzure,
  278. "beige": colBeige,
  279. "bisque": colBisque,
  280. "black": colBlack,
  281. "blanchedalmond": colBlanchedAlmond,
  282. "blue": colBlue,
  283. "blueviolet": colBlueViolet,
  284. "brown": colBrown,
  285. "burlywood": colBurlyWood,
  286. "cadetblue": colCadetBlue,
  287. "chartreuse": colChartreuse,
  288. "chocolate": colChocolate,
  289. "coral": colCoral,
  290. "cornflowerblue": colCornflowerBlue,
  291. "cornsilk": colCornsilk,
  292. "crimson": colCrimson,
  293. "cyan": colCyan,
  294. "darkblue": colDarkBlue,
  295. "darkcyan": colDarkCyan,
  296. "darkgoldenrod": colDarkGoldenRod,
  297. "darkgray": colDarkGray,
  298. "darkgreen": colDarkGreen,
  299. "darkgrey": colDarkGrey,
  300. "darkkhaki": colDarkKhaki,
  301. "darkmagenta": colDarkMagenta,
  302. "darkolivegreen": colDarkOliveGreen,
  303. "darkorange": colDarkorange,
  304. "darkorchid": colDarkOrchid,
  305. "darkred": colDarkRed,
  306. "darksalmon": colDarkSalmon,
  307. "darkseagreen": colDarkSeaGreen,
  308. "darkslateblue": colDarkSlateBlue,
  309. "darkslategray": colDarkSlateGray,
  310. "darkslategrey": colDarkSlateGrey,
  311. "darkturquoise": colDarkTurquoise,
  312. "darkviolet": colDarkViolet,
  313. "deeppink": colDeepPink,
  314. "deepskyblue": colDeepSkyBlue,
  315. "dimgray": colDimGray,
  316. "dimgrey": colDimGrey,
  317. "dodgerblue": colDodgerBlue,
  318. "firebrick": colFireBrick,
  319. "floralwhite": colFloralWhite,
  320. "forestgreen": colForestGreen,
  321. "fuchsia": colFuchsia,
  322. "gainsboro": colGainsboro,
  323. "ghostwhite": colGhostWhite,
  324. "gold": colGold,
  325. "goldenrod": colGoldenRod,
  326. "gray": colGray,
  327. "green": colGreen,
  328. "grey": colGrey,
  329. "greenyellow": colGreenYellow,
  330. "honeydew": colHoneyDew,
  331. "hotpink": colHotPink,
  332. "indianred": colIndianRed,
  333. "indigo": colIndigo,
  334. "ivory": colIvory,
  335. "khaki": colKhaki,
  336. "lavender": colLavender,
  337. "lavenderblush": colLavenderBlush,
  338. "lawngreen": colLawnGreen,
  339. "lemonchiffon": colLemonChiffon,
  340. "lightblue": colLightBlue,
  341. "lightcoral": colLightCoral,
  342. "lightcyan": colLightCyan,
  343. "lightgoldenrodyellow": colLightGoldenRodYellow,
  344. "lightgray": colLightGray,
  345. "lightgreen": colLightGreen,
  346. "lightgrey": colLightGrey,
  347. "lightpink": colLightPink,
  348. "lightsalmon": colLightSalmon,
  349. "lightseagreen": colLightSeaGreen,
  350. "lightskyblue": colLightSkyBlue,
  351. "lightslategray": colLightSlateGray,
  352. "lightslategrey": colLightSlateGrey,
  353. "lightsteelblue": colLightSteelBlue,
  354. "lightyellow": colLightYellow,
  355. "lime": colLime,
  356. "limegreen": colLimeGreen,
  357. "linen": colLinen,
  358. "magenta": colMagenta,
  359. "maroon": colMaroon,
  360. "mediumaquamarine": colMediumAquaMarine,
  361. "mediumblue": colMediumBlue,
  362. "mediumorchid": colMediumOrchid,
  363. "mediumpurple": colMediumPurple,
  364. "mediumseagreen": colMediumSeaGreen,
  365. "mediumslateblue": colMediumSlateBlue,
  366. "mediumspringgreen": colMediumSpringGreen,
  367. "mediumturquoise": colMediumTurquoise,
  368. "mediumvioletred": colMediumVioletRed,
  369. "midnightblue": colMidnightBlue,
  370. "mintcream": colMintCream,
  371. "mistyrose": colMistyRose,
  372. "moccasin": colMoccasin,
  373. "navajowhite": colNavajoWhite,
  374. "navy": colNavy,
  375. "oldlace": colOldLace,
  376. "olive": colOlive,
  377. "olivedrab": colOliveDrab,
  378. "orange": colOrange,
  379. "orangered": colOrangeRed,
  380. "orchid": colOrchid,
  381. "palegoldenrod": colPaleGoldenRod,
  382. "palegreen": colPaleGreen,
  383. "paleturquoise": colPaleTurquoise,
  384. "palevioletred": colPaleVioletRed,
  385. "papayawhip": colPapayaWhip,
  386. "peachpuff": colPeachPuff,
  387. "peru": colPeru,
  388. "pink": colPink,
  389. "plum": colPlum,
  390. "powderblue": colPowderBlue,
  391. "purple": colPurple,
  392. "rebeccapurple": colRebeccaPurple,
  393. "red": colRed,
  394. "rosybrown": colRosyBrown,
  395. "royalblue": colRoyalBlue,
  396. "saddlebrown": colSaddleBrown,
  397. "salmon": colSalmon,
  398. "sandybrown": colSandyBrown,
  399. "seagreen": colSeaGreen,
  400. "seashell": colSeaShell,
  401. "sienna": colSienna,
  402. "silver": colSilver,
  403. "skyblue": colSkyBlue,
  404. "slateblue": colSlateBlue,
  405. "slategray": colSlateGray,
  406. "slategrey": colSlateGrey,
  407. "snow": colSnow,
  408. "springgreen": colSpringGreen,
  409. "steelblue": colSteelBlue,
  410. "tan": colTan,
  411. "teal": colTeal,
  412. "thistle": colThistle,
  413. "tomato": colTomato,
  414. "turquoise": colTurquoise,
  415. "violet": colViolet,
  416. "wheat": colWheat,
  417. "white": colWhite,
  418. "whitesmoke": colWhiteSmoke,
  419. "yellow": colYellow,
  420. "yellowgreen": colYellowGreen}
  421. proc `$`*(c: Color): string =
  422. ## Converts a color into its textual representation.
  423. ##
  424. runnableExamples:
  425. assert $colFuchsia == "#FF00FF"
  426. result = '#' & toHex(int(c), 6)
  427. proc colorNameCmp(x: tuple[name: string, col: Color], y: string): int =
  428. result = cmpIgnoreCase(x.name, y)
  429. proc parseColor*(name: string): Color =
  430. ## Parses `name` to a color value.
  431. ##
  432. ## If no valid color could be parsed `ValueError` is raised.
  433. ## Case insensitive.
  434. ##
  435. runnableExamples:
  436. var
  437. a = "silver"
  438. b = "#0179fc"
  439. c = "#zzmmtt"
  440. assert parseColor(a) == Color(0xc0_c0_c0)
  441. assert parseColor(b) == Color(0x01_79_fc)
  442. doAssertRaises(ValueError): discard parseColor(c)
  443. if name.len > 0 and name[0] == '#':
  444. result = Color(parseHexInt(name))
  445. else:
  446. var idx = binarySearch(colorNames, name, colorNameCmp)
  447. if idx < 0: raise newException(ValueError, "unknown color: " & name)
  448. result = colorNames[idx][1]
  449. proc isColor*(name: string): bool =
  450. ## Returns true if `name` is a known color name or a hexadecimal color
  451. ## prefixed with `#`. Case insensitive.
  452. ##
  453. runnableExamples:
  454. var
  455. a = "silver"
  456. b = "#0179fc"
  457. c = "#zzmmtt"
  458. assert a.isColor
  459. assert b.isColor
  460. assert not c.isColor
  461. if name.len == 0: return false
  462. if name[0] == '#':
  463. for i in 1 .. name.len-1:
  464. if name[i] notin HexDigits: return false
  465. result = true
  466. else:
  467. result = binarySearch(colorNames, name, colorNameCmp) >= 0
  468. proc rgb*(r, g, b: range[0..255]): Color =
  469. ## Constructs a color from RGB values.
  470. ##
  471. runnableExamples:
  472. assert rgb(0, 255, 128) == Color(0x00_ff_80)
  473. result = rawRGB(r, g, b)