colortrans 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #! /usr/bin/env python
  2. """ Convert values between RGB hex codes and xterm-256 color codes.
  3. Nice long listing of all 256 colors and their codes. Useful for
  4. developing console color themes, or even script output schemes.
  5. Resources:
  6. * http://en.wikipedia.org/wiki/8-bit_color
  7. * http://en.wikipedia.org/wiki/ANSI_escape_code
  8. * /usr/share/X11/rgb.txt
  9. I'm not sure where this script was inspired from. I think I must have
  10. written it from scratch, though it's been several years now.
  11. """
  12. __author__ = 'Micah Elliott http://MicahElliott.com'
  13. __version__ = '0.1'
  14. __copyright__ = 'Copyright (C) 2011 Micah Elliott. All rights reserved.'
  15. __license__ = 'WTFPL http://sam.zoy.org/wtfpl/'
  16. #---------------------------------------------------------------------
  17. import sys, re
  18. CLUT = [ # color look-up table
  19. # 8-bit, RGB hex
  20. # Primary 3-bit (8 colors). Unique representation!
  21. ('00', '000000'),
  22. ('01', '800000'),
  23. ('02', '008000'),
  24. ('03', '808000'),
  25. ('04', '000080'),
  26. ('05', '800080'),
  27. ('06', '008080'),
  28. ('07', 'c0c0c0'),
  29. # Equivalent "bright" versions of original 8 colors.
  30. ('08', '808080'),
  31. ('09', 'ff0000'),
  32. ('10', '00ff00'),
  33. ('11', 'ffff00'),
  34. ('12', '0000ff'),
  35. ('13', 'ff00ff'),
  36. ('14', '00ffff'),
  37. ('15', 'ffffff'),
  38. # Strictly ascending.
  39. ('16', '000000'),
  40. ('17', '00005f'),
  41. ('18', '000087'),
  42. ('19', '0000af'),
  43. ('20', '0000d7'),
  44. ('21', '0000ff'),
  45. ('22', '005f00'),
  46. ('23', '005f5f'),
  47. ('24', '005f87'),
  48. ('25', '005faf'),
  49. ('26', '005fd7'),
  50. ('27', '005fff'),
  51. ('28', '008700'),
  52. ('29', '00875f'),
  53. ('30', '008787'),
  54. ('31', '0087af'),
  55. ('32', '0087d7'),
  56. ('33', '0087ff'),
  57. ('34', '00af00'),
  58. ('35', '00af5f'),
  59. ('36', '00af87'),
  60. ('37', '00afaf'),
  61. ('38', '00afd7'),
  62. ('39', '00afff'),
  63. ('40', '00d700'),
  64. ('41', '00d75f'),
  65. ('42', '00d787'),
  66. ('43', '00d7af'),
  67. ('44', '00d7d7'),
  68. ('45', '00d7ff'),
  69. ('46', '00ff00'),
  70. ('47', '00ff5f'),
  71. ('48', '00ff87'),
  72. ('49', '00ffaf'),
  73. ('50', '00ffd7'),
  74. ('51', '00ffff'),
  75. ('52', '5f0000'),
  76. ('53', '5f005f'),
  77. ('54', '5f0087'),
  78. ('55', '5f00af'),
  79. ('56', '5f00d7'),
  80. ('57', '5f00ff'),
  81. ('58', '5f5f00'),
  82. ('59', '5f5f5f'),
  83. ('60', '5f5f87'),
  84. ('61', '5f5faf'),
  85. ('62', '5f5fd7'),
  86. ('63', '5f5fff'),
  87. ('64', '5f8700'),
  88. ('65', '5f875f'),
  89. ('66', '5f8787'),
  90. ('67', '5f87af'),
  91. ('68', '5f87d7'),
  92. ('69', '5f87ff'),
  93. ('70', '5faf00'),
  94. ('71', '5faf5f'),
  95. ('72', '5faf87'),
  96. ('73', '5fafaf'),
  97. ('74', '5fafd7'),
  98. ('75', '5fafff'),
  99. ('76', '5fd700'),
  100. ('77', '5fd75f'),
  101. ('78', '5fd787'),
  102. ('79', '5fd7af'),
  103. ('80', '5fd7d7'),
  104. ('81', '5fd7ff'),
  105. ('82', '5fff00'),
  106. ('83', '5fff5f'),
  107. ('84', '5fff87'),
  108. ('85', '5fffaf'),
  109. ('86', '5fffd7'),
  110. ('87', '5fffff'),
  111. ('88', '870000'),
  112. ('89', '87005f'),
  113. ('90', '870087'),
  114. ('91', '8700af'),
  115. ('92', '8700d7'),
  116. ('93', '8700ff'),
  117. ('94', '875f00'),
  118. ('95', '875f5f'),
  119. ('96', '875f87'),
  120. ('97', '875faf'),
  121. ('98', '875fd7'),
  122. ('99', '875fff'),
  123. ('100', '878700'),
  124. ('101', '87875f'),
  125. ('102', '878787'),
  126. ('103', '8787af'),
  127. ('104', '8787d7'),
  128. ('105', '8787ff'),
  129. ('106', '87af00'),
  130. ('107', '87af5f'),
  131. ('108', '87af87'),
  132. ('109', '87afaf'),
  133. ('110', '87afd7'),
  134. ('111', '87afff'),
  135. ('112', '87d700'),
  136. ('113', '87d75f'),
  137. ('114', '87d787'),
  138. ('115', '87d7af'),
  139. ('116', '87d7d7'),
  140. ('117', '87d7ff'),
  141. ('118', '87ff00'),
  142. ('119', '87ff5f'),
  143. ('120', '87ff87'),
  144. ('121', '87ffaf'),
  145. ('122', '87ffd7'),
  146. ('123', '87ffff'),
  147. ('124', 'af0000'),
  148. ('125', 'af005f'),
  149. ('126', 'af0087'),
  150. ('127', 'af00af'),
  151. ('128', 'af00d7'),
  152. ('129', 'af00ff'),
  153. ('130', 'af5f00'),
  154. ('131', 'af5f5f'),
  155. ('132', 'af5f87'),
  156. ('133', 'af5faf'),
  157. ('134', 'af5fd7'),
  158. ('135', 'af5fff'),
  159. ('136', 'af8700'),
  160. ('137', 'af875f'),
  161. ('138', 'af8787'),
  162. ('139', 'af87af'),
  163. ('140', 'af87d7'),
  164. ('141', 'af87ff'),
  165. ('142', 'afaf00'),
  166. ('143', 'afaf5f'),
  167. ('144', 'afaf87'),
  168. ('145', 'afafaf'),
  169. ('146', 'afafd7'),
  170. ('147', 'afafff'),
  171. ('148', 'afd700'),
  172. ('149', 'afd75f'),
  173. ('150', 'afd787'),
  174. ('151', 'afd7af'),
  175. ('152', 'afd7d7'),
  176. ('153', 'afd7ff'),
  177. ('154', 'afff00'),
  178. ('155', 'afff5f'),
  179. ('156', 'afff87'),
  180. ('157', 'afffaf'),
  181. ('158', 'afffd7'),
  182. ('159', 'afffff'),
  183. ('160', 'd70000'),
  184. ('161', 'd7005f'),
  185. ('162', 'd70087'),
  186. ('163', 'd700af'),
  187. ('164', 'd700d7'),
  188. ('165', 'd700ff'),
  189. ('166', 'd75f00'),
  190. ('167', 'd75f5f'),
  191. ('168', 'd75f87'),
  192. ('169', 'd75faf'),
  193. ('170', 'd75fd7'),
  194. ('171', 'd75fff'),
  195. ('172', 'd78700'),
  196. ('173', 'd7875f'),
  197. ('174', 'd78787'),
  198. ('175', 'd787af'),
  199. ('176', 'd787d7'),
  200. ('177', 'd787ff'),
  201. ('178', 'd7af00'),
  202. ('179', 'd7af5f'),
  203. ('180', 'd7af87'),
  204. ('181', 'd7afaf'),
  205. ('182', 'd7afd7'),
  206. ('183', 'd7afff'),
  207. ('184', 'd7d700'),
  208. ('185', 'd7d75f'),
  209. ('186', 'd7d787'),
  210. ('187', 'd7d7af'),
  211. ('188', 'd7d7d7'),
  212. ('189', 'd7d7ff'),
  213. ('190', 'd7ff00'),
  214. ('191', 'd7ff5f'),
  215. ('192', 'd7ff87'),
  216. ('193', 'd7ffaf'),
  217. ('194', 'd7ffd7'),
  218. ('195', 'd7ffff'),
  219. ('196', 'ff0000'),
  220. ('197', 'ff005f'),
  221. ('198', 'ff0087'),
  222. ('199', 'ff00af'),
  223. ('200', 'ff00d7'),
  224. ('201', 'ff00ff'),
  225. ('202', 'ff5f00'),
  226. ('203', 'ff5f5f'),
  227. ('204', 'ff5f87'),
  228. ('205', 'ff5faf'),
  229. ('206', 'ff5fd7'),
  230. ('207', 'ff5fff'),
  231. ('208', 'ff8700'),
  232. ('209', 'ff875f'),
  233. ('210', 'ff8787'),
  234. ('211', 'ff87af'),
  235. ('212', 'ff87d7'),
  236. ('213', 'ff87ff'),
  237. ('214', 'ffaf00'),
  238. ('215', 'ffaf5f'),
  239. ('216', 'ffaf87'),
  240. ('217', 'ffafaf'),
  241. ('218', 'ffafd7'),
  242. ('219', 'ffafff'),
  243. ('220', 'ffd700'),
  244. ('221', 'ffd75f'),
  245. ('222', 'ffd787'),
  246. ('223', 'ffd7af'),
  247. ('224', 'ffd7d7'),
  248. ('225', 'ffd7ff'),
  249. ('226', 'ffff00'),
  250. ('227', 'ffff5f'),
  251. ('228', 'ffff87'),
  252. ('229', 'ffffaf'),
  253. ('230', 'ffffd7'),
  254. ('231', 'ffffff'),
  255. # Gray-scale range.
  256. ('232', '080808'),
  257. ('233', '121212'),
  258. ('234', '1c1c1c'),
  259. ('235', '262626'),
  260. ('236', '303030'),
  261. ('237', '3a3a3a'),
  262. ('238', '444444'),
  263. ('239', '4e4e4e'),
  264. ('240', '585858'),
  265. ('241', '626262'),
  266. ('242', '6c6c6c'),
  267. ('243', '767676'),
  268. ('244', '808080'),
  269. ('245', '8a8a8a'),
  270. ('246', '949494'),
  271. ('247', '9e9e9e'),
  272. ('248', 'a8a8a8'),
  273. ('249', 'b2b2b2'),
  274. ('250', 'bcbcbc'),
  275. ('251', 'c6c6c6'),
  276. ('252', 'd0d0d0'),
  277. ('253', 'dadada'),
  278. ('254', 'e4e4e4'),
  279. ('255', 'eeeeee'),
  280. ]
  281. def _str2hex(hexstr):
  282. return int(hexstr, 16)
  283. def _strip_hash(rgb):
  284. # Strip leading `#` if exists.
  285. if rgb.startswith('#'):
  286. rgb = rgb.lstrip('#')
  287. return rgb
  288. def _create_dicts():
  289. short2rgb_dict = dict(CLUT)
  290. rgb2short_dict = {}
  291. for k, v in short2rgb_dict.items():
  292. rgb2short_dict[v] = k
  293. return rgb2short_dict, short2rgb_dict
  294. def short2rgb(short):
  295. return SHORT2RGB_DICT[short]
  296. def print_all():
  297. """ Print all 256 xterm color codes.
  298. """
  299. for short, rgb in CLUT:
  300. sys.stdout.write('\033[48;5;%sm%s:%s' % (short, short, rgb))
  301. sys.stdout.write("\033[0m ")
  302. sys.stdout.write('\033[38;5;%sm%s:%s' % (short, short, rgb))
  303. sys.stdout.write("\033[0m\n")
  304. print "Printed all codes."
  305. print "You can translate a hex or 0-255 code by providing an argument."
  306. def rgb2short(rgb):
  307. """ Find the closest xterm-256 approximation to the given RGB value.
  308. @param rgb: Hex code representing an RGB value, eg, 'abcdef'
  309. @returns: String between 0 and 255, compatible with xterm.
  310. >>> rgb2short('123456')
  311. ('23', '005f5f')
  312. >>> rgb2short('ffffff')
  313. ('231', 'ffffff')
  314. >>> rgb2short('0DADD6') # vimeo logo
  315. ('38', '00afd7')
  316. """
  317. rgb = _strip_hash(rgb)
  318. incs = (0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff)
  319. # Break 6-char RGB code into 3 integer vals.
  320. parts = [ int(h, 16) for h in re.split(r'(..)(..)(..)', rgb)[1:4] ]
  321. res = []
  322. for part in parts:
  323. i = 0
  324. while i < len(incs)-1:
  325. s, b = incs[i], incs[i+1] # smaller, bigger
  326. if s <= part <= b:
  327. s1 = abs(s - part)
  328. b1 = abs(b - part)
  329. if s1 < b1: closest = s
  330. else: closest = b
  331. res.append(closest)
  332. break
  333. i += 1
  334. #print '***', res
  335. res = ''.join([ ('%02.x' % i) for i in res ])
  336. equiv = RGB2SHORT_DICT[ res ]
  337. #print '***', res, equiv
  338. return equiv, res
  339. RGB2SHORT_DICT, SHORT2RGB_DICT = _create_dicts()
  340. #---------------------------------------------------------------------
  341. if __name__ == '__main__':
  342. import doctest
  343. doctest.testmod()
  344. if len(sys.argv) == 1:
  345. print_all()
  346. raise SystemExit
  347. arg = sys.argv[1]
  348. if len(arg) < 4 and int(arg) < 256:
  349. rgb = short2rgb(arg)
  350. sys.stdout.write('xterm color \033[38;5;%sm%s\033[0m -> RGB exact \033[38;5;%sm%s\033[0m' % (arg, arg, arg, rgb))
  351. sys.stdout.write("\033[0m\n")
  352. else:
  353. short, rgb = rgb2short(arg)
  354. sys.stdout.write('RGB %s -> xterm color approx \033[38;5;%sm%s (%s)' % (arg, short, short, rgb))
  355. sys.stdout.write("\033[0m\n")