def test_from_hsl(self): c = HSL(10, 0.1, 0.2) c2 = bcr.RGB.from_hsl(c) assert c2 is not c assert c2.a == 1.0 assert c2.r == 56 assert c2.g == 48 assert c2.b == 46 c = HSL(10, 0.1, 0.2, 0.3) c2 = bcr.RGB.from_hsl(c) assert c2 is not c assert c2.a == 0.3 assert c2.r == 56 assert c2.g == 48 assert c2.b == 46
def tree_to_wedge_data(data, tree, level=0, total=None, subtotal=0, ancestor_subtotal=None, threshold=0.0025): if total is None: total = float(tree.count) initial_subtotal = subtotal final_subtotal = subtotal + tree.count item_list = tree.key_to_subtree.items() subtotal_for_color = initial_subtotal if level == 0 else ancestor_subtotal for key, subtree in sorted(item_list, key=tree_item_count, reverse=True): if subtree.count / total <= threshold: break subtotal_begin = subtotal subtotal_end = subtotal + subtree.count if level == 0: subtotal_for_color = subtotal_begin elif (subtotal_end - subtotal_begin) / (final_subtotal - initial_subtotal) >= 0.66: subtotal_for_color = ancestor_subtotal else: subtotal_for_color = fmod(subtotal_for_color + 0.1 * total, total) data.append({ 'inner_radius': level, 'outer_radius': level + 1, 'start_angle': (subtotal / total) * 2 * pi, 'end_angle': (subtotal_end / total) * 2 * pi, 'fill_color': color_for_wedge(level, total, subtotal_for_color), 'name': key }) tree_to_wedge_data(data, subtree, level=level + 1, total=total, subtotal=subtotal, ancestor_subtotal=subtotal_for_color, threshold=threshold) subtotal += subtree.count ancestor_subtotal = None if False: data.append({ 'inner_radius': level, 'outer_radius': level + 1, 'start_angle': (subtotal / total) * 2 * pi, 'end_angle': (final_subtotal / total) * 2 * pi, 'fill_color': HSL(0, 1, 1), 'name': '(other)' })
def color_for_wedge(level, total, subtotal_for_color): return HSL( int(256 * 0.99999 * (subtotal_for_color / total)), 1.0, # 1.0 - subtree.count/float(tree.count), # sat [0,1] 0.5, # 0.4+(level*0.2)/6, # light [0=black,1/2=sat,1=white] 1.0) # alpha
def my_colors(self,ii,nn): return HSL(360*(ii+1)/nn,1,0.4).to_rgb().to_hex()
def string_to_rgb(name): value = sum(bytearray(name, 'utf-8')) hue = round(value % 360 / 10) * 10 # print('{}: {}, {}'.format(name, value, hue)) return HSL(hue, 0.75, 0.8)
from bokeh.models.ranges import FactorRange, DataRange1d from bokeh.plotting import figure from bokeh.sampledata.commits import data from bokeh.transform import factor_cmap from bokeh.colors import HSL from bokeh.layouts import row, column from bokeh.themes import built_in_themes import math from hexagonal_clusters import hex_y, hex_x, delta_x, delta_y, scale_factor from nobel_data import nobel from bokeh.models.tools import BoxZoomTool, PanTool, WheelZoomTool, ResetTool, HoverTool output_file('nobel.html') cmap = { "Chemistry": HSL(0, 0.5, 0.5).to_rgb().to_hex(), "Medicine": HSL(120, 0.5, 0.5).to_rgb().to_hex(), "Physics": HSL(180, 0.5, 0.5).to_rgb().to_hex(), "Peace": HSL(240, 0.5, 0.5).to_rgb().to_hex(), "Economics": HSL(300, 0.5, 0.5).to_rgb().to_hex(), "Literature": HSL(60, 0.5, 0.5).to_rgb().to_hex() } source = ColumnDataSource(data={ "x": [], "y": [], "Category": nobel['Category'] }) aspect_ratio = 2.4 p = figure(title="Nobel Laureates",