-
Notifications
You must be signed in to change notification settings - Fork 385
/
sliders.py
324 lines (267 loc) · 9.67 KB
/
sliders.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# This file is part of MyPaint.
# Copyright (C) 2012-2018 by the MyPaint Development Team.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
"""Component sliders for power users.
"""
from __future__ import division, print_function
from lib.gibindings import Gtk
from lib.color import RGBColor
from lib.color import HSVColor
from lib.color import HCYColor
from .bases import IconRenderable
from .adjbases import ColorAdjuster
from .adjbases import SliderColorAdjuster
from .combined import CombinedAdjusterPage
from lib.gettext import C_
class ComponentSlidersAdjusterPage (CombinedAdjusterPage, IconRenderable):
"""Component sliders for precise adjustment: page for `CombinedAdjuster`.
"""
def __init__(self):
CombinedAdjusterPage.__init__(self)
grid = Gtk.Grid()
grid.set_size_request(150, -1)
grid.set_row_spacing(6)
grid.set_column_spacing(0)
grid.set_border_width(6)
self._sliders = [] #: List of slider widgets.
grid.set_valign(0.5)
grid.set_halign(0.5)
grid.set_hexpand(True)
grid.set_vexpand(False)
row_defs = [
(
C_("color sliders panel: red/green/blue: slider label", "R"),
RGBRedSlider,
0,
), (
C_("color sliders panel: red/green/blue: slider label", "G"),
RGBGreenSlider,
0,
), (
C_("color sliders panel: red/green/blue: slider label", "B"),
RGBBlueSlider,
0,
), (
C_("color sliders panel: hue/chroma/luma: slider label", "H"),
HCYHueSlider,
12,
), (
C_("color sliders panel: hue/chroma/luma: slider label", "C"),
HCYChromaSlider,
0,
), (
C_("color sliders panel: hue/chroma/luma: slider label", "Y"),
HCYLumaSlider,
0,
),
]
row = 0
for row_def in row_defs:
label_text, adj_class, margin_top = row_def
label = Gtk.Label()
label.set_text(label_text)
label.set_tooltip_text(adj_class.STATIC_TOOLTIP_TEXT)
label.set_vexpand(True)
label.set_hexpand(False)
label.set_valign(0.0)
label.set_margin_top(margin_top)
label.set_margin_start(3)
label.set_margin_end(3)
adj = adj_class()
adj.set_size_request(100, 22)
adj.set_vexpand(False)
adj.set_hexpand(True)
adj.set_margin_top(margin_top)
adj.set_margin_left(3)
adj.set_margin_right(3)
adj.set_valign(0.0)
self._sliders.append(adj)
grid.attach(label, 0, row, 1, 1)
grid.attach(adj, 1, row, 1, 1)
row += 1
align = Gtk.Alignment(
xalign=0.5, yalign=0.5,
xscale=1.0, yscale=0.0,
)
align.add(grid)
self._page_widget = align #: Page's layout widget
@classmethod
def get_page_icon_name(self):
return 'mypaint-tool-component-sliders'
@classmethod
def get_page_title(self):
return C_(
"color sliders panel: tab title (in tooltip)",
"Component Sliders"
)
@classmethod
def get_page_description(self):
return C_(
"color sliders panel: tab description (in tooltip)",
"Adjust individual components of the color.",
)
def get_page_widget(self):
return self._page_widget
def set_color_manager(self, manager):
ColorAdjuster.set_color_manager(self, manager)
for slider in self._sliders:
slider.set_color_manager(manager)
def render_as_icon(self, cr, size):
"""Renders as an icon into a Cairo context.
"""
# Strategy: construct tmp R,G,B sliders with a color that shows off
# their primary a bit. Render carefully (might need special handling
# for the 16px size).
from adjbases import ColorManager
mgr = ColorManager(prefs={}, datapath=".")
mgr.set_color(RGBColor(0.3, 0.3, 0.4))
adjs = [RGBRedSlider(), RGBGreenSlider(), RGBBlueSlider()]
for adj in adjs:
adj.set_color_manager(mgr)
if size <= 16:
cr.save()
for adj in adjs:
adj.BORDER_WIDTH = 1
adj.render_background_cb(cr, wd=16, ht=5)
cr.translate(0, 5)
cr.restore()
else:
cr.save()
bar_ht = int(size // 3)
offset = int((size - bar_ht*3) // 2)
cr.translate(0, offset)
for adj in adjs:
adj.BORDER_WIDTH = max(2, int(size // 16))
adj.render_background_cb(cr, wd=size, ht=bar_ht)
cr.translate(0, bar_ht)
cr.restore()
for adj in adjs:
adj.set_color_manager(None)
class RGBRedSlider (SliderColorAdjuster):
STATIC_TOOLTIP_TEXT = C_("color component slider: tooltip", "RGB Red")
def get_background_validity(self):
col = self.get_managed_color()
r, g, b = col.get_rgb()
return g, b
def get_color_for_bar_amount(self, amt):
col = RGBColor(color=self.get_managed_color())
col.r = amt
return col
def get_bar_amount_for_color(self, col):
return col.r
class RGBGreenSlider (SliderColorAdjuster):
STATIC_TOOLTIP_TEXT = C_("color component slider: tooltip", "RGB Green")
def get_background_validity(self):
col = self.get_managed_color()
r, g, b = col.get_rgb()
return r, b
def get_color_for_bar_amount(self, amt):
col = RGBColor(color=self.get_managed_color())
col.g = amt
return col
def get_bar_amount_for_color(self, col):
return col.g
class RGBBlueSlider (SliderColorAdjuster):
STATIC_TOOLTIP_TEXT = C_("color component slider: tooltip", "RGB Blue")
def get_background_validity(self):
col = self.get_managed_color()
r, g, b = col.get_rgb()
return r, g
def get_color_for_bar_amount(self, amt):
col = RGBColor(color=self.get_managed_color())
col.b = amt
return col
def get_bar_amount_for_color(self, col):
return col.b
class HSVHueSlider (SliderColorAdjuster):
STATIC_TOOLTIP_TEXT = C_("color component slider: tooltip", "HSV Hue")
samples = 4
def get_color_for_bar_amount(self, amt):
col = HSVColor(color=self.get_managed_color())
col.h = amt
return col
def get_bar_amount_for_color(self, col):
return col.h
class HSVSaturationSlider (SliderColorAdjuster):
STATIC_TOOLTIP_TEXT = C_(
"color component slider: tooltip",
u"HSV Saturation",
)
def get_color_for_bar_amount(self, amt):
col = HSVColor(color=self.get_managed_color())
col.s = amt
return col
def get_bar_amount_for_color(self, col):
return col.s
class HSVValueSlider (SliderColorAdjuster):
STATIC_TOOLTIP_TEXT = C_("color component slider: tooltip", "HSV Value")
def get_color_for_bar_amount(self, amt):
col = HSVColor(color=self.get_managed_color())
col.v = amt
return col
def get_bar_amount_for_color(self, col):
return col.v
class HCYHueSlider (SliderColorAdjuster):
STATIC_TOOLTIP_TEXT = C_("color component slider: tooltip", "HCY Hue")
samples = 4
def get_color_for_bar_amount(self, amt):
col = HCYColor(color=self.get_managed_color())
col.h = amt
return col
def get_bar_amount_for_color(self, col):
col = HCYColor(color=col)
return col.h
class HCYChromaSlider (SliderColorAdjuster):
STATIC_TOOLTIP_TEXT = C_("color component slider: tooltip", "HCY Chroma")
def get_color_for_bar_amount(self, amt):
col = HCYColor(color=self.get_managed_color())
col.c = amt
return col
def get_bar_amount_for_color(self, col):
col = HCYColor(color=col)
return col.c
class HCYLumaSlider (SliderColorAdjuster):
STATIC_TOOLTIP_TEXT = C_(
"color component slider: tooltip",
u"HCY Luma (Y')",
)
@property
def samples(self):
alloc = self.get_allocation()
len = self.vertical and alloc.height or alloc.width
len -= self.BORDER_WIDTH * 2
return min(int(len // 3), 64)
def get_color_for_bar_amount(self, amt):
col = HCYColor(color=self.get_managed_color())
col.y = amt
return col
def get_bar_amount_for_color(self, col):
col = HCYColor(color=col)
return col.y
def get_background_validity(self):
col = HCYColor(color=self.get_managed_color())
return int(col.h * 1000), int(col.c * 1000)
if __name__ == '__main__':
import os
import sys
from adjbases import ColorManager
mgr = ColorManager(prefs={}, datapath=".")
cs_adj = ComponentSlidersAdjusterPage()
cs_adj.set_color_manager(mgr)
cs_adj.set_managed_color(RGBColor(0.3, 0.6, 0.7))
if len(sys.argv) > 1:
icon_name = cs_adj.get_page_icon_name()
for dir_name in sys.argv[1:]:
cs_adj.save_icon_tree(dir_name, icon_name)
else:
# Interactive test
window = Gtk.Window()
window.add(cs_adj.get_page_widget())
window.set_title(os.path.basename(sys.argv[0]))
window.connect("destroy", lambda *a: Gtk.main_quit())
window.show_all()
Gtk.main()