Scraping data and statistics from charlesreid1.com/wiki

make_cmaps.py 3.8KB

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