/ equalizer / __init__.py
__init__.py
 1  # SPDX-FileCopyrightText: 2021 Kevin Matocha, Tim C, Jose David M
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  `equalizer`
 7  ===========
 8  """
 9  
10  import math
11  from vectorio import Rectangle
12  
13  try:
14      import bitmaptools
15  except NameError:
16      pass
17  
18  
19  # pylint: disable=invalid-name, too-many-arguments
20  def rectangle_helper(
21      x0: int,
22      y0: int,
23      height: int,
24      width: int,
25      bitmap,
26      color_index: int,
27      palette,
28      bitmaptool: bool = True,
29  ) -> None:
30      """rectangle_helper function
31      Draws a rectangle to the bitmap given using ``bitmapstools.bitmap`` or
32      ``vectorio.rectangle`` functions
33  
34      :param int x0: rectangle lower corner x position
35      :param int y0: rectangle lower corner y position
36  
37      :param int width: rectangle upper corner x position
38      :param int height: rectangle upper corner y position
39  
40      :param int color_index: palette color index to be used
41      :param palette: palette object to be used to draw the rectangle
42  
43      :param bitmap: bitmap for the rectangle to be drawn
44      :param bool bitmaptool: uses :py:func:`~bitmaptools.draw_line` to draw the rectangle.
45       when `False` uses :py:func:`~vectorio.Rectangle`
46      :return: None
47      :rtype: None
48  
49      """
50      if bitmaptool:
51          bitmaptools.fill_region(
52              bitmap, x0, y0, x0 + width - 1, y0 + height - 1, color_index
53          )
54      else:
55          Rectangle(
56              pixel_shader=palette,
57              width=width,
58              height=height,
59              x=x0,
60              y=y0,
61              color_index=color_index,
62          )
63  
64  
65  # Created by Alexander Pletzer 2001 under the PSF license
66  # https://code.activestate.com/recipes/52273-colormap-returns-an-rgb-tuple-on-a-0-to-255-scale-/
67  
68  
69  def rgb(mag, cmin, cmax):
70      """
71      Return a tuple of integers to be used in AWT/Java plots.
72      """
73      red, green, blue = float_rgb(mag, cmin, cmax)
74      return int(red * 255), int(green * 255), int(blue * 255)
75  
76  
77  def float_rgb(mag, cmin, cmax):
78      """
79      Return a tuple of floats between 0 and 1 for the red, green and
80      blue amplitudes.
81      """
82      try:
83          x = float(mag - cmin) / (cmax - cmin)
84      except ZeroDivisionError:
85          x = 0.5
86      blue = min((max(4 * (0.75 - x), 0.0), 1.0))
87      red = min((max((4 * (x - 0.25), 0.0)), 1.0))
88      green = min((max((4 * math.fabs(x - 0.5) - 1.0, 0.0)), 1.0))
89      return red, green, blue