-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorpicker.py
More file actions
129 lines (89 loc) · 3.9 KB
/
colorpicker.py
File metadata and controls
129 lines (89 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable, Size
from matplotlib.axes import Axes
import numpy as np
from colorviewer import *
class ColorPicker(object):
def __init__(self, ax, cview_args = { }, display_args = { }, colormap = [ ]):
cview_ax = Axes(ax.get_figure(), ax.get_position(original = True))
cmap_ax = Axes(ax.get_figure(), ax.get_position(original = True))
self.ax = ax
self.cview = ColorViewer(cview_ax, **cview_args)
self.colormap = MapDisplay(cmap_ax, self.cview, **display_args)
for name, color in colormap:
self.colormap.add_item(name, color)
divider = make_axes_locatable(ax)
pad = Size.Fixed(0.1)
colormap_width = Size.Fraction(0.29, Size.AxesX(ax))
cview_width = Size.Fraction(0.7, Size.AxesX(ax))
divider.set_horizontal([ colormap_width, pad, cview_width ])
cmap_ax.set_axes_locator(divider.new_locator(nx = 0, ny = 0))
ax.figure.add_axes(cmap_ax)
cview_ax.set_axes_locator(divider.new_locator(nx = 2, ny = 0))
ax.figure.add_axes(cview_ax)
ax.tick_params(left = False, bottom = False, labelleft = False, labelbottom = False)
ax.set_axis_off()
def add_item(self, name, color):
self.colormap.add_item(name, color)
def update_width(self):
self.colormap.update_width()
def get_dict(self):
return dict([ (name, item.rgb) for name, item in self.colormap.colors.iteritems() ])
class MapDisplay(object):
def __init__(self, ax, cview, font_size = 8, pad = 4):
self.ax = ax
ax.tick_params(left = False, bottom = False, labelleft = False, labelbottom = False)
ax.set_aspect("equal")
ax.set_anchor("N")
self.cview = cview
self.font_size = font_size
self.row_sz = 1.0 / (self.ax.figure.get_dpi() / (font_size + pad))
self.rect_height = self.row_sz * 0.8
self.rect_width = self.rect_height * 1.5
self.colors = { }
def add_item(self, name, color = None):
self.colors[name] = DisplayItem(self, name, color)
def update_width(self):
transform = self.ax.transData.inverted()
width = max([ transform.transform(text.get_window_extent())[1, 0] for text in self.ax.texts ])
self.ax.set_xlim(0, width + 0.05)
class DisplayItem(object):
def __init__(self, display, name, color = None):
row = len(display.colors) + 1
offset_y = 1.0 - (display.row_sz * row)
if color is None:
props = { "ec": (0.0, 0.0, 0.0), "fc": (1.0, 1.0, 1.0) }
else:
props = { "color": color }
self.text = display.ax.text(display.rect_width + 0.1, offset_y + (display.row_sz / 2.0), name, va = "center", ha = "left")
self.rect = plt.Rectangle((0.05, offset_y), display.rect_width, display.rect_height, **props)
display.ax.add_patch(self.rect)
display.ax.set_ylim(offset_y - display.row_sz * 0.5, 1.0)
self.connect()
self.cview = display.cview
@property
def rgb(self):
return self.rect.get_fc()[:3]
def set_color(self, color):
self.rect.set_color(color)
def on_press(self, event):
if event.inaxes != self.rect.axes:
return
contains, attrd = self.rect.contains(event)
if not contains:
return
if event.button == 1:
color = self.rgb
try:
self.cview.highlight(color)
except:
pass
elif event.button == 3:
color = self.cview.selected
if color is not None:
self.set_color(color)
self.rect.figure.canvas.draw()
def connect(self):
self.cidpress = self.rect.figure.canvas.mpl_connect('button_press_event', self.on_press)
def disconnect(self):
self.rect.figure.canvas.mpl_disconnect(self.cidpress)