Gnumed.timelinelib.canvas.drawing.utils

Package Gnumed :: Package timelinelib :: Package canvas :: Package drawing :: Module utils |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.canvas.drawing.utils

     1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
     2  #
     3  # This file is part of Timeline.
     4  #
     5  # Timeline is free software: you can redistribute it and/or modify
     6  # it under the terms of the GNU General Public License as published by
     7  # the Free Software Foundation, either version 3 of the License, or
     8  # (at your option) any later version.
     9  #
    10  # Timeline is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  # GNU General Public License for more details.
    14  #
    15  # You should have received a copy of the GNU General Public License
    16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
    17
    18
    19  import wx
    20
    21
    22 -class Metrics(object):
    23      """
    24      Convert between pixel coordinates and time coordinates.
    25      """
    26
    27 -    def __init__(self, size, time_type, time_period, divider_line_slider):
    28          self.width, self.height = size
    29          self.half_width = self.width / 2
    30          self.half_height = self.height / 2
    31          self.half_height = int(round(divider_line_slider * self.height))
    32          self.time_type = time_type
    33          self.time_period = time_period
    34
    35 -    def calc_exact_x(self, time):
    36          """Return the x position in pixels as a float for the given time."""
    37          return self.width * (
    38              (time - self.time_period.start_time) / self.time_period.delta()
    39          )
    40
    41 -    def calc_x(self, time):
    42          """Return the x position in pixels as an integer for the given time."""
    43          try:
    44              return int(round(self.calc_exact_x(time)))
    45          except OverflowError:
    46              if time < self.time_period.start_time:
    47                  return -1
    48              if time > self.time_period.end_time:
    49                  return self.width + 1
    50
    51 -    def calc_exact_width(self, time_period):
    52          """Return the with in pixels as a float for the given time_period."""
    53          return (self.calc_exact_x(time_period.end_time) -
    54                  self.calc_exact_x(time_period.start_time))
    55
    56 -    def calc_width(self, time_period):
    57          """Return the with in pixels as an integer for the given time_period."""
    58          return (self.calc_x(time_period.end_time) -
    59                  self.calc_x(time_period.start_time)) + 1
    60
    61 -    def get_time(self, x):
    62          """Return the time at pixel `x`."""
    63          if self.width == 0:
    64              x_percent_of_width = 0
    65          else:
    66              x_percent_of_width = float(x) / self.width
    67          return self.time_period.get_time_at_percent(x_percent_of_width)
    68
    69 -    def get_difftime(self, x1, x2):
    70          """Return the time length between two x positions."""
    71          return self.get_time(x1) - self.get_time(x2)
    72
    73
    74 -def darken_color(color, factor=0.7):
    75      if (factor < 0.0 or factor > 1.0):
    76          return color
    77      return tuple([int(x * factor) for x in color])
    78
    79
    80 -def lighten_color(color, factor=1.5):
    81      if (factor < 1.0 or factor > 255.0):
    82          return color
    83      if (color == (0, 0, 0)):
    84          color = (1, 1, 1)  # avoid multiplying factor by zero
    85      return tuple([min(int(x * factor), 255) for x in color])
    86
    87
    88 -def get_colour(rgb_tuple):
    89      return wx.Colour(rgb_tuple[0], rgb_tuple[1], rgb_tuple[2])
    90