def add_color(self, color=None): ''' ''' if color is None: color = self.labColorRep.cget('bg') if self.type.get() == 'Qualitative': if color in list(self.saveColors.values()): tk.messagebox.showinfo( 'Alread iny ..', 'Color already in ' + 'palette. You can delete colors ' + 'by right-clicking on bars. To change ' + 'the psoition of a color move the bars' + ' to the correct position in the graph.', parent=self.toplevel) return for n in range(len(self.bars) + 5): if n not in self.saveColors and n in self.bars: break elif n not in self.bars: self.add_rectangle(n) break self.bars[n].set_facecolor(color) self.saveColors[n] = color self.figure.canvas.draw() elif 'Gradient' in self.type.get(): if 'sequential' in self.type.get(): self.get_cmap_by_color(color) self.color_bars_by_gradient() else: if self.firstColor is None: self.firstColor = color self.firstColorSaved = color self.get_cmap_by_color(color, reverse=True) self.color_bars_by_gradient(how='half') else: col1 = husl.hex_to_husl(self.firstColor) col2 = husl.hex_to_husl(color) s, l = self.get_sl_from_entry() if s is None: return cmapOutput = sns.diverging_palette( col1[0], col2[0], s=s, l=l, as_cmap=True, center=self.center.get()) self.cmap = matplotlib.cm.get_cmap(cmapOutput) self.firstColor = None self.color_bars_by_gradient()
def test_snapshot(self): for hex_color, colors in self.snapshot.items(): # Test forward functions test_rgb = husl.hex_to_rgb(hex_color) self.assertTuplesClose(test_rgb, colors['rgb']) test_xyz = husl.rgb_to_xyz(test_rgb) self.assertTuplesClose(test_xyz, colors['xyz']) test_luv = husl.xyz_to_luv(test_xyz) self.assertTuplesClose(test_luv, colors['luv']) test_lch = husl.luv_to_lch(test_luv) self.assertTuplesClose(test_lch, colors['lch']) test_husl = husl.lch_to_husl(test_lch) self.assertTuplesClose(test_husl, colors['husl']) test_huslp = husl.lch_to_huslp(test_lch) self.assertTuplesClose(test_huslp, colors['huslp']) # Test backward functions test_lch = husl.husl_to_lch(colors['husl']) self.assertTuplesClose(test_lch, colors['lch']) test_lch = husl.huslp_to_lch(colors['huslp']) self.assertTuplesClose(test_lch, colors['lch']) test_luv = husl.lch_to_luv(test_lch) self.assertTuplesClose(test_luv, colors['luv']) test_xyz = husl.luv_to_xyz(test_luv) self.assertTuplesClose(test_xyz, colors['xyz']) test_rgb = husl.xyz_to_rgb(test_xyz) self.assertTuplesClose(test_rgb, colors['rgb']) self.assertEqual(husl.rgb_to_hex(test_rgb), hex_color) # Full test self.assertEqual(husl.husl_to_hex(*colors['husl']), hex_color) self.assertTuplesClose(husl.hex_to_husl(hex_color), colors['husl']) self.assertEqual(husl.huslp_to_hex(*colors['huslp']), hex_color) self.assertTuplesClose(husl.hex_to_huslp(hex_color), colors['huslp'])
def update_sl(self, color): ''' ''' if color is not None: huslColor = husl.hex_to_husl(color) self.slVars['s'].set(huslColor[1]) self.slVars['l'].set(huslColor[2])
def test_snapshot(self): for h in range(37): for s in range(21): for l in range(21): H = h * 10.0 S = s * 5.0 L = l * 5.0 test = husl.husl_to_hex(H, S, L) correct = self.snapshot['husl'][h][s][l] self.assertEqual(test, correct) test2 = husl.husl_to_hex(*husl.hex_to_husl(correct)) self.assertEqual(test2, correct)
def update_hex_by_husl(self, event): ''' ''' color = self.labColorRep.cget('bg') huslColorHue = husl.hex_to_husl(color)[0] s, l = self.get_sl_from_entry() if s is None: return hex = husl.husl_to_hex(huslColorHue, s, l) self.labColorRep.configure(bg=hex) paletteType = self.type.get() if paletteType != 'Qualitative': if 'diverging' in paletteType: self.firstColor = str(self.firstColorSaved) self.add_color() else: self.add_color()
import matplotlib.pyplot as plt import seaborn as sns from palettable import cartocolors from husl import hex_to_husl colours = cartocolors.qualitative.Safe_10.hex_colors cpalette = sns.color_palette(colours) cpalette_light = sns.light_palette(hex_to_husl(colours[1]), input="husl") cmap_qualitative = cartocolors.qualitative.Safe_10.mpl_colormap cmap_light = sns.light_palette(hex_to_husl(colours[1]), input="husl", as_cmap=True) cmap_diverging = sns.diverging_palette(h_neg=227, h_pos=4, s=73, l=60, n=12, as_cmap=True) cmap_diverging.set_bad('white') pltmarkers = ['o', 'X', 'D', 'P', 'X'] def set_style(): sns.set_palette(cpalette) sns.set_style( 'darkgrid', { 'axes.spines.right': False, 'axes.spines.top': False, 'axes.facecolor': '.93'
def plt_matches(data, metric, between='Generative Model', by='Attack Classifier', cols='Feature Set', n=15): between_groups = list(data.groupby(between).groups.keys()) combs = [c for c in combinations_with_replacement(between_groups, 2)] if cols == 'Generative Model': filtercmap = GMCMAP elif cols == 'Attack Classifier': filtercmap = ATTACKSCMAP elif cols == 'Feature Set': filtercmap = FEATURECMAP else: raise ValueError(f'Unknown groupby {between}') plots = [] for fg, fgroup in data.groupby(cols): groups = fgroup.groupby(by) nrows = int(ceil(len(groups) / 2)) fig, ax = plt.subplots(nrows, 2, figsize=(10, nrows * 4), sharex=True, sharey=True) ax = ax.flatten() for i, (g, group) in enumerate(groups): subgroups = group.groupby(between) pltdata = DataFrame(columns=between_groups, index=between_groups) prey = {} for sg, sgroup in subgroups: prey[sg] = set( sgroup.sort_values(metric)[['Target' ]].iloc[-n:].values.flatten()) for c in combs: matches = len(prey[c[0]].intersection(prey[c[1]])) pltdata.loc[c[0], c[1]] = matches pltdata.loc[c[1], c[0]] = matches try: cmap = sns.light_palette(hex_to_husl(filtercmap[fg]), input="husl", as_cmap=True) except: cmap = cmap_light sns.heatmap(pltdata, ax=ax[i], square=True, annot=True, vmin=0, vmax=n, cmap=cmap) ax[i].set(title=g) ax[i].set_xticklabels([x for x in ax[i].get_xticklabels()], rotation=45, ha='right') if bool(len(groups) % 2): ax[-1].axis('off') fig.suptitle( f'Matches between {n} most vulnerable targets by {by} for {cols} {fg}', y=1.05) plots.append(fig) return plots