This file is indexed.

/usr/lib/python3/dist-packages/ginga/canvas/types/utils.py is in python3-ginga 2.6.1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#
# utils.py -- classes for special shapes added to Ginga canvases.
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
from ginga.canvas.CanvasObject import (CanvasObjectBase, _bool, _color,
                                       register_canvas_types, get_canvas_type,
                                       colors_plus_none)
from .basic import Rectangle
from ginga.misc.ParamSet import Param


class ColorBar(CanvasObjectBase):

    @classmethod
    def get_params_metadata(cls):
        return [
            Param(name='height', type=int, default=14,
                  min=0, max=200, widget='spinbutton', incr=1,
                  description="Height of colorbar in pixels"),
            Param(name='offset', type=int, default=10,
                  min=0, max=200, widget='spinbutton', incr=1,
                  description="Offset in pixels from the top or bottom of the window"),
            Param(name='side', type=str,
                  default='bottom', valid=['top', 'bottom'],
                  description="Choose side of window to anchor color bar"),
            Param(name='showrange', type=_bool,
                  default=True, valid=[False, True],
                  description="Show the range in the colorbar"),
            Param(name='font', type=str, default='Sans Serif',
                  description="Font family for text"),
            Param(name='fontsize', type=int, default=8,
                  min=8, max=72,
                  description="Font size of text (default: 8)"),
            Param(name='linewidth', type=int, default=1,
                  min=0, max=20, widget='spinbutton', incr=1,
                  description="Width of outline"),
            Param(name='linestyle', type=str, default='solid',
                  valid=['solid', 'dash'],
                  description="Style of outline (default: solid)"),
            Param(name='color',
                  valid=colors_plus_none, type=_color, default='black',
                  description="Color of outline"),
            Param(name='alpha', type=float, default=1.0,
                  min=0.0, max=1.0, widget='spinfloat', incr=0.05,
                  description="Opacity of outline"),
            Param(name='fillalpha', type=float, default=1.0,
                  min=0.0, max=1.0, widget='spinfloat', incr=0.05,
                  description="Opacity of fill"),
            ]

    def __init__(self, height=14, offset=10, side='bottom', showrange=True,
                 font='Sans Serif', fontsize=8,
                 color='black', linewidth=1, linestyle='solid', alpha=1.0,
                 fillalpha=1.0, rgbmap=None, optimize=True, **kwdargs):
        super(ColorBar, self).__init__(height=height, offset=offset, side=side,
                                       showrange=showrange,
                                       font=font, fontsize=fontsize,
                                       color=color, linewidth=linewidth,
                                       linestyle=linestyle, alpha=alpha,
                                       fillalpha=fillalpha, **kwdargs)
        self.rgbmap = rgbmap
        self.kind = 'colorbar'

        # for drawing range
        self.t_spacing = 40
        self.tick_ht = 4


    def draw(self, viewer):
        rgbmap = self.rgbmap
        if rgbmap is None:
            rgbmap = viewer.get_rgbmap()

        width, height = viewer.get_window_size()

        loval, hival = viewer.get_cut_levels()

        cr = viewer.renderer.setup_cr(self)

        # Calculate reasonable spacing for range numbers
        cr.set_font(self.font, self.fontsize, color=self.color,
                    alpha=self.alpha)
        text = "%.4g" % (hival)
        txt_wd, txt_ht = cr.text_extents(text)
        avg_pixels_per_range_num = self.t_spacing + txt_wd

        pxwd, pxht = width, max(self.height, txt_ht + self.tick_ht + 2)
        #print("colormap size is %d,%d" % (pxwd, pxht))

        # calculate intervals for range numbers
        nums = max(int(pxwd // avg_pixels_per_range_num), 1)
        spacing = 256 // nums
        _interval = { i*spacing: True for i in range(nums) }
        #self.logger.debug("nums=%d spacing=%d intervals=%s" % (
        #    nums, spacing, _interval))

        y_base = self.offset
        if self.side == 'bottom':
            y_base = height - self.offset - pxht

        x1 = 0; x2 = pxwd
        clr_wd = pxwd // 256
        rem_px = x2 - (clr_wd * 256)
        if rem_px > 0:
            ival = 256 // rem_px
        else:
            ival = 0
        clr_ht = pxht
        #print("clr is %dx%d width=%d rem=%d ival=%d" % (
        #       width, height, clr_wd, rem_px, ival))

        dist = rgbmap.get_dist()

        j = ival; off = 0
        range_pts = []
        for i in range(256):

            wd = clr_wd
            if rem_px > 0:
                j -= 1
                if j == 0:
                    rem_px -= 1
                    j = ival
                    wd += 1
            x = off

            (r, g, b) = rgbmap.get_rgbval(i)
            color = (r/255., g/255., b/255.)

            cr.set_line(color, linewidth=0)
            cr.set_fill(color, alpha=self.fillalpha)

            cx1, cy1, cx2, cy2 = x, y_base, x+wd, y_base+pxht
            cr.draw_polygon(((cx1, cy1), (cx2, cy1), (cx2, cy2), (cx1, cy2)))

            # Draw range scale if we are supposed to
            if self.showrange and i in _interval:
                cb_pct = float(x) / pxwd
                # get inverse of distribution function and calculate value
                # at this position
                rng_pct = dist.get_dist_pct(cb_pct)
                val = float(loval + (rng_pct * (hival - loval)))
                text = "%.4g" % (val)

                rx = x
                ry = y_base + self.tick_ht + txt_ht
                ryy = y_base
                range_pts.append((rx, ry, ryy, text))

            off += wd

        cr.set_line(color=self.color, linewidth=1, alpha=self.alpha)

        # draw optional border
        if self.linewidth > 0:
            cx1, cy1, cx2, cy2 = 0, y_base, wd, y_base+pxht
            cpoints = ((cx1, cy1), (cx2, cy1), (cx2, cy2), (cx1, cy2))
            cr.draw_polygon(cpoints)

        # draw range
        if self.showrange:
            cr.set_font(self.font, self.fontsize, color=self.color,
                    alpha=self.alpha)
            for (cx, cy, cyy, text) in range_pts:
                # tick
                cr.draw_line(cx, cyy, cx, cyy+self.tick_ht)
                # number
                cr.draw_text(cx, cy, text)


class DrawableColorBar(Rectangle):

    @classmethod
    def get_params_metadata(cls):
        return [
            Param(name='x1', type=float, default=0.0, argpos=0,
                  description="First X coordinate of object"),
            Param(name='y1', type=float, default=0.0, argpos=1,
                  description="First Y coordinate of object"),
            Param(name='x2', type=float, default=0.0, argpos=2,
                  description="Second X coordinate of object"),
            Param(name='y2', type=float, default=0.0, argpos=3,
                  description="Second Y coordinate of object"),
            Param(name='cm_name', type=str, default='gray',
                  description="Color map name to draw"),
            Param(name='showrange', type=_bool,
                  default=True, valid=[False, True],
                  description="Show the range in the colorbar"),
            Param(name='font', type=str, default='Sans Serif',
                  description="Font family for text"),
            Param(name='fontsize', type=int, default=8,
                  min=8, max=72,
                  description="Font size of text (default: 8)"),
            Param(name='linewidth', type=int, default=1,
                  min=0, max=20, widget='spinbutton', incr=1,
                  description="Width of outline"),
            Param(name='linestyle', type=str, default='solid',
                  valid=['solid', 'dash'],
                  description="Style of outline (default: solid)"),
            Param(name='color',
                  valid=colors_plus_none, type=_color, default='black',
                  description="Color of outline"),
            Param(name='alpha', type=float, default=1.0,
                  min=0.0, max=1.0, widget='spinfloat', incr=0.05,
                  description="Opacity of outline"),
            Param(name='fillalpha', type=float, default=1.0,
                  min=0.0, max=1.0, widget='spinfloat', incr=0.05,
                  description="Opacity of fill"),
            ]

    def __init__(self, x1, y1, x2, y2, showrange=True,
                 font='Sans Serif', fontsize=8,
                 color='black', linewidth=1, linestyle='solid', alpha=1.0,
                 fillalpha=1.0, rgbmap=None, optimize=True, **kwdargs):
        Rectangle.__init__(self, x1, y1, x2, y2,
                           font=font, fontsize=fontsize,
                           color=color, linewidth=linewidth,
                           linestyle=linestyle, alpha=alpha,
                           fillalpha=fillalpha, **kwdargs)
        self.showrange = showrange
        self.rgbmap = rgbmap
        self.kind = 'drawablecolorbar'

        # for drawing range
        self.t_spacing = 40
        self.tick_ht = 4


    def draw(self, viewer):
        rgbmap = self.rgbmap
        if rgbmap is None:
            rgbmap = viewer.get_rgbmap()

        cpoints = self.get_cpoints(viewer)
        cx1, cy1 = cpoints[0]
        cx2, cy2 = cpoints[2]
        cx1, cy1, cx2, cy2 = self.swapxy(cx1, cy1, cx2, cy2)
        width, height = abs(cx2 - cx1), abs(cy2 - cy1)

        loval, hival = viewer.get_cut_levels()

        cr = viewer.renderer.setup_cr(self)

        # Calculate reasonable spacing for range numbers
        cr.set_font(self.font, self.fontsize, color=self.color,
                    alpha=self.alpha)
        text = "%.4g" % (hival)
        txt_wd, txt_ht = cr.text_extents(text)
        avg_pixels_per_range_num = self.t_spacing + txt_wd

        pxwd, pxht = width, max(height, txt_ht + self.tick_ht + 2)
        pxwd, pxht = max(pxwd, 1), max(pxht, 1)

        # calculate intervals for range numbers
        nums = max(int(pxwd // avg_pixels_per_range_num), 1)
        spacing = 256 // nums
        _interval = { i*spacing: True for i in range(nums) }

        y_base = cy1

        x1 = cx1; x2 = pxwd
        clr_wd = pxwd // 256
        rem_px = x2 - (clr_wd * 256)
        if rem_px > 0:
            ival = 256 // rem_px
        else:
            ival = 0
        clr_ht = pxht
        #print("clr is %dx%d width=%d rem=%d ival=%d" % (
        #       width, height, clr_wd, rem_px, ival))

        dist = rgbmap.get_dist()

        j = ival; off = cx1
        range_pts = []
        for i in range(256):

            wd = clr_wd
            if rem_px > 0:
                j -= 1
                if j == 0:
                    rem_px -= 1
                    j = ival
                    wd += 1
            x = off

            (r, g, b) = rgbmap.get_rgbval(i)
            color = (r/255., g/255., b/255.)

            cr.set_line(color, linewidth=0)
            cr.set_fill(color, alpha=self.fillalpha)

            cx1, cy1, cx2, cy2 = x, y_base, x+wd, y_base+pxht
            cr.draw_polygon(((cx1, cy1), (cx2, cy1), (cx2, cy2), (cx1, cy2)))

            # Draw range scale if we are supposed to
            if self.showrange and i in _interval:
                cb_pct = float(x) / pxwd
                # get inverse of distribution function and calculate value
                # at this position
                rng_pct = dist.get_dist_pct(cb_pct)
                val = float(loval + (rng_pct * (hival - loval)))
                text = "%.4g" % (val)

                rx = x
                ry = y_base + self.tick_ht + txt_ht
                ryy = y_base
                range_pts.append((rx, ry, ryy, text))

            off += wd

        cr.set_line(color=self.color, linewidth=1, alpha=self.alpha)

        # draw optional border
        if self.linewidth > 0:
            cx1, cy1, cx2, cy2 = 0, y_base, wd, y_base+pxht
            cpoints = ((cx1, cy1), (cx2, cy1), (cx2, cy2), (cx1, cy2))
            cr.draw_polygon(cpoints)

        # draw range
        if self.showrange:
            cr.set_font(self.font, self.fontsize, color=self.color,
                    alpha=self.alpha)
            for (cx, cy, cyy, text) in range_pts:
                # tick
                cr.draw_line(cx, cyy, cx, cyy+self.tick_ht)
                # number
                cr.draw_text(cx, cy, text)


class ModeIndicator(CanvasObjectBase):
    """
    Shows a mode indicator.

    NOTE: to get this to work properly, you need to add a callback to your
    viewer's bindmapper like so:

        bm = viewer.get_bindmap()
        bm.add_callback('mode-set', lambda *args: viewer.redraw(whence=3))

    """
    @classmethod
    def get_params_metadata(cls):
        return [
            Param(name='corner', type=str,
                  default='lr', valid=['ll', 'lr', 'ul', 'ur'],
                  description="Choose corner of window to anchor mode indicator"),
            Param(name='offset', type=int, default=10,
                  min=0, max=200, widget='spinbutton', incr=1,
                  description="Offset in pixels from the right and bottom of the window"),
            Param(name='font', type=str, default='Sans Serif',
                  description="Font family for text"),
            Param(name='fontsize', type=int, default=None,
                  min=8, max=72,
                  description="Font size of text (default: vary by scale)"),
            Param(name='color',
                  valid=colors_plus_none, type=_color, default='yellow',
                  description="Color of text"),
            Param(name='alpha', type=float, default=1.0,
                  min=0.0, max=1.0, widget='spinfloat', incr=0.05,
                  description="Opacity of outline"),
            Param(name='fillalpha', type=float, default=1.0,
                  min=0.0, max=1.0, widget='spinfloat', incr=0.05,
                  description="Opacity of fill"),
            ]

    def __init__(self, corner='lr', offset=10, font='Sans Serif', fontsize=12,
                 color='yellow', alpha=1.0, fillalpha=1.0, **kwdargs):
        super(ModeIndicator, self).__init__(corner=corner, offset=offset,
                                            font=font, fontsize=fontsize,
                                            color=color, alpha=alpha,
                                            fillalpha=fillalpha, **kwdargs)
        self.kind = 'modeindicator'
        self.xpad = 8
        self.ypad = 4

    def draw(self, viewer):

        win_wd, win_ht = viewer.get_window_size()

        bm = viewer.get_bindmap()
        mode, mode_type = bm.current_mode()

        if mode is None:
            return

        cr = viewer.renderer.setup_cr(self)

        if mode_type == 'locked':
            text = '%s [L]' % (mode)
        elif mode_type == 'softlock':
            text = '%s [SL]' % (mode)
        else:
            text = mode

        cr.set_font(self.font, self.fontsize, color=self.color,
                    alpha=self.alpha)
        txt_wd, txt_ht = cr.text_extents(text)

        # draw bg
        box_wd, box_ht = 2 * self.xpad + txt_wd, 2 * self.ypad + txt_ht
        if self.corner == 'lr':
            x_base, y_base = win_wd - self.offset - box_wd, win_ht - self.offset - box_ht
        elif self.corner == 'll':
            x_base, y_base = self.offset, win_ht - self.offset - box_ht
        if self.corner == 'ur':
            x_base, y_base = win_wd - self.offset - box_wd, self.offset
        if self.corner == 'ul':
            x_base, y_base = self.offset, self.offset

        cr.set_line('black', linewidth=0)
        cr.set_fill('black', alpha=self.fillalpha)

        cx1, cy1, cx2, cy2 = x_base, y_base, x_base + box_wd, y_base + box_ht
        cr.draw_polygon(((cx1, cy1), (cx2, cy1), (cx2, cy2), (cx1, cy2)))

        # draw fg
        cr.set_line(color=self.color, linewidth=1, alpha=self.alpha)

        cx, cy = x_base + self.xpad, y_base + txt_ht + self.ypad
        cr.draw_text(cx, cy, text)


class FocusIndicator(CanvasObjectBase):
    """
    Shows an indicator that the canvas has the keyboard/mouse focus.
    This is shown by a dotted rectangle around the perimeter of the window.

    NOTE: to get this to work properly, you need to add a callback to your
    viewer like so:

        viewer.add_callback('focus', focus_obj.focus_cb)

    """
    @classmethod
    def get_params_metadata(cls):
        return [
            Param(name='color',
                  valid=colors_plus_none, type=_color, default='white',
                  description="Color of text"),
            Param(name='alpha', type=float, default=1.0,
                  min=0.0, max=1.0, widget='spinfloat', incr=0.05,
                  description="Opacity of outline"),
            Param(name='linewidth', type=int, default=2,
                  min=0, max=20, widget='spinbutton', incr=1,
                  description="Width of outline"),
            Param(name='linestyle', type=str, default='dash',
                  valid=['solid', 'dash'],
                  description="Style of outline (default: dash)"),
            ]

    def __init__(self, color='white', linewidth=2, linestyle='dash',
                 alpha=1.0, **kwdargs):
        super(FocusIndicator, self).__init__(color=color, linewidth=linewidth,
                                             linestyle=linestyle,
                                             alpha=alpha, **kwdargs)
        self.kind = 'focusindicator'
        self.has_focus = False
        self.enabled = True

    def draw(self, viewer):

        if (not self.has_focus) or (not self.enabled):
            return

        cr = viewer.renderer.setup_cr(self)

        wd, ht = viewer.get_window_size()
        lw = self.linewidth
        off = lw // 2

        # draw a rectangle around the perimeter
        cr.set_line(color=self.color, linewidth=lw, alpha=self.alpha,
                    style=self.linestyle)

        points = ((off, off), (wd-off, off), (wd-off, ht-off), (off, ht-off))
        cr.draw_polygon(points)

    def focus_cb(self, viewer, onoff):
        has_focus = self.has_focus
        self.has_focus = onoff

        if has_focus != self.has_focus:
            viewer.redraw(whence=3)


# register our types
register_canvas_types(dict(colorbar=ColorBar, drawablecolorbar=DrawableColorBar,
                           modeindicator=ModeIndicator,
                           focusindicator=FocusIndicator))