Package Gnumed :: Package timelinelib :: Package canvas :: Package drawing :: Package drawers
[frames] | no frames]

Source Code for Package Gnumed.timelinelib.canvas.drawing.drawers

  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  GRAY_FACTOR = 1.2 
 20  LUMINENCE_FACTOR = 0.8 
 21   
 22   
23 -def get_progress_color(base_color):
24 """ 25 Return a color that is slightly brighter then the base_color. 26 For shades of gray the constant GRAY_FACTOR decides the brightness. 27 Higher factor gives more brightness. 28 For other colors the LUMINENCE_FACTOR decides the brightness. 29 Smaller factor gives more brightness. 30 """ 31 if min(base_color) == max(base_color): 32 return _adjust_gray_luminence(base_color) 33 else: 34 return _adjust_color_luminence(base_color)
35 36
37 -def _adjust_gray_luminence(base_color):
38 r = _apply_gray_factor_on_item(base_color[0]) 39 g = _apply_gray_factor_on_item(base_color[1]) 40 b = _apply_gray_factor_on_item(base_color[2]) 41 return (r, g, b)
42 43
44 -def _apply_gray_factor_on_item(item):
45 return min(255, int(GRAY_FACTOR * item))
46 47
48 -def _adjust_color_luminence(base_color):
49 h, s, _ = _rgb2hsl(base_color) 50 l = LUMINENCE_FACTOR * s 51 return _hsl2rgb(h, s, l)
52 53
54 -def _rgb2hsl(color):
55 r = float(color[0]) / 255.0 56 g = float(color[1]) / 255.0 57 b = float(color[2]) / 255.0 58 var_min = min(r, g, b) 59 var_max = max(r, g, b) 60 del_max = var_max - var_min 61 l = (var_max + var_min) / 2.0 62 if del_max == 0: 63 h = 0 64 s = 0 65 else: 66 if l < 0.5: 67 s = del_max / (var_max + var_min) 68 else: 69 s = del_max / (2.0 - var_max - var_min) 70 del_r = (((var_max - r) / 6.0) + (del_max / 2.0)) / del_max 71 del_g = (((var_max - g) / 6.0) + (del_max / 2.0)) / del_max 72 del_b = (((var_max - b) / 6.0) + (del_max / 2.0)) / del_max 73 74 if (r == var_max): 75 h = del_b - del_g 76 elif (g == var_max): 77 h = (1.0 / 3.0) + del_r - del_b 78 elif (b == var_max): 79 h = (2.0 / 3.0) + del_g - del_r 80 if (h < 0): 81 h += 1.0 82 if (h > 1.0): 83 h -= 1.0 84 return (h, s, l)
85 86
87 -def _hsl2rgb(h, s, l):
88 if s == 0: 89 r = l * 155 90 g = l * 255 91 b = l * 255 92 else: 93 if (l < 0.5): 94 var_2 = l * (1.0 + s) 95 else: 96 var_2 = (l + s) - (s * l) 97 var_1 = 2.0 * l - var_2 98 r = 255 * _hue_2_rgb(var_1, var_2, h + (1.0 / 3.0)) 99 g = 255 * _hue_2_rgb(var_1, var_2, h) 100 b = 255 * _hue_2_rgb(var_1, var_2, h - (1.0 / 3.0)) 101 return (int(r), int(g), int(b))
102 103
104 -def _hue_2_rgb(v1, v2, vh):
105 if (vh < 0): 106 vh += 1.0 107 if (vh > 1.0): 108 vh -= 1.0 109 if ((6.0 * vh) < 1): 110 return (v1 + (v2 - v1) * 6.0 * vh) 111 if ((2.0 * vh) < 1.0): 112 return (v2) 113 if ((3.0 * vh) < 2.0): 114 x = (v1 + (v2 - v1) * ((2.0 / 3.0 - vh) * 6.0)) 115 return x 116 return v1
117