Package pygtk_chart :: Module basics
[hide private]
[frames] | no frames]

Source Code for Module pygtk_chart.basics

  1  #!/usr/bin/env python 
  2  # 
  3  #       misc.py 
  4  #        
  5  #       Copyright 2008 Sven Festersen <sven@sven-festersen.de> 
  6  #        
  7  #       This program is free software; you can redistribute it and/or modify 
  8  #       it under the terms of the GNU General Public License as published by 
  9  #       the Free Software Foundation; either version 2 of the License, or 
 10  #       (at your option) any later version. 
 11  #        
 12  #       This program is distributed in the hope that it will be useful, 
 13  #       but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  #       GNU General Public License for more details. 
 16  #        
 17  #       You should have received a copy of the GNU General Public License 
 18  #       along with this program; if not, write to the Free Software 
 19  #       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 20  #       MA 02110-1301, USA. 
 21  """ 
 22  This module contains simple functions needed by all other modules. 
 23   
 24  Author: Sven Festersen (sven@sven-festersen.de) 
 25  """ 
 26  __docformat__ = "epytext" 
 27  import cairo 
 28  import os 
 29   
 30  REF_BOTTOM_LEFT = 0 
 31  REF_TOP_LEFT = 1 
 32  REF_TOP_RIGHT = 2 
 33  REF_BOTTOM_RIGHT = 4 
 34   
 35   
36 -def is_in_range(x, (xmin, xmax)):
37 """ 38 Use this method to test whether M{xmin <= x <= xmax}. 39 40 @type x: number 41 @type xmin: number 42 @type xmax: number 43 """ 44 return (xmin <= x and xmax >= x)
45
46 -def intersect_ranges(range_a, range_b):
47 min_a, max_a = range_a 48 min_b, max_b = range_b 49 return max(min_a, min_b), min(max_a, max_b)
50
51 -def get_center(rect):
52 """ 53 Find the center point of a rectangle. 54 55 @type rect: gtk.gdk.Rectangle 56 @param rect: The rectangle. 57 @return: A (x, y) tuple specifying the center point. 58 """ 59 return rect.width / 2, rect.height / 2
60
61 -def color_rgb_to_cairo(color):
62 """ 63 Convert a 8 bit RGB value to cairo color. 64 65 @type color: a triple of integers between 0 and 255 66 @param color: The color to convert. 67 @return: A color in cairo format. 68 """ 69 return (color[0] / 255.0, color[1] / 255.0, color[2] / 255.0)
70
71 -def color_html_to_cairo(color):
72 """ 73 Convert a html (hex) RGB value to cairo color. 74 75 @type color: html color string 76 @param color: The color to convert. 77 @return: A color in cairo format. 78 """ 79 if color[0] == '#': 80 color = color[1:] 81 (r, g, b) = (int(color[:2], 16), 82 int(color[2:4], 16), 83 int(color[4:], 16)) 84 return color_rgb_to_cairo((r, g, b))
85
86 -def color_list_from_file(filename):
87 """ 88 Read a file with one html hex color per line and return a list 89 of cairo colors. 90 """ 91 result = [] 92 if os.path.exists(filename): 93 f = open(filename, "r") 94 for line in f.readlines(): 95 line = line.strip() 96 result.append(color_html_to_cairo(line)) 97 return result
98
99 -def show_text(context, rect, x, y, text, font, size, slant=cairo.FONT_SLANT_NORMAL, weight=cairo.FONT_WEIGHT_NORMAL, underline=False, reference_point=REF_BOTTOM_LEFT):
100 context.set_font_size(max(size, 8)) 101 context.select_font_face(font, slant, weight) 102 103 text_dimensions = context.text_extents(text) 104 text_width = text_dimensions[2] 105 text_height = text_dimensions[3] 106 107 ref = (0, 0) 108 if reference_point == REF_TOP_LEFT: 109 ref = (0, text_height) 110 elif reference_point == REF_TOP_RIGHT: 111 ref = (-text_width, text_height) 112 elif reference_point == REF_BOTTOM_RIGHT: 113 ref = (-text_width, 0) 114 x = x + ref[0] 115 y = y + ref[1] 116 117 context.move_to(x, y) 118 context.show_text(text)
119