forked from blenderskool/kaleidoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_picker.py
255 lines (227 loc) · 9.9 KB
/
color_picker.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Advnced ColorRamp Color Picker Tool
import bpy
import blf
import math
import bgl
from . import spectrum
node_tree_type = None
start_draw = False
x_loc = 0
y_loc = 0
def draw_call(self, context):
global x_loc
global y_loc
width = 30
height = 30
"""if start_draw == True:
bgl.glEnable(bgl.GL_BLEND)
bgl.glLineWidth(4)
bgl.glBegin(bgl.GL_LINE_STRIP)
bgl.glColor4f(0.0, 0.0, 0.0, 0.75)
for x, y in self.draw_mouse_path:
bgl.glVertex2i(x, y)
bgl.glEnd()"""
bgl.glEnable(bgl.GL_BLEND)
bgl.glColor4f(1.0, 1.0, 1.0, 0.98)
font_id = 0 # XXX, need to find out how best to get this.
# draw some text
blf.position(font_id, 25, 20, 0)
blf.size(font_id, 20, 57)
blf.draw(font_id, "Press Enter to Confirm")
blf.position(font_id, 40, 7, 0)
blf.size(font_id, 20, 40)
blf.draw(font_id, "Kaleidoscope Modal 0.1")
bgl.glBegin(bgl.GL_QUADS)
bgl.glColor3f(c[0][0], c[0][1], c[0][2])
bgl.glVertex3f(x_loc+10,y_loc+10,0)
bgl.glVertex3f(x_loc+10+width,y_loc+10,0)
bgl.glVertex3f(x_loc+10+width,y_loc+height+10,0)
bgl.glVertex3f(x_loc+10,y_loc+height+10,0)
bgl.glEnd()
if c[0][0] < 0.1 and c[0][1] < 0.1 and c[0][2] < 0.1:
bgl.glColor3f(1, 1, 1)
else:
bgl.glColor3f(0, 0, 0)
bgl.glEnable(bgl.GL_BLEND)
bgl.glLineWidth(1)
bgl.glBegin(bgl.GL_LINE_STRIP)
bgl.glVertex2f(x_loc+10-1, y_loc+10-1)
bgl.glVertex2f(x_loc+10+width+1, y_loc+10-1)
bgl.glVertex2f(x_loc+10+width, y_loc+10+height+1)
bgl.glVertex2f(x_loc+10-1, y_loc+10+height)
bgl.glVertex2f(x_loc+10-1, y_loc+10-1)
bgl.glEnd()
c = bgl.Buffer(bgl.GL_FLOAT, [1,3])
selected_col = bgl.Buffer(bgl.GL_FLOAT, [1,3])
col_list = []
class ModalPickerOperator(bpy.types.Operator):
"""Kaleidoscope Color Picker Tool"""
bl_idname = "kaleidoscope.color_picker"
bl_label = "Kaleidoscope Color Picker Modal"
def modal(self, context, event):
context.area.tag_redraw()
global start_draw
if event.type == 'MOUSEMOVE':
global x_loc
global y_loc
x_loc = event.mouse_region_x
y_loc = event.mouse_region_y
bgl.glReadPixels(event.mouse_x, event.mouse_y,1,1,bgl.GL_RGB,bgl.GL_FLOAT,c)
if start_draw == True:
self.draw_mouse_path.append((event.mouse_region_x, event.mouse_region_y))
self.mouse_color_path.append((event.mouse_x, event.mouse_y))
else:
self.draw_mouse_path.clear()
if event.type == 'LEFTMOUSE':
if event.value == 'PRESS':
start_draw = True
else:
start_draw = False
#if self.cursor_set:
#context.window.cursor_modal_restore()
#bpy.types.SpaceImageEditor.draw_handler_remove(self._handle, 'WINDOW')
for x, y in self.mouse_color_path:
bgl.glReadPixels(x, y,1,1,bgl.GL_RGB,bgl.GL_FLOAT,selected_col)
col_list.append((selected_col[0][0], selected_col[0][1], selected_col[0][2]))
total_colors = len(col_list)
if total_colors > 32:
total_colors = 32
if node_tree_type == 'OBJECT':
# Objects
if bpy.context.object.active_material.node_tree.nodes.active.type == 'VALTORGB':
node = bpy.context.object.active_material.node_tree.nodes.active
elif node_tree_type == 'WORLD':
# World
if bpy.context.scene.world.node_tree.nodes.active.type == 'VALTORGB':
node = bpy.context.scene.world.node_tree.nodes.active
elif node_tree_type == 'LAMP':
# Lamps
for lamp in bpy.data.lamps:
if lamp.name == bpy.context.active_object.name:
if lamp.node_tree.nodes.active.type == 'VALTORGB':
node = lamp.node_tree.nodes.active
break
while len(node.color_ramp.elements) > 1:
node.color_ramp.elements.remove(node.color_ramp.elements[0])
i=1
color1 = None
for color in col_list:
if i > 1:
color_rgb = (int(color[0]*255), int(color[1]*255), int(color[2]*255))
res = math.pow((color_rgb[0]-color1[0]), 2) + math.pow((color_rgb[1]-color1[1]), 2) + math.pow((color_rgb[2]-color1[2]), 2)
if res > 10:
color = spectrum.hex_to_rgb(spectrum.real_rgb_to_hex(color_rgb))
try:
element = node.color_ramp.elements.new(i/total_colors)
element.color[0] = color[0]
element.color[1] = color[1]
element.color[2] = color[2]
except:
self.report({'INFO'}, 'ColorRamp limit exceed, some colors have been removed')
color1 = color_rgb
i=i+1
else:
i=i+1
continue
elif i == 1:
color1 = (int(color[0]*255), int(color[1]*255), int(color[2]*255))
color = spectrum.hex_to_rgb(spectrum.real_rgb_to_hex(color1))
node.color_ramp.elements[0].position = i/total_colors
node.color_ramp.elements[0].color[0] = color[0]
node.color_ramp.elements[0].color[1] = color[1]
node.color_ramp.elements[0].color[2] = color[2]
i=i+1
elif event.type == 'ESC':
if self.cursor_set:
col_list.clear()
context.window.cursor_modal_restore()
bpy.types.SpaceImageEditor.draw_handler_remove(self._handle, 'WINDOW')
return {'CANCELLED'}
elif event.type in {'RET', 'RIGHTMOUSE'}:
if self.cursor_set:
col_list.clear()
context.window.cursor_modal_restore()
bpy.types.SpaceImageEditor.draw_handler_remove(self._handle, 'WINDOW')
return {'FINISHED'}
return {'RUNNING_MODAL'}
def invoke(self, context, event):
self.cursor_set = False
if context.area.type == 'IMAGE_EDITOR':
self.cursor_set = False
args = (self, context)
self.draw_mouse_path = []
self.mouse_color_path = []
self._handle = bpy.types.SpaceImageEditor.draw_handler_add(draw_call, args, 'WINDOW', 'POST_PIXEL')
context.window_manager.modal_handler_add(self)
bpy.context.window.cursor_modal_set('EYEDROPPER')
self.cursor_set = True
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "Image Editor not found, Color Picker will not run")
return {'CANCELLED'}
class ColorPickerButton(bpy.types.Operator):
"""Use the Kaleidoscope Color Picker to Sample an Image for colors"""
bl_idname = "kaleidoscope.color_picker_button"
bl_label = "Color Picker Button"
def execute(self, context):
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
image_editor = True
override = {'area': area, 'region': area.regions[-1]}
bpy.ops.kaleidoscope.color_picker(override, 'INVOKE_SCREEN')
break
return{'FINISHED'}
def color_picker_button_ui(self, context):
global node_tree_type
layout = self.layout
layout.separator()
box = layout.box()
box.label("Kaleidoscope Picker", icon='COLOR')
row = box.row(align=True)
if bpy.context.space_data.shader_type == 'OBJECT' and bpy.context.active_object.type != 'LAMP':
# Object
node_tree_type = 'OBJECT'
if bpy.context.object.active_material.node_tree.nodes.active.type == 'VALTORGB':
row.enabled = True
else:
row.enabled = False
elif bpy.context.space_data.shader_type == 'WORLD':
# World
node_tree_type = 'WORLD'
if bpy.context.scene.world.node_tree.nodes.active.type == 'VALTORGB':
row.enabled = True
else:
row.enabled = False
else:
# Lamps
for lamp in bpy.data.lamps:
if lamp.name == bpy.context.active_object.name:
node_tree_type = 'LAMP'
if bpy.context.scene.world.node_tree.nodes.active.type == 'VALTORGB':
row.enabled = True
else:
row.enabled = False
break
row.scale_y = 1.2
row.operator(ColorPickerButton.bl_idname, text='Use Color Picker', icon='EYEDROPPER')
if bpy.context.space_data.shader_type == 'OBJECT' and bpy.context.active_object.type != 'LAMP':
# Object
if bpy.context.object.active_material.node_tree.nodes.active.type != 'VALTORGB':
col = box.column(align=True)
col.label("Active Node is not")
col.label("a valid ColorRamp node.")
elif bpy.context.space_data.shader_type == 'WORLD':
# World
if bpy.context.scene.world.node_tree.nodes.active.type != 'VALTORGB':
col = box.column(align=True)
col.label("Active Node is not")
col.label("a valid ColorRamp node.")
else:
# Lamps
for lamp in bpy.data.lamps:
if lamp.name == bpy.context.active_object.name:
if bpy.context.scene.world.node_tree.nodes.active.type != 'VALTORGB':
col = box.column(align=True)
col.label("Active Node is not")
col.label("a valid ColorRamp node.")
break