D3 calendar visualization of wiki and git edits.

make_cmaps.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import webcolors
  2. import cmocean
  3. def main():
  4. purple_to_orange()
  5. def purple_to_orange():
  6. """Dark purple to bright orange."""
  7. start_hex = "#4d2b4b"
  8. mid1_hex = "#8c6bb1"
  9. mid2_hex = "#fdae6b"
  10. end_hex = "#f16913"
  11. start_color = [j/255 for j in webcolors.hex_to_rgb(start_hex)]
  12. mid1_color = [j/255 for j in webcolors.hex_to_rgb(mid1_hex)]
  13. mid2_color = [j/255 for j in webcolors.hex_to_rgb(mid2_hex)]
  14. end_color = [j/255 for j in webcolors.hex_to_rgb(end_hex)]
  15. colors = [start_color, mid1_color, mid2_color, end_color]
  16. position = [0, 0.5, 0.6, 1]
  17. cm = make_cmap(colors, position=position)
  18. # Now just call cm(0.0) thru cm(1.0)
  19. N = 9
  20. hex_colorz = []
  21. for i in range(N+1):
  22. x = i/N
  23. rgbd_color = cm(x)[0:3]
  24. rgb_color = [int(c*255) for c in rgbd_color]
  25. hex_color = webcolors.rgb_to_hex(rgb_color)
  26. hex_colorz.append(hex_color)
  27. print(hex_colorz_to_string(hex_colorz))
  28. def dark_sea_to_candy_red():
  29. """Dark sea green to candy red."""
  30. start_hex = "#004a4a"
  31. mid_hex = "#1fb0b0"
  32. end_hex = "#ff1717"
  33. start_color = [j/255 for j in webcolors.hex_to_rgb(start_hex)]
  34. mid_color = [j/255 for j in webcolors.hex_to_rgb(mid_hex)]
  35. end_color = [j/255 for j in webcolors.hex_to_rgb(end_hex)]
  36. colors = [start_color, mid_color, end_color]
  37. position = [0, 0.3, 1]
  38. cm = make_cmap(colors, position=position)
  39. # Now just call cm(0.0) thru cm(1.0)
  40. N = 9
  41. hex_colorz = []
  42. for i in range(N+1):
  43. x = i/N
  44. rgbd_color = cm(x)[0:3]
  45. rgb_color = [int(c*255) for c in rgbd_color]
  46. hex_color = webcolors.rgb_to_hex(rgb_color)
  47. hex_colorz.append(hex_color)
  48. print(hex_colorz_to_string(hex_colorz))
  49. def ocean():
  50. """Construct a colorap from cmocean colormaps."""
  51. cmdict = cmocean.tools.get_dict(cmocean.cm.ice, N=9)
  52. for colorclass in cmdict.keys():
  53. print("-"*20)
  54. print(colorclass)
  55. colors = cmdict[colorclass]
  56. hex_colorz = []
  57. for color in colors:
  58. color_triplet = (int(x*255) for x in color)
  59. hex_color = webcolors.rgb_to_hex(color_triplet)
  60. hex_colorz.append(hex_color)
  61. print(hex_colorz_to_string(hex_colorz))
  62. def make_cmap(colors, position=None, bit=False):
  63. '''
  64. make_cmap takes a list of tuples which contain RGB values. The RGB
  65. values may either be in 8-bit [0 to 255] (in which bit must be set to
  66. True when called) or arithmetic [0 to 1] (default). make_cmap returns
  67. a cmap with equally spaced colors.
  68. Arrange your tuples so that the first color is the lowest value for the
  69. colorbar and the last is the highest.
  70. position contains values from 0 to 1 to dictate the location of each color.
  71. '''
  72. import matplotlib as mpl
  73. import numpy as np
  74. bit_rgb = np.linspace(0,1,256)
  75. if position == None:
  76. position = np.linspace(0,1,len(colors))
  77. else:
  78. if len(position) != len(colors):
  79. sys.exit("position length must be the same as colors")
  80. elif position[0] != 0 or position[-1] != 1:
  81. sys.exit("position must start with 0 and end with 1")
  82. if bit:
  83. for i in range(len(colors)):
  84. colors[i] = (bit_rgb[colors[i][0]],
  85. bit_rgb[colors[i][1]],
  86. bit_rgb[colors[i][2]])
  87. cdict = {'red':[], 'green':[], 'blue':[]}
  88. for pos, color in zip(position, colors):
  89. cdict['red'].append((pos, color[0], color[0]))
  90. cdict['green'].append((pos, color[1], color[1]))
  91. cdict['blue'].append((pos, color[2], color[2]))
  92. cmap = mpl.colors.LinearSegmentedColormap('my_colormap',cdict,256)
  93. return cmap
  94. def hex_colorz_to_string(hex_colorz):
  95. """Utility method: turn a list of hex colros into a Javascript string. """
  96. finalstring = []
  97. for hex_color in hex_colorz:
  98. finalstring.append("\"%s\""%(hex_color))
  99. result = " .range([" + ",".join(finalstring) + "]);"
  100. return result
  101. if __name__=="__main__":
  102. main()